模拟数据源
建立pojo
Employee
package com.qw.springboot_test.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.omg.CORBA.INTERNAL;
import java.util.Date;
/**
* @author QBS @win10
* @version 1.0
* @date 2021/5/24 0:30
* 类说明
*/
@Data
@NoArgsConstructor
@ToString
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender; //0:女 1:男
private Department department;
private Date date;
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.date = new Date();
}
}
Department
package com.qw.springboot_test.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* @author QBS @win10
* @version 1.0
* @date 2021/5/24 0:29
* 部门
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Department {
private Integer id;
private String departmentName;
}
模拟Dao
EmployeeDao
@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,"AA","A12313@qq.com",1,new Department(101,"后勤部")));
employees.put(1002,new Employee(1002,"BB","B12313@qq.com",0,new Department(102,"后勤部")));
employees.put(1003,new Employee(1003,"CC","C12313@qq.com",1,new Department(103,"后勤部")));
employees.put(1004,new Employee(1004,"DD","D12313@qq.com",0,new Department(104,"后勤部")));
employees.put(1005,new Employee(1005,"EE","E12313@qq.com",1,new Department(105,"后勤部")));
}
/**
* 增加一个员工
* 主键自增
*/
private static Integer initId = 1006;
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> getEmployee(){
return employees.values();
}
/**
* 通过id查询员工
*
*/
public Employee getEmployeeById(Integer id){
return employees.get(id);
}
/**
* 删除员工
*/
public void deleteEmployee(Integer id){
employees.remove(id);
}
}
DepartmentDao
@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,"运营部"));
departments.put(105,new Department(105,"后勤部"));
}
/**
* 所有部门信息
*/
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}