Java paging tool class and its use (sample share)

  • 2020-05-27 04:47:47
  • OfStack

Pager.java


package pers.kangxu.datautils.common;
import java.io.Serializable;
import java.util.List;
/**
 * 
 * <b>  Paging general class  </b>
 * 
 * @author kangxu
 * @param <T>
 * 
 */
public class Pager<T> implements Serializable {
 /**
 * 
 */
 private static final long serialVersionUID = 4542617637761955078L;
 /**
 * currentPage  The current page 
 */
 private int currentPage = 1;
 /**
 * pageSize  The size of each page 
 */
 private int pageSize = 10;
 /**
 * pageTotal  Total number of pages 
 */
 private int pageTotal;
 /**
 * recordTotal  The total number of article 
 */
 private int recordTotal = 0;
 /**
 * previousPage  before 1 page 
 */
 private int previousPage;
 /**
 * nextPage  Under the 1 page 
 */
 private int nextPage;
 /**
 * firstPage  The first 1 page 
 */
 private int firstPage = 1;
 /**
 * lastPage  The last 1 page 
 */
 private int lastPage;
 /**
 * content  Each page 
 */
 private List<T> content;
 //  The following set The mode is assigned 
 /**
 *  Set the current page  <br>
 * 
 * @author kangxu
 * 
 * @param currentPage
 */
 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }
 /**
 *  Set the size per page , You don't have to assign , Default size is 10 article  <br>
 * 
 * @author kangxu
 * 
 * @param pageSize
 */
 public void setPageSize(int pageSize) {
 this.pageSize = pageSize;
 }
 /**
 *  Set the total number of bars , The default is 0 <br>
 * 
 * @author kangxu
 * 
 * @param recordTotal
 */
 public void setRecordTotal(int recordTotal) {
 this.recordTotal = recordTotal;
 otherAttr();
 }
 /**
 *  Set the paging content  <br>
 * 
 * @author kangxu
 * 
 * @param content
 */
 public void setContent(List<T> content) {
 this.content = content;
 }
 /**
 *  Set other parameters 
 * 
 * @author kangxu
 * 
 */
 public void otherAttr() {
 //  Total number of pages 
 this.pageTotal = this.recordTotal % this.pageSize > 0 ? this.recordTotal / this.pageSize + 1 : this.recordTotal / this.pageSize;
 //  The first 1 page 
 this.firstPage = 1;
 //  The last 1 page 
 this.lastPage = this.pageTotal;
 //  before 1 page 
 if (this.currentPage > 1) {
 this.previousPage = this.currentPage - 1;
 } else {
 this.previousPage = this.firstPage;
 }
 //  Under the 1 page 
 if (this.currentPage < this.lastPage) {
 this.nextPage = this.currentPage + 1;
 } else {
 this.nextPage = this.lastPage;
 }
 }
 //  Let go of private properties 
 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 + "]";
 }
}

Using PagerTester java


package pers.kangxu.datautils.utils;
import java.util.ArrayList;
import java.util.List;
import pers.kangxu.datautils.common.Pager;
/**
 *  Paging data test 
 * <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);
 }
}

Related articles: