基本上和部门管理系统一致,但是有几个地方是不同的

  1. 员工的字段里面有一个字段是did(所属部门)为外键,需要在以下部分添加以下外键

    • 在创建数据库时候需要加一句,alter table staff add constraint foreign key(did) references department(id);
    • 在Staff的实体类中要,添加一个关联对象department,并创建get、set方法
    • 在staff的映射文件中,staffDao.xml文件中设置resultMap中配置多对一的标签。
      <association property="department" column="did" javaType="Department" select="com.imooc.sm.dao.DepartmentDao.selectById"/>
      通过定义select属性select="com.imooc.sm.dao.DepartmentDao.selectById"在获取员工对象的时候获取复杂的部门对象
  2. 处理完持久层以后要在业务层上与部门有些变化的地方是:
    通常insert一个Staff时,要设置一些默认的属性,包括密码,入职时间,状态等

    public void add(Staff staff) {
         staff.setPassword("123456");
         staff.setWorkTime(new Date());
         staff.setStatus("正常");
         staffDao.insert(staff);
     }