项目准备
做一个员工管理系统,实现员工的CRUD,我们这里不用数据库只在处理数据逻辑中模拟一下数据就好了,我们使用之前的springboot项目把之前使用过的一些代码删掉(controller,resources)
目录结构
需要改动的地方已经为你们标注出来了
开始代码编写
步骤:
-
编写pojo实体类,为了偷懒引入了lombok
- 部门
//部门表 @Data @AllArgsConstructor @NoArgsConstructor public class Department { private Integer id; private String departmentName; }
- 员工
//员工表 @Data @AllArgsConstructor @NoArgsConstructor public class Employee { private Integer id; private String lastName; private String email; private Integer gender;//0:女 1:男 private Department department; private Date birth; 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.birth = new Date();//这里注意,这个有参构造方法中,birth=new Date。意思是birth会自动赋值当前的时间。 } }
注意点:我们这里用的主外键,员工的部门是在部门里面取出来的内容
-
编写dao层,实现pojo实体类,做出增删改查,注意看:我们会在静态代码块里面模仿数据库中的数据
- 部门
//部门操作 @Repository public class DepartmentDao { //模拟数据库中的数据 //生成一个Map集合departments,初始化赋值为空 private static Map<Integer, Department> departments = null; // 给departments中赋值 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); } }
- 员工
//员工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,"AA","aHaor1250@outlook.com",0,new Department(101,"教学部"))); employees.put(1002,new Employee(1002,"BB","bHaor1250@outlook.com",1,new Department(102,"市场部"))); employees.put(1003,new Employee(1003,"CC","cHaor1250@outlook.com",0,new Department(103,"校验部"))); employees.put(1004,new Employee(1004,"DD","dHaor1250@outlook.com",1,new Department(104,"运营部"))); employees.put(1005,new Employee(1005,"EE","eHaor1250@outlook.com",0,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> getAll(){ return employees.values(); } //通过id查询员工 public Employee getEmployeeBuId(Integer id) { return employees.get(id); } //删除员工通过id public void delete(Integer id){ employees.remove(id); } }
导入静态资源
和大家分享一下静态资源,有意向的可以点击下载:
链接:https://pan.baidu.com/s/1tc3gDuX8uIy4039BWSzhFg
提取码:5m3q
下载好后我们在resources目录下构建我们的静态资源以及页面
准备工作就到这里了,下一章会带大家进行首页的测试