mybatis分页插件pagehelper详解

mybatis分页插件pagehelper详解

一 、使用插件

1、老规矩,导入maven依赖

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

2、看如下示例:

public PageBean findDeviceVersionList(Integer pageSize, Integer pageNum) {

    //分页
    PageHelper.startPage(pageNum, pageSize);

    //查询数据
    List userDeviceVersionModelList = deviceVersionMapper.selectAll();

    //转化vo
    PageBean deviceVoPageInfo = new PageBean<>(userDeviceVersionModelList);

    return deviceVoPageInfo;
}

PageHelper.startPage(pageNum, pageSize);放在最前面
后面跟着你的查询sql语句
最后转化为PageBean

他的原理就是将 PageHelper.startPage(pageNum, pageSize);后面的第一句sql加上limit来实现分页的效果。

二、特殊情况

现在要讲的是特殊情况:当你需要查出指定数据之后在进行分页。
举个例子,当我按各种条件查询数据的时候我一般喜欢先将数据全部查出来,在在内存里按条件过滤。那么这时候就不能先分页了,而是按条件过滤后在分页。
没办法,这时候只能手动分页了。看下面的示例:

public PageBean searchHomeParam(Integer pageSize, Integer pageNum, SearchHomeParam param){

    List homeModelList = homeMapper.findList();

    // 过滤数据
    homeModelList = filterData(homeModelList, param);

    //手动分页
    PageBean deviceVoPageInfo = new PageBean<>(homeModelList, pageSize, pageNum);

    return deviceVoPageInfo;
}

homeModelList是你查出来的数据列表,pageSize是第几页,pageNum是每页条数。
PageBean类如下:

import com.github.pagehelper.Page;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author zyl
 * 手动分页
 * @date 2020/10/29
 */
public class PageBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private int pageNo;
    private int pageSize;
    private int size;
    private int startRow;
    private int endRow;
    private long totalCount;
    private long total;
    private int totalPage;
    private List data;
    private int prePage;
    private int nextPage;
    private boolean isFirstPage;
    private boolean isLastPage;
    private boolean hasPreviousPage;
    private boolean hasNextPage;
    private int navigatePages;
    private int[] navigatePageNos;
    private int navigateFirstPage;
    private int navigateLastPage;

    public PageBean() {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
    }

    public PageBean(List list) {
        this(list, 8);
    }

    public PageBean(List list, Integer pageSize, Integer pageNo) {
        this(list, 8, pageSize, pageNo);
    }

    public PageBean(List list, int navigatePages) {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
        if (list instanceof Page) {
            Page page = (Page)list;
            this.pageNo = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.totalPage = page.getPages();
            this.data = page;
            this.size = page.size();
            this.totalCount = page.getTotal();
            if (this.size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
                this.startRow = page.getStartRow() + 1;
                this.endRow = this.startRow - 1 + this.size;
            }
        } else if (list instanceof Collection) {
            this.pageNo = 1;
            this.pageSize = list.size();
            this.totalPage = this.pageSize > 0 ? 1 : 0;
            this.data = list;
            this.size = list.size();
            this.totalCount = (long)list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }

        if (list instanceof Collection) {
            this.navigatePages = navigatePages;
            this.calcNavigatePageNos();
            this.calcPage();
            this.judgePageBoudary();
        }

    }

    //手动分页
    public PageBean(List list, int navigatePages, Integer pageSize, Integer pageNo) {
        if(list.size() == 0) getPageBeanNoData(pageSize, pageNo);
        else {
            this.isFirstPage = false;
            this.isLastPage = false;
            this.hasPreviousPage = false;
            this.hasNextPage = false;
            this.pageNo = pageNo;
            this.pageSize = pageSize;
            this.totalPage = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
            this.data = getData(this.totalPage, pageSize, pageNo, list);
            this.size = pageSize;
            this.total = (long) list.size();
            this.startRow = 1;
            this.endRow = pageSize;

            if (list instanceof Collection) {
                this.navigatePages = navigatePages;
                this.calcNavigatePageNos();
                this.calcPage();
                this.judgePageBoudary();
            }
        }
    }

    private List getData(Integer totalPage, Integer pageSize, Integer pageNo, List list){
        int size = list.size();
        if(pageNo == 1 && size < pageSize) {
            return list.subList(0, size);
        }else if(pageNo == 1 && size >= pageSize){
            return list.subList(0, pageSize);
        }else if(pageNo > 1 && pageNo < totalPage){
            return list.subList((pageNo - 1) * pageSize, (pageSize - 1) + (pageNo - 1) * pageSize + 1);
        }else{
            return list.subList((pageNo - 1) * pageSize, size);
        }
    }

    private void getPageBeanNoData(Integer pageSize, Integer pageNo){
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalPage = 0;
        this.data = Lists.newArrayListWithCapacity(0);
        this.size = 0;
        this.totalCount = 0;
        this.startRow = 0;
        this.endRow = 0;
    }

    private void calcNavigatePageNos() {
        int i;
        if (this.totalPage <= this.navigatePages) {
            this.navigatePageNos = new int[this.totalPage];

            for(i = 0; i < this.totalPage; ++i) {
                this.navigatePageNos[i] = i + 1;
            }
        } else {
            this.navigatePageNos = new int[this.navigatePages];
            i = this.pageNo - this.navigatePages / 2;
            int endNum = this.pageNo + this.navigatePages / 2;
            int j;
            if (i < 1) {
                j = 1;

                for(i = 0; i < this.navigatePages; ++i) {
                    this.navigatePageNos[i] = i++;
                }
            } else if (endNum > this.totalPage) {
                endNum = this.totalPage;

                for(i = this.navigatePages - 1; i >= 0; --i) {
                    this.navigatePageNos[i] = endNum--;
                }
            } else {
                for(i = 0; i < this.navigatePages; ++i) {
                    this.navigatePageNos[i] = i++;
                }
            }
        }

    }

    private void calcPage() {
        if (this.navigatePageNos != null && this.navigatePageNos.length > 0) {
            this.navigateFirstPage = this.navigatePageNos[0];
            this.navigateLastPage = this.navigatePageNos[this.navigatePageNos.length - 1];
            if (this.pageNo > 1) {
                this.prePage = this.pageNo - 1;
            }

            if (this.pageNo < this.totalPage) {
                this.nextPage = this.pageNo + 1;
            }
        }

    }

    private void judgePageBoudary() {
        this.isFirstPage = this.pageNo == 1;
        this.isLastPage = this.pageNo == this.totalPage || this.totalPage == 0;
        this.hasPreviousPage = this.pageNo > 1;
        this.hasNextPage = this.pageNo < this.totalPage;
    }

    public int getPageNo() {
        return this.pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return this.pageSize;
    }

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

    public int getSize() {
        return this.size;
    }

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

    public int getStartRow() {
        return this.startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public int getEndRow() {
        return this.endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return this.totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List getData() {
        return this.data;
    }

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

    /** @deprecated */
    @Deprecated
    public int getFirstPage() {
        return this.navigateFirstPage;
    }

    /** @deprecated */
    @Deprecated
    public void setFirstPage(int firstPage) {
        this.navigateFirstPage = firstPage;
    }

    public int getPrePage() {
        return this.prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public int getNextPage() {
        return this.nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    /** @deprecated */
    @Deprecated
    public int getLastPage() {
        return this.navigateLastPage;
    }

    /** @deprecated */
    @Deprecated
    public void setLastPage(int lastPage) {
        this.navigateLastPage = lastPage;
    }

    public boolean isIsFirstPage() {
        return this.isFirstPage;
    }

    public void setIsFirstPage(boolean isFirstPage) {
        this.isFirstPage = isFirstPage;
    }

    public boolean isIsLastPage() {
        return this.isLastPage;
    }

    public void setIsLastPage(boolean isLastPage) {
        this.isLastPage = isLastPage;
    }

    public boolean isHasPreviousPage() {
        return this.hasPreviousPage;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public boolean isHasNextPage() {
        return this.hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public int getNavigatePages() {
        return this.navigatePages;
    }

    public void setNavigatePages(int navigatePages) {
        this.navigatePages = navigatePages;
    }

    public int[] getNavigatePageNos() {
        return this.navigatePageNos;
    }

    public void setNavigatePageNos(int[] navigatePageNos) {
        this.navigatePageNos = navigatePageNos;
    }

    public int getNavigateFirstPage() {
        return this.navigateFirstPage;
    }

    public int getNavigateLastPage() {
        return this.navigateLastPage;
    }

    public void setNavigateFirstPage(int navigateFirstPage) {
        this.navigateFirstPage = navigateFirstPage;
    }

    public void setNavigateLastPage(int navigateLastPage) {
        this.navigateLastPage = navigateLastPage;
    }

    public long getTotal() {
        return total;
    }

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

    public String toString() {
        StringBuffer sb = new StringBuffer("PageBean{");
        sb.append("PageNo=").append(this.pageNo);
        sb.append(", pageSize=").append(this.pageSize);
        sb.append(", size=").append(this.size);
        sb.append(", startRow=").append(this.startRow);
        sb.append(", endRow=").append(this.endRow);
        sb.append(", total=").append(this.totalCount);
        sb.append(", pages=").append(this.totalPage);
        sb.append(", list=").append(this.data);
        sb.append(", prePage=").append(this.prePage);
        sb.append(", nextPage=").append(this.nextPage);
        sb.append(", isFirstPage=").append(this.isFirstPage);
        sb.append(", isLastPage=").append(this.isLastPage);
        sb.append(", hasPreviousPage=").append(this.hasPreviousPage);
        sb.append(", hasNextPage=").append(this.hasNextPage);
        sb.append(", navigatePages=").append(this.navigatePages);
        sb.append(", navigateFirstPage=").append(this.navigateFirstPage);
        sb.append(", navigateLastPage=").append(this.navigateLastPage);
        sb.append(", navigatePageNos=");
        if (this.navigatePageNos == null) {
            sb.append("null");
        } else {
            sb.append('[');

            for(int i = 0; i < this.navigatePageNos.length; ++i) {
                sb.append(i == 0 ? "" : ", ").append(this.navigatePageNos[i]);
            }

            sb.append(']');
        }

        sb.append('}');
        return sb.toString();
    }
}

你可能感兴趣的:(mybatis分页,mybatis分页插件,pagehelper分页,分页插件)