JAVA 分頁工具類及其使用,java分頁工具類
Pager.java
1 package pers.kangxu.datautils.common; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 /** 7 * 8 * <b> 分頁通用類 </b> 9 * 10 * @author kangxu 11 * @param <T> 12 * 13 */ 14 public class Pager<T> implements Serializable { 15 16 /** 17 * 18 */ 19 private static final long serialVersionUID = 4542617637761955078L; 20 21 /** 22 * currentPage 當前頁 23 */ 24 private int currentPage = 1; 25 /** 26 * pageSize 每頁大小 27 */ 28 private int pageSize = 10; 29 /** 30 * pageTotal 總頁數 31 */ 32 private int pageTotal; 33 /** 34 * recordTotal 總條數 35 */ 36 private int recordTotal = 0; 37 /** 38 * previousPage 前一頁 39 */ 40 private int previousPage; 41 /** 42 * nextPage 下一頁 43 */ 44 private int nextPage; 45 /** 46 * firstPage 第一頁 47 */ 48 private int firstPage = 1; 49 /** 50 * lastPage 最後一頁 51 */ 52 private int lastPage; 53 /** 54 * content 每頁的內容 55 */ 56 private List<T> content; 57 58 // 以下set方式是需要賦值的 59 /** 60 * 設定當前頁 <br> 61 * 62 * @author kangxu 63 * 64 * @param currentPage 65 */ 66 public void setCurrentPage(int currentPage) { 67 this.currentPage = currentPage; 68 } 69 70 /** 71 * 設定每頁大小,也可以不用賦值,預設大小為10條 <br> 72 * 73 * @author kangxu 74 * 75 * @param pageSize 76 */ 77 public void setPageSize(int pageSize) { 78 this.pageSize = pageSize; 79 } 80 81 /** 82 * 設定總條數,預設為0 <br> 83 * 84 * @author kangxu 85 * 86 * @param recordTotal 87 */ 88 public void setRecordTotal(int recordTotal) { 89 this.recordTotal = recordTotal; 90 otherAttr(); 91 } 92 93 /** 94 * 設定分頁內容 <br> 95 * 96 * @author kangxu 97 * 98 * @param content 99 */100 public void setContent(List<T> content) {101 this.content = content;102 }103 104 /**105 * 設定其他參數106 * 107 * @author kangxu108 * 109 */110 public void otherAttr() {111 // 總頁數112 this.pageTotal = this.recordTotal % this.pageSize > 0 ? this.recordTotal / this.pageSize + 1 : this.recordTotal / this.pageSize;113 // 第一頁114 this.firstPage = 1;115 // 最後一頁116 this.lastPage = this.pageTotal;117 // 前一頁118 if (this.currentPage > 1) {119 this.previousPage = this.currentPage - 1;120 } else {121 this.previousPage = this.firstPage;122 }123 // 下一頁124 if (this.currentPage < this.lastPage) {125 this.nextPage = this.currentPage + 1;126 } else {127 this.nextPage = this.lastPage;128 }129 }130 131 // 放開私人屬性132 public int getCurrentPage() {133 return currentPage;134 }135 136 public int getPageSize() {137 return pageSize;138 }139 140 public int getPageTotal() {141 return pageTotal;142 }143 144 public int getRecordTotal() {145 return recordTotal;146 }147 148 public int getPreviousPage() {149 return previousPage;150 }151 152 public int getNextPage() {153 return nextPage;154 }155 156 public int getFirstPage() {157 return firstPage;158 }159 160 public int getLastPage() {161 return lastPage;162 }163 164 public List<T> getContent() {165 return content;166 }167 168 @Override169 public String toString() {170 return "Pager [currentPage=" + currentPage + ", pageSize=" + pageSize171 + ", pageTotal=" + pageTotal + ", recordTotal=" + recordTotal172 + ", previousPage=" + previousPage + ", nextPage=" + nextPage173 + ", firstPage=" + firstPage + ", lastPage=" + lastPage174 + ", content=" + content + "]";175 }176 177 178 179 }
使用 PagerTester.java
1 package pers.kangxu.datautils.utils; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import pers.kangxu.datautils.common.Pager; 7 8 /** 9 * 分頁資料測試10 * <b>11 *12 * </b>13 * @author kangxu14 *15 */16 public class PagerTester {17 18 public static void main(String[] args) {19 20 Pager<String> pager = new Pager<String>();21 22 List<String> content = new ArrayList<String>();23 content.add("str1");24 content.add("str2");25 content.add("str3");26 content.add("str4");27 content.add("str5");28 content.add("str6");29 content.add("str7");30 content.add("str8");31 content.add("str9");32 content.add("str10");33 34 pager.setCurrentPage(1);35 pager.setPageSize(10);36 pager.setRecordTotal(62);37 pager.setContent(content);38 39 System.out.println(pager);40 41 }42 43 }