PageInfo
項目中我們會經常用到分頁的一些邏輯代碼
下面給大家分享一個非常簡單又使用的
分頁内容 封裝對象 PageInfo
因為具體的業務代碼中,隻需要 得到一個這樣的JSON
"pageInfo": {
"firstPageNo": ,
"lastPageNo": ,
"nextPageNo": ,
"pageCount": ,
"pageNo": ,
"prePageNo": ,
"totalRecord":
}
是以部分不需要參與 toJSONString的字段
加上了 @JSONField(serialize=false) 的注解
大家可以根據自己的業務邏輯 來 修改下代碼哦
好處
當你count 完了資料庫條數以後
你隻需要調用這個構造方法
public PageInfo(int totalRecord, int pageNo, int limit) {
init(totalRecord, pageNo, limit);
}
接下來,下面的這個JSON 就已經 内部給你構造好了
"pageInfo": {
"firstPageNo": ,
"lastPageNo": ,
"nextPageNo": ,
"pageCount": ,
"pageNo": ,
"prePageNo": ,
"totalRecord":
}
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 = ;
@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 = ;
@JSONField(serialize=false)
private int offset = ;
private int pageCount;
public int getOffset() {
return this.offset;
}
public void init(int totalRecord, int pageNo, int limit) {
setTotalRecord(totalRecord);
if (limit > ) {
setLimit(limit);
} else {
setLimit();
limit = ;
}
if (totalRecord % limit == ) {
if (totalRecord == ) {
setLastPageNo();
} else {
setLastPageNo(totalRecord / limit);
}
} else {
setLastPageNo(totalRecord / limit + );
}
int pageNum = pageNo > this.lastPageNo ? this.lastPageNo : pageNo;
pageNum = pageNum <= ? : pageNum;
setPageNo(pageNum);
if (pageNum > ) {
setFirstPageNo(pageNum - );
} else {
setFirstPageNo();
}
if (getLastPageNo() > pageNum) {
setNextPageNo(pageNum + );
} else {
setNextPageNo(pageNum);
}
if (totalRecord == ) {
this.offset = ;
} else {
this.offset = ((getPageNo() - ) * 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 > ) {
this.offset = ((pageNo - ) * limit);
}
}
public PageInfo(int totalRecord, int pageNo, int limit) {
init(totalRecord, pageNo, limit);
}
public PageInfo(String pageNoStr, String limitStr, int totalRecord) {
int pageNotemp = ;
try {
if (StringUtils.isNotBlank(pageNoStr)) {
pageNotemp = Integer.parseInt(pageNoStr);
}
} catch (NumberFormatException e) {
this.logger.error("pageNoStr轉換整形出錯:" + e.getMessage(), e);
}
int limitSize = ;
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 - ;
page = page > ? page : ;
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 != ;
}
@JSONField(serialize=false)
public List<Integer> getPageNumList() {
List<Integer> pageNoList = new ArrayList();
if ((hasPrePage()) && (hasNextPage()) && (getPageNo() > )) {
assignPageNo(getPageNo() - , pageNoList);
} else if (!hasPrePage()) {
assignPageNo(getPageNo(), pageNoList);
} else if (!hasNextPage()) {
assignPageNo(getPageNo() - , pageNoList);
}
return pageNoList;
}
private void assignPageNo(int start, List<Integer> pageNoList) {
int startPageNo = start;
int loopCount = getLastPageNo() > ? : getLastPageNo();
startPageNo = startPageNo > ? startPageNo : ;
for (int i = ; i < loopCount; i++) {
pageNoList.add(Integer.valueOf(startPageNo));
startPageNo += ;
}
}
public int getPageCount() {
return this.pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
}
best regards
本人自己寫的,如果大家在使用的過程中有什麼BUG
請一定要來下面留言吐槽哦
我會繼續努力