非常好用的分页对象PageInfo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31922571/article/details/77064575

PageInfo

项目中我们会经常用到分页的一些逻辑代码
下面给大家分享一个非常简单又使用的
分页内容 封装对象 PageInfo


因为具体的业务代码中,只需要 得到一个这样的JSON

"pageInfo": {
            "firstPageNo": 1,
            "lastPageNo": 4,
            "nextPageNo": 2,
            "pageCount": 5,
            "pageNo": 1,
            "prePageNo": 1,
            "totalRecord": 20
        }

所以部分不需要参与 toJSONString的字段
加上了 @JSONField(serialize=false) 的注解
大家可以根据自己的业务逻辑 来 修改下代码哦

好处

当你count 完了数据库条数以后
你只需要调用这个构造方法

public PageInfo(int totalRecord, int pageNo, int limit) {
        init(totalRecord, pageNo, limit);
    }

接下来,下面的这个JSON 就已经 内部给你构造好了

"pageInfo": {
            "firstPageNo": 1,
            "lastPageNo": 4,
            "nextPageNo": 2,
            "pageCount": 5,
            "pageNo": 1,
            "prePageNo": 1,
            "totalRecord": 20
        }
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.annotation.JSONField;

public class PageInfo {
    private Log logger = LogFactory.getLog(getClass());
    public static final int DEFAULT_LIMIT = 20;
    @SuppressWarnings("rawtypes")
    @JSONField(serialize=false)
    private List<?> dataList = new ArrayList();
    private int totalRecord;
    private int firstPageNo;
    private int nextPageNo;
    private int lastPageNo;
    private int pageNo;
    @JSONField(serialize=false)
    private int limit = 20;
    @JSONField(serialize=false)
    private int offset = 0;
    private int pageCount;

    public int getOffset() {
        return this.offset;
    }

    public void init(int totalRecord, int pageNo, int limit) {
        setTotalRecord(totalRecord);
        if (limit > 0) {
            setLimit(limit);
        } else {
            setLimit(20);
            limit = 20;
        }
        if (totalRecord % limit == 0) {
            if (totalRecord == 0) {
                setLastPageNo(1);
            } else {
                setLastPageNo(totalRecord / limit);
            }
        } else {
            setLastPageNo(totalRecord / limit + 1);
        }
        int pageNum = pageNo > this.lastPageNo ? this.lastPageNo : pageNo;
        pageNum = pageNum <= 0 ? 1 : pageNum;
        setPageNo(pageNum);
        if (pageNum > 1) {
            setFirstPageNo(pageNum - 1);
        } else {
            setFirstPageNo(1);
        }
        if (getLastPageNo() > pageNum) {
            setNextPageNo(pageNum + 1);
        } else {
            setNextPageNo(pageNum);
        }
        if (totalRecord == 0) {
            this.offset = 0;
        } else {
            this.offset = ((getPageNo() - 1) * limit);
        }
    }

    public PageInfo(int totalRecord, int pageNo, int limit, List<?> dataList) {
        setDataList(dataList);
        init(totalRecord, pageNo, limit);
    }

    public PageInfo(int pageNo, int limit) {
        this.pageNo = pageNo;
        this.limit = limit;
        if (pageNo > 0) {
            this.offset = ((pageNo - 1) * limit);
        }
    }

    public PageInfo(int totalRecord, int pageNo, int limit) {
        init(totalRecord, pageNo, limit);
    }

    public PageInfo(String pageNoStr, String limitStr, int totalRecord) {
        int pageNotemp = 1;
        try {
            if (StringUtils.isNotBlank(pageNoStr)) {
                pageNotemp = Integer.parseInt(pageNoStr);
            }
        } catch (NumberFormatException e) {
            this.logger.error("pageNoStr转换整形出错:" + e.getMessage(), e);
        }
        int limitSize = 20;
        try {
            if (StringUtils.isNotBlank(limitStr)) {
                limitSize = Integer.parseInt(limitStr);
            }
        } catch (NumberFormatException e) {
            this.logger.error("limitStr转换整形出错:" + e.getMessage(), e);
        }
        this.dataList = Collections.emptyList();

        init(totalRecord, pageNotemp, limitSize);
    }

    public PageInfo(int pageNo, int limit, List<?> totalData) {
        int totalSize = totalData.size();
        init(totalSize, pageNo, limit);

        int begin = getOffset();
        int end = begin + limit > totalSize ? totalSize : begin + limit;
        setDataList(totalData.subList(begin, end));
    }

    public List<?> getDataList() {
        return this.dataList;
    }

    public void setDataList(List<?> dataList) {
        this.dataList = dataList;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalRecord() {
        return this.totalRecord;
    }

    public int getFirstPageNo() {
        return this.firstPageNo;
    }

    public void setFirstPageNo(int firstPageNo) {
        this.firstPageNo = firstPageNo;
    }

    public int getNextPageNo() {
        return this.nextPageNo < this.lastPageNo ? this.nextPageNo : this.lastPageNo;
    }

    public int getPrePageNo() {
        int page = this.pageNo - 1;
        page = page > 0 ? page : 1;
        return page;
    }

    public void setNextPageNo(int nextPageNo) {
        this.nextPageNo = nextPageNo;
    }

    public int getLastPageNo() {
        return this.lastPageNo;
    }

    public void setLastPageNo(int lastPageNo) {
        this.lastPageNo = lastPageNo;
    }

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

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

    public int getLimit() {
        return this.limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public boolean hasNextPage() {
        return this.pageNo != this.lastPageNo;
    }

    public boolean hasPrePage() {
        return this.pageNo != 1;
    }

    @JSONField(serialize=false)
    public List<Integer> getPageNumList() {
        List<Integer> pageNoList = new ArrayList();
        if ((hasPrePage()) && (hasNextPage()) && (getPageNo() > 1)) {
            assignPageNo(getPageNo() - 1, pageNoList);
        } else if (!hasPrePage()) {
            assignPageNo(getPageNo(), pageNoList);
        } else if (!hasNextPage()) {
            assignPageNo(getPageNo() - 2, pageNoList);
        }
        return pageNoList;
    }

    private void assignPageNo(int start, List<Integer> pageNoList) {
        int startPageNo = start;
        int loopCount = getLastPageNo() > 3 ? 3 : getLastPageNo();
        startPageNo = startPageNo > 0 ? startPageNo : 1;
        for (int i = 0; i < loopCount; i++) {
            pageNoList.add(Integer.valueOf(startPageNo));
            startPageNo += 1;
        }
    }

    public int getPageCount() {
        return this.pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
}

best regards

本人自己写的,如果大家在使用的过程中有什么BUG
请一定要来下面留言吐槽哦
我会继续努力

猜你喜欢

转载自blog.csdn.net/qq_31922571/article/details/77064575