Pager.java
package pers.kangxu.datautils.common;import java.io.Serializable;import java.util.List;/** * * <b> 分頁通用類 </b> * * @author kangxu * @param <T> * */public class Pager<T> implements Serializable { /** * */ private static final long serialVersionUID = 4542617637761955078L; /** * currentPage 當前頁 */ private int currentPage = 1; /** * pageSize 每頁大小 */ private int pageSize = 10; /** * pageTotal 總頁數(shù) */ private int pageTotal; /** * recordTotal 總條數(shù) */ private int recordTotal = 0; /** * previousPage 前一頁 */ private int previousPage; /** * nextPage 下一頁 */ private int nextPage; /** * firstPage 第一頁 */ private int firstPage = 1; /** * lastPage 最后一頁 */ private int lastPage; /** * content 每頁的內(nèi)容 */ private List<T> content; // 以下set方式是需要賦值的 /** * 設置當前頁 <br> * * @author kangxu * * @param currentPage */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } /** * 設置每頁大小,也可以不用賦值,默認大小為10條 <br> * * @author kangxu * * @param pageSize */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * 設置總條數(shù),默認為0 <br> * * @author kangxu * * @param recordTotal */ public void setRecordTotal(int recordTotal) { this.recordTotal = recordTotal; otherAttr(); } /** * 設置分頁內(nèi)容 <br> * * @author kangxu * * @param content */ public void setContent(List<T> content) { this.content = content; } /** * 設置其他參數(shù) * * @author kangxu * */ public void otherAttr() { // 總頁數(shù) this.pageTotal = this.recordTotal % this.pageSize > 0 ? this.recordTotal / this.pageSize + 1 : this.recordTotal / this.pageSize; // 第一頁 this.firstPage = 1; // 最后一頁 this.lastPage = this.pageTotal; // 前一頁 if (this.currentPage > 1) { this.previousPage = this.currentPage - 1; } else { this.previousPage = this.firstPage; } // 下一頁 if (this.currentPage < this.lastPage) { this.nextPage = this.currentPage + 1; } else { this.nextPage = this.lastPage; } } // 放開私有屬性 public int getCurrentPage() { return currentPage; } public int getPageSize() { return pageSize; } public int getPageTotal() { return pageTotal; } public int getRecordTotal() { return recordTotal; } public int getPreviousPage() { return previousPage; } public int getNextPage() { return nextPage; } public int getFirstPage() { return firstPage; } public int getLastPage() { return lastPage; } public List<T> getContent() { return content; } @Override public String toString() { return "Pager [currentPage=" + currentPage + ", pageSize=" + pageSize + ", pageTotal=" + pageTotal + ", recordTotal=" + recordTotal + ", previousPage=" + previousPage + ", nextPage=" + nextPage + ", firstPage=" + firstPage + ", lastPage=" + lastPage + ", content=" + content + "]"; }}使用 PagerTester.java
package pers.kangxu.datautils.utils;import java.util.ArrayList;import java.util.List;import pers.kangxu.datautils.common.Pager;/** * 分頁數(shù)據(jù)測試 * <b> * * </b> * @author kangxu * */public class PagerTester { public static void main(String[] args) { Pager<String> pager = new Pager<String>(); List<String> content = new ArrayList<String>(); content.add("str1"); content.add("str2"); content.add("str3"); content.add("str4"); content.add("str5"); content.add("str6"); content.add("str7"); content.add("str8"); content.add("str9"); content.add("str10"); pager.setCurrentPage(1); pager.setPageSize(10); pager.setRecordTotal(62); pager.setContent(content); System.out.println(pager); }}以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網(wǎng)!
新聞熱點
疑難解答