Spring Boot使用PageHelper进行分页

1、引入pom包

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

 

2、封装一个分页对象

/**
 * @author helin
 * @date 2021/4/26 15:49
 * @description 分页
 */
public class Pager<T> implements Serializable {
    private int page;//分页起始页
    private int size;//每页记录数
    private T rows;//返回的记录集合
    private long total;//总记录条数

    public int getPageTotal() {
        return pageTotal;
    }
    public void setPageTotal(int pageTotal) {
        this.pageTotal = pageTotal;
    }
    private int pageTotal;//总页数
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public T getRows() {
        return rows;
    }
    public void setRows(T rows) {
        this.rows = rows;
    }
    public long getTotal() {
        return total;
    }
    public void setTotal(long total) {
        this.total = total;
    }
}

 

3、Controller层

    /**
     * 分页查询产品网络原型表清单
     */
    @ApiOperation(value = "分页查询产品网络原型表清单")
    @PostMapping(value = "listQueryByPage")
    public Pager<List<ProdNetOrgDesigner>> listQueryByPage(@RequestBody(required = false) Map param) {
        Object page = Optional.ofNullable(param.get("page")).orElse(1);
        Object size = Optional.ofNullable(param.get("size")).orElse(10);
        return prodNetOrgDesignerService.listQueryByPage(Integer.parseInt(page.toString()),
                Integer.parseInt(size.toString()),param);
    }

 

4、Service层

    @Transactional(propagation=Propagation.SUPPORTS)
    public Pager<List<ProdNetOrgDesigner>> listQueryByPage(int page, int size, Map param) {
     // 实例化 Pager<List<ProdNetOrgDesigner>> pager = new Pager<>(); PageHelper.startPage(page,size); List<ProdNetOrgDesigner> list = prodNetOrgDesignerDao.listQuery(param); PageInfo<ProdNetOrgDesigner> pageInfo = new PageInfo<>(list); pager.setPage(pageInfo.getPageNum()); pager.setPageTotal(pageInfo.getPages()); pager.setSize(pageInfo.getPageSize()); pager.setTotal(pageInfo.getTotal()); pager.setRows(list); return pager; }

 

5、

上一篇:journalctl 日志查看方法


下一篇:自定义分页器