1. 程式人生 > >後端分頁工具PageHelper

後端分頁工具PageHelper

1.引入maven

<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.0.0</version>
    </dependency>

2.service應用

		//pageNum、pageSize自定義
		PageHelper.startPage(pageNum,pageSize);//1
		List<Role> roles = roleMapper.selectAllRole(id,roleName,roleClass,createTime);//2
        PageInfo<Role> pageInfo = new PageInfo<>(roles);//3
        //分頁查詢
        Map all =new HashMap();
        Map num = new HashMap();
        List data = new ArrayList();
        for(int i = 0;i<roles.size();i++){
            if(i == 0){
                num.put("pages",pageInfo.getPages());
                num.put("total",pageInfo.getTotal());
                num.put("pageNum",pageInfo.getPageNum());
                num.put("pageSize",pageInfo.getSize());
            }
            data.add(roles.get(i));
        }
        all.put("num",num);
        all.put("data",data);
        return all;

注:上面程式碼中的1.2.3順序不能亂,要將查詢結果放在PageHelper和PageInfo中間