其他分享
首页 > 其他分享> > pagehelper分页工具

pagehelper分页工具

作者:互联网

pagehelper分页工具

pom.xml

<!--pagehelper -->
<dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-starter</artifactId>
   <version>1.2.12</version>
</dependency>

yaml

# 分页插件配置
pagehelper:
  helperDialect: mysql
  supportMethodsArguments: true

使用分页插件,在查询前使用分页插件,原理:统一拦截sql,为其提供分页功能

public PagedGridResult.......
/**
 * page: 第几页
 * pageSize: 每页显示条数
 */
PageHelper.startPage(page, pageSize);
//脱敏
List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map);
for (ItemCommentVO vo : list) {
    vo.setNickname(DesensitizationUtil.commonDisplay(vo.getNickname()));
}

return setterPagedGrid(list, page,pagenum);

分页数据封装到 PagedGridResult.java 传给前端

    public PagedGridResult setterPagedGrid(List<?> list, Integer page,Integer pagenum) {
        PageInfo<?> pageList = new PageInfo<>(list);
        PagedGridResult grid = new PagedGridResult();
        grid.setPage(page);
        final int size = list.size();
        Integer endsize=(page)*pagenum> size ? size  :(page)*pagenum;
         List<?> objects = list.subList((page-1) * pagenum, endsize);
        grid.setRows(objects);
         double ceil = (double) size / (double)pagenum;
        grid.setTotal(new Double(Math.ceil(ceil)).intValue());
        grid.setRecords(pageList.getTotal());
        return grid;
    }

PagedGridResult.java

public class PagedGridResult {
   
   private int page;        // 当前页数
   private int total;       // 总页数 
   private long records;     // 总记录数
   private List<?> rows;     // 每行显示的内容

   public int getPage() {
      return page;
   }
   public void setPage(int page) {
      this.page = page;
   }
   public int getTotal() {
      return total;
   }
   public void setTotal(int total) {
      this.total = total;
   }
   public long getRecords() {
      return records;
   }
   public void setRecords(long records) {
      this.records = records;
   }
   public List<?> getRows() {
      return rows;
   }
   public void setRows(List<?> rows) {
      this.rows = rows;
   }
}

返回参数说明