com.github.pagehelper.Page Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sqlhelper-mybatis-over-pagehelper Show documentation
Show all versions of sqlhelper-mybatis-over-pagehelper Show documentation
supports migrate from mybatis-pagehelper
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.pagehelper;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;
/**
* Mybatis - 分页对象
*
* @author liuzh/abel533/isea533
* @version 3.6.0
* 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
*/
public class Page extends ArrayList implements Closeable {
private static final long serialVersionUID = 1L;
/**
* 页码,从1开始
*/
private int pageNum;
/**
* 页面大小
*/
private int pageSize;
/**
* 起始行
*/
private long startRow;
/**
* 末行
*/
private long endRow;
/**
* 总数
*/
private long total;
/**
* 总页数
*/
private int pages;
/**
* 包含count查询
*/
private boolean count = true;
/**
* 分页合理化
*/
private Boolean reasonable;
/**
* 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
*/
private Boolean pageSizeZero;
/**
* 进行count查询的列名
*/
private String countColumn;
/**
* 排序
*/
private String orderBy;
/**
* 只增加排序
*/
private boolean orderByOnly;
public Page() {
super();
}
public Page(int pageNum, int pageSize) {
this(pageNum, pageSize, true, null);
}
public Page(int pageNum, int pageSize, boolean count) {
this(pageNum, pageSize, count, null);
}
private Page(int pageNum, int pageSize, boolean count, Boolean reasonable) {
super(0);
if (pageNum == 1 && pageSize == Integer.MAX_VALUE) {
pageSizeZero = true;
pageSize = -1;
}
setPageNum(pageNum);
setPageSize(pageSize);
this.count = count;
calculateStartAndEndRow();
setReasonable(reasonable);
}
/**
* int[] rowBounds
* 0 : offset
* 1 : limit
*/
public Page(int[] rowBounds, boolean count) {
super(0);
if (rowBounds[0] == 0 && rowBounds[1] == Integer.MAX_VALUE) {
pageSizeZero = true;
this.pageSize = -1;
} else {
this.pageSize = rowBounds[1];
this.pageNum = rowBounds[1] != 0 ? (int) (Math.ceil(((double) rowBounds[0] + rowBounds[1]) / rowBounds[1])) : 0;
}
this.startRow = rowBounds[0];
this.count = count;
this.endRow = this.startRow + rowBounds[1];
}
public List getResult() {
return this;
}
public int getPages() {
return pages;
}
public Page setPages(int pages) {
this.pages = pages;
return this;
}
public long getEndRow() {
return endRow;
}
public Page setEndRow(long endRow) {
this.endRow = endRow;
return this;
}
public int getPageNum() {
return pageNum;
}
public Page setPageNum(int pageNum) {
if (pageNum > 0) {
this.pageNum = pageNum;
}
return this;
}
public int getPageSize() {
return pageSize;
}
public Page setPageSize(int pageSize) {
this.pageSize = pageSize;
return this;
}
public long getStartRow() {
return startRow;
}
public Page setStartRow(long startRow) {
this.startRow = startRow;
return this;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
if (total == -1) {
pages = 1;
return;
}
if (pageSize > 0) {
pages = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
} else {
pages = 0;
}
//分页合理化,针对不合理的页码自动处理
if ((reasonable != null && reasonable) && pageNum > pages) {
if (pages != 0) {
pageNum = pages;
}
calculateStartAndEndRow();
}
}
public Boolean getReasonable() {
return reasonable;
}
public Page setReasonable(Boolean reasonable) {
if (reasonable == null) {
return this;
}
this.reasonable = reasonable;
//分页合理化,针对不合理的页码自动处理
if (this.reasonable && this.pageNum <= 0) {
this.pageNum = 1;
calculateStartAndEndRow();
}
return this;
}
public Boolean getPageSizeZero() {
return pageSizeZero;
}
public Page setPageSizeZero(Boolean pageSizeZero) {
if (this.pageSizeZero == null && pageSizeZero != null) {
this.pageSizeZero = pageSizeZero;
}
return this;
}
public String getOrderBy() {
return orderBy;
}
public Page setOrderBy(String orderBy) {
this.orderBy = orderBy;
PageHelper.PagingRequestAdapter requestAdapter = PageHelper.getLocalPagingRequest();
if (requestAdapter != null && requestAdapter.page == this) {
requestAdapter.setOrderBy();
}
return (Page) this;
}
public boolean isOrderByOnly() {
return orderByOnly;
}
public void setOrderByOnly(boolean orderByOnly) {
this.orderByOnly = orderByOnly;
}
/**
* 计算起止行号
*/
private void calculateStartAndEndRow() {
if (pageSize < 0) {
this.startRow = 0;
this.endRow = Integer.MAX_VALUE;
} else if (pageSize == 0) {
this.startRow = -1;
this.endRow = -1;
} else if (pageSize > 0) {
this.startRow = (pageNum - 1L) * pageSize;
this.endRow = startRow + pageSize;
}
}
public boolean isCount() {
return this.count;
}
public Page setCount(boolean count) {
this.count = count;
PageHelper.PagingRequestAdapter requestAdapter = PageHelper.getLocalPagingRequest();
if (requestAdapter != null && requestAdapter.page == this) {
requestAdapter.setCount(this.count);
}
return this;
}
/**
* 设置页码
*
* @param pageNum
* @return
*/
public Page pageNum(int pageNum) {
//分页合理化,针对不合理的页码自动处理
this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum;
PageHelper.PagingRequestAdapter requestAdapter = PageHelper.getLocalPagingRequest();
if (requestAdapter != null && requestAdapter.page == this) {
requestAdapter.setPageNo(this.pageNum);
}
return this;
}
/**
* 设置页面大小
*
* @param pageSize
* @return
*/
public Page pageSize(int pageSize) {
this.pageSize = pageSize;
calculateStartAndEndRow();
PageHelper.PagingRequestAdapter requestAdapter = PageHelper.getLocalPagingRequest();
if (requestAdapter != null && requestAdapter.page == this) {
requestAdapter.setPageSize(pageSize);
}
return this;
}
/**
* 是否执行count查询
*
* @param count
* @return
*/
public Page count(Boolean count) {
this.count = count;
return this;
}
/**
* 设置合理化
*
* @param reasonable
* @return
*/
public Page reasonable(Boolean reasonable) {
setReasonable(reasonable);
return this;
}
/**
* 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
*
* @param pageSizeZero
* @return
*/
public Page pageSizeZero(Boolean pageSizeZero) {
setPageSizeZero(pageSizeZero);
return this;
}
/**
* 指定 count 查询列
*
* @param columnName
* @return
*/
public Page countColumn(String columnName) {
setCountColumn(columnName);
return this;
}
public PageInfo toPageInfo() {
return new PageInfo(this);
}
public PageInfo toPageInfo(Function function) {
List list = new ArrayList(this.size());
for (E e : this) {
list.add(function.apply(e));
}
PageInfo pageInfo = new PageInfo(list);
pageInfo.setPageNum(this.getPageNum());
pageInfo.setPageSize(this.getPageSize());
pageInfo.setPages(this.getPages());
pageInfo.setStartRow(this.getStartRow());
pageInfo.setEndRow(this.getEndRow());
pageInfo.calcByNavigatePages(PageInfo.DEFAULT_NAVIGATE_PAGES);
return pageInfo;
}
public PageSerializable toPageSerializable() {
return new PageSerializable(this);
}
public PageSerializable toPageSerializable(Function function) {
List list = new ArrayList(this.size());
for (E e : this) {
list.add(function.apply(e));
}
return new PageSerializable(list);
}
/**
* 兼容低版本 Java 7-
*/
public interface Function {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
T apply(E t);
}
public Page doSelectPage(ISelect select) {
select.doSelect();
return (Page) this;
}
public PageInfo doSelectPageInfo(ISelect select) {
select.doSelect();
return (PageInfo) this.toPageInfo();
}
public PageSerializable doSelectPageSerializable(ISelect select) {
select.doSelect();
return (PageSerializable) this.toPageSerializable();
}
public long doCount(ISelect select) {
this.pageSizeZero = true;
this.pageSize = 0;
select.doSelect();
return this.total;
}
public String getCountColumn() {
return countColumn;
}
public void setCountColumn(String countColumn) {
this.countColumn = countColumn;
PageHelper.PagingRequestAdapter requestAdapter = PageHelper.getLocalPagingRequest();
if (requestAdapter != null && requestAdapter.page == this) {
requestAdapter.setCountColumn(countColumn);
}
}
@Override
public String toString() {
return "Page{" +
"count=" + count +
", pageNum=" + pageNum +
", pageSize=" + pageSize +
", startRow=" + startRow +
", endRow=" + endRow +
", total=" + total +
", pages=" + pages +
", reasonable=" + reasonable +
", pageSizeZero=" + pageSizeZero +
'}' + super.toString();
}
@Override
public void close() {
PageHelper.clearPage();
}
}