Java介面分頁模版
阿新 • • 發佈:2018-11-06
Java介面分頁模版
- 程式碼如下
import java.io.Serializable;
import java.util.List;
/**
* 翻頁模板工具類
*
*/
public final class PageModel<T> implements Serializable {
private static final long serialVersionUID = 1L;
private int currentPage;// 當前第幾頁
private int pageSize;// 每頁顯示條數
private int totalPage = 1;// 總頁數
private int totalRecord;// 總記錄數
private List<T> dataList;// 分頁資料
public PageModel() {
}
/*
* 初始化PageModel例項
*/
public PageModel(final int pageSize, final int currentPage, final int totalRecord, List<T> dataList) {
// 初始化每頁顯示條數
this .pageSize = pageSize;
// 設定總記錄數
this.totalRecord = totalRecord;
// 初始化總頁數
this.dataList = dataList;
// 初始化當前頁
this.currentPage = currentPage;
this.totalPage = this.totalRecord == 0 ? 1 : (this.totalRecord + this.pageSize - 1) / this.pageSize;
}
/*
* 外界獲得PageModel例項
*/
@SuppressWarnings("unchecked")
public static PageModel<?> newPageModel(final int pageSize, final int currentPage, final int totalRecord, List<?> dataList) {
return new PageModel(pageSize, currentPage, totalRecord, dataList);
}
// 設定當前請求頁
private void setCurrentPage(int currentPage) {
if (currentPage < 1) {
this.currentPage = 1;
}
if (currentPage > totalPage) {
this.currentPage = totalPage;
}
}
public int getCurrentPage() {
return currentPage;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
/*
* 獲得開始行數
*/
public int getStartRow() {
return (currentPage - 1) * pageSize;
}
/*
* 獲得結束行
*/
public int getEndRow() {
return currentPage * pageSize;
}
/*
* 獲得翻頁資料
*/
public List<T> getDataList() {
return dataList;
}
/*
* 設定翻頁資料
*/
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
// 首頁
public int getFirst() {
return 1;
}
// 上一頁
public int getPrevious() {
return currentPage - 1;
}
// 下一頁
public int getNext() {
return currentPage + 1;
}
// 尾頁
public int getLast() {
return totalPage;
}
}