springboot:员工管理系统-准备数据

项目准备

    做一个员工管理系统,实现员工的CRUD,我们这里不用数据库只在处理数据逻辑中模拟一下数据就好了,我们使用之前的springboot项目把之前使用过的一些代码删掉(controller,resources)

    目录结构
在这里插入图片描述

    需要改动的地方已经为你们标注出来了

开始代码编写

    步骤:

  1. 编写pojo实体类,为了偷懒引入了lombok

    1. 部门
    //部门表
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Department {
    
    private Integer id;
    private String departmentName;
    
    }
    
    1. 员工
    //员工表
    @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会自动赋值当前的时间。
     }
    }
    
    

    注意点:我们这里用的主外键,员工的部门是在部门里面取出来的内容

  1. 编写dao层,实现pojo实体类,做出增删改查,注意看:我们会在静态代码块里面模仿数据库中的数据

    1. 部门
    //部门操作
    @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);
     }
    }
    
    
    1. 员工
    //员工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","[email protected]",0,new Department(101,"教学部")));
        employees.put(1002,new Employee(1002,"BB","[email protected]",1,new Department(102,"市场部")));
        employees.put(1003,new Employee(1003,"CC","[email protected]",0,new Department(103,"校验部")));
        employees.put(1004,new Employee(1004,"DD","[email protected]",1,new Department(104,"运营部")));
        employees.put(1005,new Employee(1005,"EE","[email protected]",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目录下构建我们的静态资源以及页面
在这里插入图片描述

准备工作就到这里了,下一章会带大家进行首页的测试

发布了65 篇原创文章 · 获赞 0 · 访问量 661

猜你喜欢

转载自blog.csdn.net/adsdaas/article/details/105043012