java pagehelper 得分页查询插件 返回过多需要自己封装

1、pom加入


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.7

2、分页得参数实体
public class Tablepar {

    private int pageNum=1;//页码
    private int pageSize=20;//数量

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "Tablepar{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                '}';
    }
3、分页得查询条件 前台传入  还是默认

PageHelper.startPage(tablepar.getPageNum(),tablepar.getPageSize());
List sysRoles = mapper.getByPage();
return new PageBean(sysRoles);

4、其中PageBean 需要自己定义需要返回得字段

 

public class PageBean implements Serializable {

    private static final long serialVersionUID = -9202109574544652243L;
    private long total;        //总记录数
    private List list;    //结果集
    private int pageNum;    // 第几页
    private int pageSize;    // 每页记录数
    private int pages;        // 总页数
    private int size;        // 当前页的数量 <= pageSize,该属性来自ArrayList的size属性

    /**
     * 包装Page对象,因为直接返回Page对象,在JSON处理以及其他情况下会被当成List来处理,
     * 而出现一些问题。
     * @param list          page结果
     * @param
     */
    public PageBean(List list) {
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
        }
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

}

你可能感兴趣的:(java)