分頁查詢往往需要使用到查詢工具類
package cn.edu.nwsuaf.GYL.query;import java.util.ArrayList;import java.util.List;public class PageResult<T> { //這是一個分頁查詢相關的類,他需要具有泛型的功能 //功能 //1:頁面上顯示分頁 //2:頁面上顯示數據 // 主表的當前頁碼 PRivate int currentPage; // 子表的當前頁碼 private int currentPage_zhib; //提供了set和get方法 public int getCurrentPage_zhib() { return currentPage_zhib; } public void setCurrentPage_zhib(int currentPage_zhib) { this.currentPage_zhib = currentPage_zhib; } // 一頁顯示的條數 private int pageSize; // 總條數,查詢出來的 private int totalRows; // 總頁數:計算出來的 private int totalPages; // 當前頁的數據,當前頁的數據是不確定的,因此需要使用到泛型 private List<T> rows = new ArrayList<T>(); public PageResult() { } //通過構造函數將需要的數據傳入,當前頁,頁面的大小,以及總共的條數記錄,這個總的條數記錄是從數據庫里邊 //查詢出來的 public PageResult(int currentPage, int pageSize, int totalRows) { this.currentPage = currentPage; this.pageSize = pageSize; this.totalRows = totalRows; // currentPage和pageSize最小值必須是1,在向上翻頁的時候頁面值會減小 //并且當前頁顯示的數據也有,但是當前頁的頁面最小也只能是1,不能小于1 this.currentPage = Math.max(this.currentPage, 1); this.pageSize = Math.max(this.pageSize, 1); // 總頁數:計算出來,這是一個計算方法,有幾種同的計算方法, //比如:一頁顯示5條,總共24條,那么(24+5-1)/5=5頁,這實際就是在不能為整數 //的時候就需要多加一頁 this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize; // 錯誤處理 // 當前頁面不能大于總頁數 //再向下翻頁的時候,當前頁面的頁數大小,不能大于總的頁數 //例如:只有五頁,當你向第六頁翻的時候,就還是第五頁 this.currentPage = Math.min(this.currentPage, this.totalPages); } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalRows() { return totalRows; } public void setTotalRows(int totalRows) { this.totalRows = totalRows; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } @Override public String toString() { return "PageResult [currentPage=" + currentPage + ", pageSize=" + pageSize + ", totalRows=" + totalRows + ", totalPages=" + totalPages + ", rows=" + rows.size() + "]"; }}新聞熱點
疑難解答