28.员工管理后端接口设计

创建分页查询的model

RespPageBean

public class RespPageBean {
    private Long total;
    private List<?> data;
      //set get

EmployeeController

@RestController
@RequestMapping("/system/emp")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @GetMapping("/")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer size) {
        return employeeService.getEmployeeByPage(page, size);
    }
}

EmployeeService

@Service
public class EmployeeService {

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

EmployeeMapper

public interface EmployeeMapper {

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

    Long getTotal();
}

EmployeeMapper.xml

  <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>
  <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 LIMIT #{page},#{size};
  </select>
  <select id="getTotal" resultType="Long">
    select count(*) from employee
  </select>

测试

posted @ 2020-08-09 19:32  柒丶月  阅读(216)  评论(0编辑  收藏  举报