java 常用后端分页类

import lombok.Data;

import java.io.Serializable;

@Data
public class Page  implements Serializable {

  private static final long serialVersionUID = -3989189348324121L;

  /**
     * 默认页码
     */
  public static final int DEFAULT_PAGE_NO = 1;

  /**
     * 默认分页条数
     */
  public static final int DEFAULT_PAGE_SIZE = 20;

  /**
     * 分页最大条数
     */
  public static final int MAX_PAGE_SIZE = 500;

  /**
     * 是否需要分页
     */
  private boolean needPagination = true;

  private int pageIndex = DEFAULT_PAGE_NO;

  private int pageSize = DEFAULT_PAGE_SIZE;

  public int getPageIndex() {
    // 当前页码小于 1 的话就返回 1
    return Math.max(this.pageIndex, 1);
  }

  public int getPageSize() {
    // 分页条数小于 1 返回默认分页条数
    if (this.pageSize < 1) {
      pageSize = DEFAULT_PAGE_SIZE;
    }
    // 分页条数大于 500 返回最大条数 500
    if (this.pageSize > 500) {
      pageSize = MAX_PAGE_SIZE;
    }
    return this.pageSize;
  }

  // 获取偏移量
  public int getOffset() {
    return (this.getPageIndex() - DEFAULT_PAGE_NO) * this.getPageSize();
  }

  public boolean isNeedPagination() {
    return this.needPagination;
  }

  public void setPageIndex(int pageIndex) {
    this.pageIndex = pageIndex;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public void setNeedPagination(boolean needPagination) {
    this.needPagination = needPagination;
  }

}


你可能感兴趣的:(代码,Util,java)