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 kangxu 108 * 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 @Override 169 public String toString() { 170 return "Pager [currentPage=" + currentPage + ", pageSize=" + pageSize 171 + ", pageTotal=" + pageTotal + ", recordTotal=" + recordTotal 172 + ", previousPage=" + previousPage + ", nextPage=" + nextPage 173 + ", firstPage=" + firstPage + ", lastPage=" + lastPage 174 + ", 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 kangxu 14 * 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 }