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