分页插件PageHelper

导入PageHelper依赖:

compile group: 'com.github.pagehelper', name: 'pagehelper-spring-boot-starter', version: '1.2.12'

创建初始化页数工具:

package cn.cityworks.utils;

/**
 * @author: zsx
 * @date: 2019/7/2 0002
 */
public  class  PageInitUtil {
    private static Integer page;
    private static Integer rows;

    public static void init(Integer page, Integer rows){

        if(page==null){
            setPage(0);
        }else{
            setPage(page);
        }
        if(rows==null){
          setRows(9999);
        }else{
            setRows(rows);
        }
    }

    public static Integer getPage() {
        return page;
    }

    public static void setPage(Integer page) {
        PageInitUtil.page = page;
    }

    public static Integer getRows() {
        return rows;
    }

    public static void setRows(Integer rows) {
        PageInitUtil.rows = rows;
    }
}

Controller层:

@RequestMapping(value = "/getUserListByCondition", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@ResponseBody
public Result getUserListByCondition(
        @RequestParam (value="org",required = false) String org,
        @RequestParam (value="officepark",required = false) String officepark,
        @RequestParam(value="properties",required = false) String properties,
        @RequestParam(value="position",required = false) String position,
        @RequestParam(value="state",required = false) String state,
        @RequestParam(value="role",required = false) String role,
        @RequestParam(value="nameOrPhone",required = false) String nameOrPhone,
        @RequestParam(value="page",required = false) Integer  page,
        @RequestParam(value="rows",required = false) Integer  rows
) {
    Result result = new Result();
    //初始化
    PageInitUtil.init(page,rows);
    try{
        Map map= userService.getUserListByCondition(org,officepark.trim(),properties.trim(),position.trim(),state.trim(),role.trim(),nameOrPhone.trim(),PageInitUtil.getPage(),PageInitUtil.getRows());
        result.setData(map);
    }catch (Exception e){
        e.printStackTrace();
        result.setCode(999);
        result.setMessage("查询异常");
    }
    return result;
}

Service层:

public Map getUserListByCondition(String org,String officepark, String properties,  String position,  String state,  String role,  String nameOrPhone,int page, int rows){
    //使用分页插件,核心代码就这一行,页数、每页行数
    Page objects = PageHelper.startPage(page, rows);
    List list=userMapper.getUserListByCondition(org,officepark,properties,position==""?null:"%"+position+"%",state,role,nameOrPhone==null?null:"%"+nameOrPhone+"%");
    Map map=new HashMap();
    map.put("size",objects.getTotal());
    map.put("list",list);
    return map;
}
 
  

最后由前端传rows和page即可实现分页功能

你可能感兴趣的:(Java)