1. 程式人生 > >非常好用的分頁物件PageInfo

非常好用的分頁物件PageInfo

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
請一定要來下面留言吐槽哦
我會繼續努力