(未连接数据库版)
员工管理系统准备工作:
idea 创建 web 项目
pom.xml 导入 Thymeleaf 和 lombok 的 jar 包:
<!-- Thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
假设几张表
pojo 包下新建 Department 部门实体类:
package com.demo.web.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
pojo 包下新建 Employee 员工实体类:
package com.demo.web.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
//员工表
@Data
@NoArgsConstructor
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender; //0:女 1:男
private Department department;
private Date birthday;
public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
//默认创建日期
this.birthday = new Date();
}
}
在 Dao 层(访问数据库的类)新建 DepartmentDao.java
模拟数据库中的数据,通过 put 方法插入数据,并获得部门信息
package com.demo.web.dao;
import com.demo.web.pojo.Department;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
//部门dao
@Repository
public class DepartmentDao {
//模拟数据库中的数据
private static Map<Integer, Department> departments = null;
static {
departments = new HashMap<Integer,Department>(); //创建一个部门表
departments.put(101,new Department(101,"研发部"));
departments.put(102,new Department(102,"测试部"));
departments.put(103,new Department(103,"运维部"));
departments.put(104,new Department(104,"产品部"));
}
//获取所有部门信息
public Collection<Department> getDepartments() {
return departments.values();
}
//通过id获取部门信息
public Department getDepartmentById(Integer id) {
return departments.get(id);
}
}
在 Dao 层(访问数据库的类)新建 EmployeeDao.java
模拟数据库建表插入数据,设置一些方法
@Autowired 自动装配
package com.demo.web.dao;
import com.demo.web.pojo.Department;
import com.demo.web.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
//员工dao
@Repository
public class EmployeeDao {
//模拟数据库中的数据
private static Map<Integer, Employee> employees = null;
//员工所属部门
@Autowired //自动装配
private DepartmentDao departmentDao;
static {
employees = new HashMap<Integer, Employee>(); //创建一个员工表
employees.put(1001,new Employee(1001,"张三1","123@qq.com",1,new Department(101,"研发部")));
employees.put(1002,new Employee(1002,"张三2","123@qq.com",0,new Department(102,"测试部")));
employees.put(1003,new Employee(1003,"张三3","123@qq.com",1,new Department(103,"运维部")));
employees.put(1004,new Employee(1004,"张三4","123@qq.com",0,new Department(104,"产品部")));
}
//主键自增
private static Integer initId = 1005;
//增加一个员工
public void save(Employee employee) {
if(employee.getId()==null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
//查询所有员工信息
public Collection<Employee> getAll() {
return employees.values();
}
//通过id查询员工
public Employee getEmployeeById(Integer id) {
return employees.get(id);
}
//通过id删除员工
public void delete(Integer id) {
employees.remove(id);
}
}
(未完)