通用分頁(yè)解決方案:簡(jiǎn)化項(xiàng)目開(kāi)發(fā)中的分頁(yè)難題
前言
在實(shí)際項(xiàng)目開(kāi)發(fā)過(guò)程中,分頁(yè)功能的使用頻率極高,尤其是針對(duì)不同數(shù)據(jù)表進(jìn)行分頁(yè)操作,往往是一件頗為繁瑣的事情。不同的數(shù)據(jù)表結(jié)構(gòu)、業(yè)務(wù)需求以及數(shù)據(jù)量大小,都可能導(dǎo)致分頁(yè)實(shí)現(xiàn)方式的差異,這無(wú)疑增加了開(kāi)發(fā)的復(fù)雜性和工作量。
本文將分享一個(gè)通用分頁(yè)解決方案,它能夠?qū)θ我鈹?shù)據(jù)表實(shí)現(xiàn)分頁(yè),使用方法簡(jiǎn)單便捷,有效提升開(kāi)發(fā)效率。
實(shí)現(xiàn)
/**
* 通用分頁(yè)工具類(lèi)
*
* @param <T>
*/
@Data
public class PageUtils<T> implements Serializable {
/**
* 當(dāng)前頁(yè)碼
*/
private int currentPage;
/**
* 每頁(yè)大小
*/
private int pageSize;
/**
* 總數(shù)據(jù)條數(shù)
*/
private int totalNum;
/**
* 首頁(yè)
*/
private int first = 1;
/**
* 尾頁(yè)
*/
private int last;
/**
* 總頁(yè)數(shù)
*/
private int totalPage;
/**
* 上一頁(yè)
*/
private int prev;
/**
* 下一頁(yè)
*/
private int next;
/**
* 頁(yè)面序號(hào)顯示的起始位置
*/
private int startNum;
/**
* 頁(yè)碼顯示控制-開(kāi)始頁(yè)碼
*/
private int start;
/**
* 頁(yè)碼顯示控制-結(jié)束頁(yè)碼
*/
private int end;
/**
* 顯示頁(yè)碼控制-總顯示頁(yè)碼(防止頁(yè)碼過(guò)多,頁(yè)面顯示擁擠問(wèn)題)
*/
private int count = 10;
/**
* 數(shù)據(jù)
*/
private List<T> list = new ArrayList<>();
/**
* 在構(gòu)造器中根據(jù)指定的參數(shù),計(jì)算其他所有屬性的屬性值
*
* @param currentPage
* @param pageSize
* @param totalNum
*/
public PageUtils(int currentPage, int pageSize, int totalNum) {
this.currentPage = currentPage;
//賦值每天顯示的記錄條數(shù)
this.pageSize = pageSize;
//賦值總記錄數(shù)(總數(shù)據(jù)條數(shù))
this.totalNum = totalNum;
//計(jì)算獲得總頁(yè)數(shù)以及尾頁(yè)
this.totalPage = this.last = (int) Math.ceil((double) totalNum / pageSize);
//防止當(dāng)前頁(yè)小于1
this.currentPage = Math.max(this.currentPage, 1);
//防止當(dāng)前頁(yè)超過(guò)總頁(yè)數(shù)
this.currentPage = Math.min(this.totalPage, this.currentPage);
//設(shè)置上一頁(yè):上一頁(yè)不能小于1
this.prev = Math.max(this.currentPage - 1, 1);
//設(shè)置下一頁(yè):下一頁(yè)不能超過(guò)總頁(yè)數(shù)
this.next = Math.min(this.currentPage + 1, this.totalPage);
//計(jì)算獲取數(shù)據(jù)顯示的序號(hào)位置
this.startNum = (this.currentPage - 1) * pageSize;
//計(jì)算顯示頁(yè)碼的起始位置:起始位置不能小于1
this.start = Math.max(this.currentPage - this.count / 2, 1);
//計(jì)算顯示頁(yè)碼的結(jié)束位置:結(jié)束位置不能超過(guò)總頁(yè)數(shù)
this.end = Math.min(this.start + this.count, this.totalPage);
}
}
具體使用:
假設(shè)這是數(shù)據(jù)訪問(wèn)對(duì)象類(lèi),負(fù)責(zé)與數(shù)據(jù)庫(kù)交互
public class DemoDAO {
// 模擬獲取總數(shù)據(jù)條數(shù)的方法,實(shí)際中會(huì)執(zhí)行數(shù)據(jù)庫(kù)查詢
public int totalNum() {
// 這里簡(jiǎn)單返回一個(gè)固定值模擬數(shù)據(jù),實(shí)際應(yīng)從數(shù)據(jù)庫(kù)查詢
return 100;
}
// 模擬根據(jù)頁(yè)碼和每頁(yè)大小查詢數(shù)據(jù)的方法,實(shí)際中會(huì)執(zhí)行數(shù)據(jù)庫(kù)查詢
public List<Demo> findByPage(int currentPage, int pageSize) {
List<Demo> resultList = new ArrayList<>();
// 計(jì)算數(shù)據(jù)起始位置
int startIndex = (currentPage - 1) * pageSize;
// 簡(jiǎn)單模擬數(shù)據(jù)生成,實(shí)際應(yīng)從數(shù)據(jù)庫(kù)查詢
for (int i = startIndex; i < startIndex + pageSize && i < 100; i++) {
resultList.add(new Demo(i, "Demo_" + i));
}
return resultList;
}
}
業(yè)務(wù)服務(wù)類(lèi),調(diào)用DAO
類(lèi)并使用分頁(yè)工具類(lèi)進(jìn)行分頁(yè)處理
public class DemoService {
public PageUtils query(int currentPage, int pageSize) {
DemoDAO dao = new DemoDAO();
//查總數(shù)據(jù)條數(shù)
int totalNum = dao.totalNum();
//根據(jù)提供的參數(shù)構(gòu)建一個(gè)PageUtils對(duì)象
PageUtils<Demo> pu = new PageUtils<>(currentPage, pageSize, totalNum);
//查當(dāng)前頁(yè)數(shù)據(jù)
List<Demo> list = dao.findByPage(pu.getCurrentPage(), pu.getPageSize());
//將查詢到的指定頁(yè)碼的數(shù)據(jù)存儲(chǔ)到分頁(yè)工具對(duì)象中
pu.setList(list);
//將分頁(yè)工具對(duì)象返回
return pu;
}
}
測(cè)試: 1-10
圖片
3-10
圖片