对于新手而言,想要深度学习是需要一些数据来做测试的,那么这些数据从哪来呢?
此时我们就要学习如何创建模拟数据:
首先我们先创建两个构造器:Department和employee,其属性自己随便整:
废话不多说了,直接看代码吧!
先写Department:
@Data
public class Department {
private Integer id;
private String departmentName;
public Department() {
}
public Department(Integer id, String departmentName) {
this.id = id;
this.departmentName = departmentName;
}
}
//部门dao
@Resource
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();
}
//通过id得到部门
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}
然后是Employee:
@Data
//员工表
public class Employee {
private Integer id;
private String LastName;
private String email;
private Integer gender;//0代表女,1代表男
private Date birth;
private Department department;
public Employee() {
}
public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
this.id = id;
LastName = lastName;
this.email = email;
this.gender = gender;
this.birth = new Date();//默认的创建日期
this.department = department;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
//员工dao
@Resource
public class EmployeeDao {
//模拟数据库中的数据
private static Map<Integer, Employee> employees = null;
@Autowired
private Department department;
static {
employees = new HashMap<Integer, Employee>();//创建一个部门表
employees.put(101,new Employee(101,"袁","771593784@qq.com",1,new Department(101,"教育部")));
employees.put(102,new Employee(102,"赵","771593785@qq.com",1,new Department(102,"事业部")));
employees.put(103,new Employee(103,"钱","771593786@qq.com",1,new Department(103,"教研部")));
employees.put(104,new Employee(104,"孙","771593787@qq.com",1,new Department(104,"后勤部")));
employees.put(105,new Employee(105,"李","771593788@qq.com",1,new Department(105,"小卖部")));
}
//主键自增
private static Integer initid = 106;
//增加一个员工
public void save(Employee employee){
if (employee.getId()==null){
employee.setId(initid++);
}
}
//查询全部员工信息
public Collection<Employee> getAll(){
return employees.values();
}
//通过id查询员工
public Employee getEmployeeId(Integer id){
return employees.get(id);
}
//通过id删除员工
public void delete(Integer id){
employees.remove(id);
}