低版本mybatis不能用PageHeper插件的时候用这个分页

package com.yh.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 分页类
 * @Author: 张颖辉(yh)
 * @CreateDate: 2018/5/7 19:00
 * @UpdateUser: 张颖辉(yh)
 * @UpdateDate: 2018/5/7 19:00
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
public class PageInfo<T> {
    /*数据*/
    private List<T> list;
    /*总条数*/
    private Long count;
    /*总页数*/
    private Integer pages;
    /*当前页码*/
    private Integer currPage;
    /*每页条数*/
    private Integer pageSize;
    /*开始行*/
    private Long startRow;
    /*显示排列的页*/
    private List<Integer> showNos;
    /*是否有上一页*/
    private Boolean hasPrevious;
    /*是否有下一页*/
    private Boolean hasNext;

    public PageInfo(Integer currPage, Integer pageSize, Long count) {
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.count = count;
        this.update();
        if (currPage > this.pages) {
            setCurrPage(this.pages);
        }
    }

    public List<T> getList() {
        return list;
    }

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

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
        this.update();
    }

    public Integer getPages() {
        return pages;
    }


    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        currPage = currPage < 1 ? 1 : currPage;
        this.currPage = currPage;
        this.update();
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize < 1) {
            throw new RuntimeException("pageSize不能小于1");
        }
        this.pageSize = pageSize;
        this.update();
    }

    public Long getStartRow() {
        return startRow;
    }

    public List<Integer> getShowNos() {
        return showNos;
    }

    public Boolean getHasPrevious() {
        this.hasPrevious=this.currPage > 1;
        return hasPrevious;
    }

    public Boolean getHasNext() {
        this.hasNext=this.currPage < this.pages;
        return hasNext;
    }


    /**
     * @Description: 更新数据:总页数,和数据开始行数
     * @Author: 张颖辉(yh)
     * @Date: 2018/5/8 10:42
     * @param: []
     * @return: void
     * @Version: 1.0
     */
    private void update() {
        if (count != null) {
            if (pageSize != null && pageSize != 0) {
                if (count % pageSize != 0) {
                    this.pages = (int) (count / pageSize) + 1;
                } else {
                    this.pages = (int) (count / pageSize);
                }
            }
        }
        if (currPage != null) {
            if (pageSize != null) {
                this.startRow = (currPage - 1) * pageSize.longValue();
            }
            /*显示排列页*/
            if (pages != null) {
                showNos = new ArrayList<>();
                for (int i = (currPage - 2 > 0 ? currPage - 2 : 1); i <= (currPage + 2 > pages ? pages : currPage + 2); i++) {
                    showNos.add(i);
                }
            }
        }
    }
}

使用方式:

服务端关键代码:

        Long count=auditLogMapper.getCountByCondition(userId, userName, startTimeStr, endTimeStr);;
        PageInfo<AuditLog> pageInfo=new PageInfo(currPage,  pageSize, count);
        List<AuditLog> list = auditLogMapper.getAuditsByCondition(userId, userName, startTimeStr, endTimeStr, pageInfo.getStartRow(), pageInfo.getPageSize());
        pageInfo.setList(list);
        return pageInfo;

猜你喜欢

转载自my.oschina.net/iyinghui/blog/1809440