【狂神说Java】SpringBoot最新教程IDEA版通俗易懂20 ~ 28:员工管理系统

20、员工管理系统:准备工作

首先确定项目的结构:
在这里插入图片描述
从底层开始编写。

静态资源导入

在这里插入图片描述

(伪造)数据库

在这里插入图片描述
导入lombok,添加lombok依赖:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

编写部门表

// 部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
   

    private Integer id ;
    private String departmentName ;
}

编写员工表

@Data
@NoArgsConstructor
public class Employee {
   

    private Integer id ;
    private String lastName ;
    private String email ;
    private Integer sex ;  // 0女、1男
    private Department department ;
    private Date birth ;  // Date类用于描述日期信息

    // 定义有参构造,为了让birth内部生成
    public Employee(Integer id, String lastName, String email, Integer sex, Department department) {
   
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.sex = sex;
        this.department = department;
        this.birth = new Date();
    }
}

编写Dao层

部门

@Repository
public class DepartmentDao {
   

    // 初始化数据:模拟数据库中的数据

    // 在初始化时加载,需要static;库需要map实现
    private static Map<Integer, Department> departments = null ;

    // 下面赋初始值
    static {
   
        departments = new HashMap<>() ;  // 创建一个部门表,JDK1.8之后泛型可省略

        departments.put(1701, new Department(1701, "Fintech Department")) ;
        departments.put(1702, new Department(1702, "Administrative Department")) ;
        departments.put(1703, new Department(1703, "Human Resources Department")) ;
        departments.put(1704, new Department(1704, "Department of trade and Finance")) ;
        departments.put(1705, new Department(1705, "Retail Business Department")) ;

    }

    /* 数据库的增删查改 */

    // 获得所有部门信息
    public Collection<Department> getDepartments() {
   
        return departments.values() ;
    }

    // 通过部门id得到部门信息
    public Department getDepartmentById(Integer id) {
   
        return departments.get(id) ;
    }
}

员工

@Repository
public class EmployeeDao {
   

    // 初始化数据:模拟数据库中的数据

    // 在初始化时加载,需要static;库需要map实现
    private static Map<Integer, Employee> employees = null ;

    // 首先员工有所属部门
    @Autowired  // 可以使用DepartmentDao了
    private DepartmentDao departmentDao ;  // 这个操作要求这两个Dao被@Repository ➡ 被Spring托管

    // 下面赋初始值
    static {
   
        employees = new HashMap<>() ;  // 创建一个部门表,JDK1.8之后泛型可省略

        employees.put(13001, new Employee(13001, "Hathway", "[email protected]", 0, new Department(1704, "Department of trade and Finance"))) ;
        employees.put(13002, new Employee(13002, "Dexter", "[email protected]", 1, new Department(1701, "Fintech Department"))) ;
        employees.put(13003, new Employee(13003, "Nana", "[email protected]", 0, new Department(1703, "Human Resources Department"))) ;
        employees.put(13004, new Employee(13004, "Choco", "[email protected]", 0, new Department(1705, "Retail Dusiness Department"))) ;
        employees.put(13005, new Employee(13005, "Tataki", "[email protected]", 1, new Department(1702, "Administrative Department"))) ;
        // 因为static的原因,无法用departmentDao.getDepartmentById(Integer id)方法来获取部门
    }

    /* 增删查改 */
    
    // 增加一个员工
    private static Integer initId = 13006 ;
    public void add(Employee employee) {
   
        if (null == employee.getId()) {
   
            employee.setId(initId++) ;
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId()
  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值