springboot项目:mybatis使用pagehelper分页插件

1.引入依赖



   com.github.pagehelper
   pagehelper
   5.0.3

 

2.在application.yml文件中配置:

#pagehelper
pagehelper:
    helperDialect: oracle
    #false页数为负数或者超出最大页数时为空  true则是最后一页数据
    reasonable: false
    #支持通过Mapper接口参数来传递分页参数
    supportMethodsArguments: true
    params: count=countSql
    #always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page
    returnPageInfo: check
 

3.在service中的分页方法上加上:

PageHelper.startPage(pageNum,pageSize);pageNum:当前页数 pageSize:当前页需要显示的数量

例如:

public PageBean find(int currentPage, int pageSize, Operation operation) {
    /**
     * 配置分页
     * @Param currentPage 当前页数  pageSize 每页显示数量  totalNum 总页数
     */
    PageBean operationPageBean = new PageBean<>(currentPage,pageSize,opLogMapper.getCount(operation));
    PageHelper.startPage(currentPage,pageSize);
    List operations =  opLogMapper.find(operation);
    for (Operation op : operations){
        //模块枚举
        Module module =  Module.valueOf(op.getOperationModule());
        op.setOperationModuleName(module.getModuleName());
        //请求类型枚举
        Type type = Type.valueOf(op.getReqestType());
        op.setRequestTypeName(type.getTypeName());
    }
    operationPageBean.setItems(operations);
    return operationPageBean;
}

附上pageBean:

package com.nikoyo.nmsp.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

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

/**
 * 分页查询实体类
 */
@ApiModel(value = "PageBean", description = "分页结果集")
public class PageBean implements Serializable {
    /** 当前页码 */
    @ApiModelProperty(value = "当前页码")
    private Integer currentPage;
    /** 每页显示的总条数 */
    @ApiModelProperty(value = "每页显示的总条数")
    private Integer pageSize;
    /** 总条数 */
    @ApiModelProperty(value = "总条数")
    private Integer totalNum;
    /** 总页数 */
    @ApiModelProperty(value = "总页数")
    private Integer totalPage;
    /** 开始索引 */
    @ApiModelProperty(value = "开始索引")
    private Integer startIndex;
    /** 结束索引 */
    @ApiModelProperty(value = "结束索引")
    private Integer endIndex;
    /** 是否有上一页 */
    @ApiModelProperty(value = "是否有上一页")
    private boolean hasPrevious = false;
    /** 是否有下一页 */
    @ApiModelProperty(value = "是否有下一页")
    private boolean hasNext = false;
    /** 首页 */
    @ApiModelProperty(value = "首页")
    private Integer firstPageNum;
    /** 上一页 */
    @ApiModelProperty(value = "上一页")
    private Integer priviousPageNum;
    /** 下一页 */
    @ApiModelProperty(value = "下一页")
    private Integer nextPageNum;
    /** 尾页 */
    @ApiModelProperty(value = "尾页")
    private Integer endPageNum;
    /** 查询结果 */
    @ApiModelProperty(value = "查询结果")
    private List items;



    public PageBean() {
    }

    public PageBean(Integer currentPage,Integer pageSize){
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
        //每页显示的记录数
        this.pageSize = pageSize == null ? 10 : pageSize;
        //当前页码数
        this.currentPage = currentPage == null ? 1 : currentPage;
        //总记录数
        this.totalNum = totalNum == null ? 0 : totalNum;
        //总页码数
        this.totalPage = (int) Math.ceil(this.totalNum * 1.0 / this.pageSize);
        //判断当前页码数是否大于总页码数
        if (this.currentPage > this.totalPage && this.totalPage >= 1) {
            this.currentPage = this.totalPage;
        }
        //查询的时候从多少记录开始
        this.startIndex = (this.currentPage - 1) * this.pageSize;
        //查询的时候 实际结束索引
        this.endIndex =  this.startIndex+this.pageSize-1;

        //是否有上一页
        if (this.currentPage > 1) {
            this.hasPrevious = true;
            this.firstPageNum = 1;
            this.priviousPageNum = this.currentPage - 1;
        }else if(this.currentPage == 1){
            this.hasPrevious = false;
            this.firstPageNum = 1;
            this.priviousPageNum = this.currentPage;
        }else{
            this.hasPrevious = false;
            this.firstPageNum = null;
            this.priviousPageNum = null;
        }
        //是否有下一页
        if (this.currentPage < this.totalPage) {
            this.hasNext = true;
            this.nextPageNum = this.currentPage + 1;
            this.endPageNum = this.totalPage;
        }else if(this.currentPage == this.totalPage){
            this.hasNext = false;
            this.nextPageNum = this.currentPage;
            this.endPageNum = this.currentPage;
        }else{
            this.hasNext = false;
            this.nextPageNum = null;
            this.endPageNum = null;
        }

        if(this.startIndex+1>this.totalNum){
            this.endIndex = this.totalNum - 1;
        }
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

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

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    public Integer getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(Integer endIndex) {
        this.endIndex = endIndex;
    }

    public boolean isHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public Integer getFirstPageNum() {
        return firstPageNum;
    }

    public void setFirstPageNum(Integer firstPageNum) {
        this.firstPageNum = firstPageNum;
    }

    public Integer getPriviousPageNum() {
        return priviousPageNum;
    }

    public void setPriviousPageNum(Integer priviousPageNum) {
        this.priviousPageNum = priviousPageNum;
    }

    public Integer getNextPageNum() {
        return nextPageNum;
    }

    public void setNextPageNum(Integer nextPageNum) {
        this.nextPageNum = nextPageNum;
    }

    public Integer getEndPageNum() {
        return endPageNum;
    }

    public void setEndPageNum(Integer endPageNum) {
        this.endPageNum = endPageNum;
    }

    public List getItems() {
        return items;
    }

    public void setItems(List items) {
        this.items = items;
    }

    @Override
    public String toString() {
        return "PageBean{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalNum=" + totalNum +
                ", totalPage=" + totalPage +
                ", startIndex=" + startIndex +
                ", endIndex=" + endIndex +
                ", hasPrevious=" + hasPrevious +
                ", hasNext=" + hasNext +
                ", firstPageNum=" + firstPageNum +
                ", priviousPageNum=" + priviousPageNum +
                ", nextPageNum=" + nextPageNum +
                ", endPageNum=" + endPageNum +
                ", items=" + items +
                '}';
    }
}

你可能感兴趣的:(springboot项目:mybatis使用pagehelper分页插件)