天天看点

30.员工管理后端

EmployeeController

@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;

    //数据分页
    @GetMapping("/")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer size,
                                          String name) {
        return employeeService.getEmployeeByPage(page, size,name);
    }
    //插入数据
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee){
        if (employeeService.addEmp(employee) == 1) {
            return RespBean.ok("插入成功");
        }
        return RespBean.error("插入失败");
    }
    //删除数据
    @DeleteMapping("/{id}")
    public RespBean deleteEmpById(@PathVariable("id") Integer id) {
        if (employeeService.deleteEmpById(id) == 1) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //修改数据
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee) {
        if (employeeService.updateEmp(employee) == 1) {
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @GetMapping("/nations")
    public List<Nation> getAllNation(){
        return nationService.getAllNation();
    }
    @GetMapping("/politicsstatus")
    public List<Politicsstatus> getAllPoliticsstatus(){
        return politicsstatusService.getAllPoliticsstatus();
    }
    @GetMapping("/departments")
    public List<Department> getAllDepartments(){
        return departmentService.getAllDepartments();
    }
    @GetMapping("/joblevels")
    public List<Joblevel> getAllJoblevels(){
        return jobLevelService.getAllJoblevels();
    }

    @GetMapping("/positions")
    public List<Position> getAllPosition(){
        return positionService.getAllPositons();
    }
    //数据库中最大工号+1
    @GetMapping("/maxWorkID")
    public RespBean getMaxWorkID(){
        return RespBean.build().setStatus(200).setObj(String.format("%08d",
                employeeService.maxWorkID()+1));
    }
}

           

在Employee上添加下面代码,格式化时间

@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
           

RespBean

把RespBean改为建造者模式

public class RespBean {
    private Integer status;
    private String msg;
    private Object obj;

    public static RespBean build(){
        return new RespBean();
    }

    public static RespBean ok(String msg){
        return new RespBean(200, msg, null);
    }

    public static RespBean ok(String msg,Object obj){
        return new RespBean(200, msg, obj);
    }

    public static RespBean error(String msg){
        return new RespBean(500, msg, null);
    }

    public static RespBean error(String msg,Object obj){
        return new RespBean(500, msg, obj);
    }



    private RespBean(){

    }

    private RespBean(Integer status, String msg, Object obj) {
        this.status = status;
        this.msg = msg;
        this.obj = obj;
    }

    public Integer getStatus() {
        return status;
    }

    public RespBean setStatus(Integer status) {
        this.status = status;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public RespBean setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getObj() {
        return obj;
    }

    public RespBean setObj(Object obj) {
        this.obj = obj;
        return this;
    }
}
           

EmployeeService

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    SimpleDateFormat YearFormat = new SimpleDateFormat("yyyy");
    SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    public RespPageBean getEmployeeByPage(Integer page, Integer size, String name) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(page, size, name);
        Long total = employeeMapper.getTotal(name);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(data);
        respPageBean.setTotal(total);
        return respPageBean;
    }

    public Integer addEmp(Employee employee) {
        //自动计算合同期限
        Date begincontract = employee.getBegincontract();
        Date endcontract = employee.getEndcontract();
        double month =
                (Double.parseDouble(YearFormat.format(endcontract)) - Double.parseDouble(YearFormat.format(begincontract))) * 12 - (Double.parseDouble(monthFormat.format(endcontract)) - Double.parseDouble(monthFormat.format(begincontract)));
        employee.setContractterm(Double.parseDouble(decimalFormat.format(month/12)));
        return employeeMapper.insertSelective(employee);
    }

    public Integer maxWorkID() {
        return employeeMapper.maxWorkID();
    }

    public Integer deleteEmpById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    public Integer updateEmp(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }
}
           

EmployeeMapper

public interface EmployeeMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Employee record);

    int insertSelective(Employee record);

    Employee selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Employee record);

    int updateByPrimaryKey(Employee record);

    List<Employee> getEmployeeByPage(@Param("page") Integer page, @Param("size") Integer size,
                                     @Param("keyword")String name);

    Long getTotal(String keyword);

    Integer maxWorkID();
}
           

EmployeeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.qwl.vhr.mapper.EmployeeMapper" >
  <resultMap id="BaseResultMap" type="com.qwl.vhr.model.Employee" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="gender" property="gender" jdbcType="CHAR" />
    <result column="birthday" property="birthday" jdbcType="DATE" />
    <result column="idCard" property="idcard" jdbcType="CHAR" />
    <result column="wedlock" property="wedlock" jdbcType="CHAR" />
    <result column="nationId" property="nationid" jdbcType="INTEGER" />
    <result column="nativePlace" property="nativeplace" jdbcType="VARCHAR" />
    <result column="politicId" property="politicid" jdbcType="INTEGER" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="address" property="address" jdbcType="VARCHAR" />
    <result column="departmentId" property="departmentid" jdbcType="INTEGER" />
    <result column="jobLevelId" property="joblevelid" jdbcType="INTEGER" />
    <result column="posId" property="posid" jdbcType="INTEGER" />
    <result column="engageForm" property="engageform" jdbcType="VARCHAR" />
    <result column="tiptopDegree" property="tiptopdegree" jdbcType="CHAR" />
    <result column="specialty" property="specialty" jdbcType="VARCHAR" />
    <result column="school" property="school" jdbcType="VARCHAR" />
    <result column="beginDate" property="begindate" jdbcType="DATE" />
    <result column="workState" property="workstate" jdbcType="CHAR" />
    <result column="workID" property="workid" jdbcType="CHAR" />
    <result column="contractTerm" property="contractterm" jdbcType="DOUBLE" />
    <result column="conversionTime" property="conversiontime" jdbcType="DATE" />
    <result column="notWorkDate" property="notworkdate" jdbcType="DATE" />
    <result column="beginContract" property="begincontract" jdbcType="DATE" />
    <result column="endContract" property="endcontract" jdbcType="DATE" />
    <result column="workAge" property="workage" jdbcType="INTEGER" />
  </resultMap>
  <resultMap id="AllEmployeeInfo" type="com.qwl.vhr.model.Employee" 
             extends="BaseResultMap">
    <association javaType="com.qwl.vhr.model.Nation" property="nation">
      <id column="nid" property="id"/>
      <result column="nname" property="name"/>
    </association>
    <association javaType="com.qwl.vhr.model.Position" property="position">
      <id column="posid" property="id"/>
      <result column="posname" property="name"/>
    </association>
    <association javaType="com.qwl.vhr.model.Politicsstatus" property="politicsstatus">
      <id column="pid" property="id"/>
      <result column="pname" property="name"/>
    </association>
    <association javaType="com.qwl.vhr.model.Department" property="department">
      <id column="did" property="id"/>
      <result column="dname" property="name"/>
    </association>
    <association javaType="com.qwl.vhr.model.Joblevel" property="joblevel">
      <id column="jid" property="id"/>
      <result column="jname" property="name"/>
    </association>
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, gender, birthday, idCard, wedlock, nationId, nativePlace, politicId, email, 
    phone, address, departmentId, jobLevelId, posId, engageForm, tiptopDegree, specialty, 
    school, beginDate, workState, workID, contractTerm, conversionTime, notWorkDate, 
    beginContract, endContract, workAge
  </sql>
  <select id="maxWorkID" resultType="java.lang.Integer">
    select max(workId) from employee
  </select>
  <select id="getEmployeeByPage" resultMap="AllEmployeeInfo">
    select e.*,n.id as nid,n.`name`as nname, d.id as did,d.`name`as dname,j.id as jid,j.`name`as jname,pos.id as posid,pos.`name`as posname
 from employee e,nation n,politicsstatus p,department d,joblevel j,position pos
where e.nationId=n.id and e.politicId=p.id and
e.departmentId=d.id and e.jobLevelId=j.id and e.posId=pos.id
<if test="keyword!=null and keyword!=''">
 and e.name like concat('%',#{keyword},'%')
</if>
LIMIT #{page},#{size};
  </select>
  <select id="getTotal" resultType="Long">
    select count(*) from employee
    <if test="keyword!=null and keyword!=''">
      where name like concat('%',#{keyword},'%')
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from employee
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from employee
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.qwl.vhr.model.Employee" >
    insert into employee (id, name, gender, 
      birthday, idCard, wedlock, nationId, 
      nativePlace, politicId, email, 
      phone, address, departmentId, 
      jobLevelId, posId, engageForm, 
      tiptopDegree, specialty, school, 
      beginDate, workState, workID, 
      contractTerm, conversionTime, notWorkDate, 
      beginContract, endContract, workAge
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR}, 
      #{birthday,jdbcType=DATE}, #{idcard,jdbcType=CHAR}, #{wedlock,jdbcType=CHAR}, #{nationid,jdbcType=INTEGER}, 
      #{nativeplace,jdbcType=VARCHAR}, #{politicid,jdbcType=INTEGER}, #{email,jdbcType=VARCHAR}, 
      #{phone,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{departmentid,jdbcType=INTEGER}, 
      #{joblevelid,jdbcType=INTEGER}, #{posid,jdbcType=INTEGER}, #{engageform,jdbcType=VARCHAR}, 
      #{tiptopdegree,jdbcType=CHAR}, #{specialty,jdbcType=VARCHAR}, #{school,jdbcType=VARCHAR}, 
      #{begindate,jdbcType=DATE}, #{workstate,jdbcType=CHAR}, #{workid,jdbcType=CHAR}, 
      #{contractterm,jdbcType=DOUBLE}, #{conversiontime,jdbcType=DATE}, #{notworkdate,jdbcType=DATE}, 
      #{begincontract,jdbcType=DATE}, #{endcontract,jdbcType=DATE}, #{workage,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.qwl.vhr.model.Employee" >
    insert into employee
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="gender != null" >
        gender,
      </if>
      <if test="birthday != null" >
        birthday,
      </if>
      <if test="idcard != null" >
        idCard,
      </if>
      <if test="wedlock != null" >
        wedlock,
      </if>
      <if test="nationid != null" >
        nationId,
      </if>
      <if test="nativeplace != null" >
        nativePlace,
      </if>
      <if test="politicid != null" >
        politicId,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="phone != null" >
        phone,
      </if>
      <if test="address != null" >
        address,
      </if>
      <if test="departmentid != null" >
        departmentId,
      </if>
      <if test="joblevelid != null" >
        jobLevelId,
      </if>
      <if test="posid != null" >
        posId,
      </if>
      <if test="engageform != null" >
        engageForm,
      </if>
      <if test="tiptopdegree != null" >
        tiptopDegree,
      </if>
      <if test="specialty != null" >
        specialty,
      </if>
      <if test="school != null" >
        school,
      </if>
      <if test="begindate != null" >
        beginDate,
      </if>
      <if test="workstate != null" >
        workState,
      </if>
      <if test="workid != null" >
        workID,
      </if>
      <if test="contractterm != null" >
        contractTerm,
      </if>
      <if test="conversiontime != null" >
        conversionTime,
      </if>
      <if test="notworkdate != null" >
        notWorkDate,
      </if>
      <if test="begincontract != null" >
        beginContract,
      </if>
      <if test="endcontract != null" >
        endContract,
      </if>
      <if test="workage != null" >
        workAge,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="gender != null" >
        #{gender,jdbcType=CHAR},
      </if>
      <if test="birthday != null" >
        #{birthday,jdbcType=DATE},
      </if>
      <if test="idcard != null" >
        #{idcard,jdbcType=CHAR},
      </if>
      <if test="wedlock != null" >
        #{wedlock,jdbcType=CHAR},
      </if>
      <if test="nationid != null" >
        #{nationid,jdbcType=INTEGER},
      </if>
      <if test="nativeplace != null" >
        #{nativeplace,jdbcType=VARCHAR},
      </if>
      <if test="politicid != null" >
        #{politicid,jdbcType=INTEGER},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="departmentid != null" >
        #{departmentid,jdbcType=INTEGER},
      </if>
      <if test="joblevelid != null" >
        #{joblevelid,jdbcType=INTEGER},
      </if>
      <if test="posid != null" >
        #{posid,jdbcType=INTEGER},
      </if>
      <if test="engageform != null" >
        #{engageform,jdbcType=VARCHAR},
      </if>
      <if test="tiptopdegree != null" >
        #{tiptopdegree,jdbcType=CHAR},
      </if>
      <if test="specialty != null" >
        #{specialty,jdbcType=VARCHAR},
      </if>
      <if test="school != null" >
        #{school,jdbcType=VARCHAR},
      </if>
      <if test="begindate != null" >
        #{begindate,jdbcType=DATE},
      </if>
      <if test="workstate != null" >
        #{workstate,jdbcType=CHAR},
      </if>
      <if test="workid != null" >
        #{workid,jdbcType=CHAR},
      </if>
      <if test="contractterm != null" >
        #{contractterm,jdbcType=DOUBLE},
      </if>
      <if test="conversiontime != null" >
        #{conversiontime,jdbcType=DATE},
      </if>
      <if test="notworkdate != null" >
        #{notworkdate,jdbcType=DATE},
      </if>
      <if test="begincontract != null" >
        #{begincontract,jdbcType=DATE},
      </if>
      <if test="endcontract != null" >
        #{endcontract,jdbcType=DATE},
      </if>
      <if test="workage != null" >
        #{workage,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.qwl.vhr.model.Employee" >
    update employee
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="gender != null" >
        gender = #{gender,jdbcType=CHAR},
      </if>
      <if test="birthday != null" >
        birthday = #{birthday,jdbcType=DATE},
      </if>
      <if test="idcard != null" >
        idCard = #{idcard,jdbcType=CHAR},
      </if>
      <if test="wedlock != null" >
        wedlock = #{wedlock,jdbcType=CHAR},
      </if>
      <if test="nationid != null" >
        nationId = #{nationid,jdbcType=INTEGER},
      </if>
      <if test="nativeplace != null" >
        nativePlace = #{nativeplace,jdbcType=VARCHAR},
      </if>
      <if test="politicid != null" >
        politicId = #{politicid,jdbcType=INTEGER},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="departmentid != null" >
        departmentId = #{departmentid,jdbcType=INTEGER},
      </if>
      <if test="joblevelid != null" >
        jobLevelId = #{joblevelid,jdbcType=INTEGER},
      </if>
      <if test="posid != null" >
        posId = #{posid,jdbcType=INTEGER},
      </if>
      <if test="engageform != null" >
        engageForm = #{engageform,jdbcType=VARCHAR},
      </if>
      <if test="tiptopdegree != null" >
        tiptopDegree = #{tiptopdegree,jdbcType=CHAR},
      </if>
      <if test="specialty != null" >
        specialty = #{specialty,jdbcType=VARCHAR},
      </if>
      <if test="school != null" >
        school = #{school,jdbcType=VARCHAR},
      </if>
      <if test="begindate != null" >
        beginDate = #{begindate,jdbcType=DATE},
      </if>
      <if test="workstate != null" >
        workState = #{workstate,jdbcType=CHAR},
      </if>
      <if test="workid != null" >
        workID = #{workid,jdbcType=CHAR},
      </if>
      <if test="contractterm != null" >
        contractTerm = #{contractterm,jdbcType=DOUBLE},
      </if>
      <if test="conversiontime != null" >
        conversionTime = #{conversiontime,jdbcType=DATE},
      </if>
      <if test="notworkdate != null" >
        notWorkDate = #{notworkdate,jdbcType=DATE},
      </if>
      <if test="begincontract != null" >
        beginContract = #{begincontract,jdbcType=DATE},
      </if>
      <if test="endcontract != null" >
        endContract = #{endcontract,jdbcType=DATE},
      </if>
      <if test="workage != null" >
        workAge = #{workage,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.qwl.vhr.model.Employee" >
    update employee
    set name = #{name,jdbcType=VARCHAR},
      gender = #{gender,jdbcType=CHAR},
      birthday = #{birthday,jdbcType=DATE},
      idCard = #{idcard,jdbcType=CHAR},
      wedlock = #{wedlock,jdbcType=CHAR},
      nationId = #{nationid,jdbcType=INTEGER},
      nativePlace = #{nativeplace,jdbcType=VARCHAR},
      politicId = #{politicid,jdbcType=INTEGER},
      email = #{email,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      departmentId = #{departmentid,jdbcType=INTEGER},
      jobLevelId = #{joblevelid,jdbcType=INTEGER},
      posId = #{posid,jdbcType=INTEGER},
      engageForm = #{engageform,jdbcType=VARCHAR},
      tiptopDegree = #{tiptopdegree,jdbcType=CHAR},
      specialty = #{specialty,jdbcType=VARCHAR},
      school = #{school,jdbcType=VARCHAR},
      beginDate = #{begindate,jdbcType=DATE},
      workState = #{workstate,jdbcType=CHAR},
      workID = #{workid,jdbcType=CHAR},
      contractTerm = #{contractterm,jdbcType=DOUBLE},
      conversionTime = #{conversiontime,jdbcType=DATE},
      notWorkDate = #{notworkdate,jdbcType=DATE},
      beginContract = #{begincontract,jdbcType=DATE},
      endContract = #{endcontract,jdbcType=DATE},
      workAge = #{workage,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>