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