等价于com.github.pagehelper.PageInfo分页
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
public class PageUtil<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 当前页码数
*/
private int pageNum;
/**
* 每页显示的记录数
*/
private int pageSize;
/**
* 开始行号
*/
private long startRow;
/**
* 结束行号
*/
private long endRow;
/**
* 总记录数
*/
private long total;
/**
* 总页码数
*/
private int pages;
/**
* 导航条 页码数
*/
private int navigatePages ;
/**
* 导航条 具体页码数
*/
private int[] navigatePageNums;
/**
* 导航条上的开始页
*/
private int navigateStartPage;
/**
* 导航条上的结束页
*/
private int navigateEndPage;
/**
* 首页页码
*/
private int firstPage;
/**
* 上一页页码
*/
private int previousPage;
/**
* 下一页页码
*/
private int nextPage;
/**
* 尾页页码
*/
private int lastPage;
/**
* 是否有上一页
*/
private boolean hasPreviousPage = false;
/**
* 是否有下一页
*/
private boolean hasNextPage = false;
/**
* 是否为首页
*/
private boolean isFirstPage = false;
/**
* 是否为最后一页
*/
private boolean isLastPage = false;
// 存放本次分页查询出来的数据
private List<T> list;
public PageUtil(){
this.list = new ArrayList<>();
}
public PageUtil(List<T> list,int pageNum,int pageSize,long total){
this( list,pageNum,pageSize, total,8);
}
public PageUtil(List<T> list,int pageNum,int pageSize,long total,int navigatePages) {
if (list instanceof List) {
List<T> pageList = (List<T>)list;
this.list = pageList;
// 当前页码
this.pageNum = pageNum;
// 每页显示的记录数
this.pageSize = pageSize;
// 总记录数
this.total = total;
// 导航条 页码数
this.navigatePages = navigatePages;
// 总页码数
this.pages = (int) Math.ceil(this.total * 1.0 / this.pageSize);
// 当前页码大于总页码数时,当前页码=总页码数
if (this.pageNum > this.pages) {
this.pageNum = this.pages;
}
// 开始行号
this.startRow = (this.pageNum * this.pageSize)-(this.pageSize-1);
// 结束行号
this.endRow = this.pageNum * this.pageSize;
//当结束行号>总行数时,结束行号=总行数
if(this.endRow > total) {
this.endRow = total;
}
// 导航条 开始页码和结束页码
this.navigateStartPage = (this.pageNum - this.navigatePages / 2) > 0 ? (this.pageNum - this.navigatePages / 2) : 1;
this.navigateEndPage = this.navigateStartPage+this.navigatePages;
if(this.navigateEndPage>this.pages){
this.navigateEndPage = this.pages;
if(this.navigateEndPage>this.navigatePages){
this.navigateStartPage = this.navigateEndPage-this.navigatePages+1;
}
}
//导航条具体页数
this.navigatePageNums = new int[this.navigateEndPage];
for(int i=this.navigateStartPage; i<=this.navigateEndPage; i++){
this.navigatePageNums[i-this.navigateStartPage] = i;
}
//设置首页
this.firstPage = 1;
//设置尾页
this.lastPage = this.pages;
//是否为第一页
this.isFirstPage = this.pageNum==this.firstPage;
//是否为最后一页
this.isLastPage = this.pageNum==this.lastPage;
// 设置上一页
if (this.pageNum > 1) {
this.hasPreviousPage = true;
this.previousPage = this.pageNum - 1;
}
// 设置下一页
if (this.pageNum < this.pages) {
this.hasNextPage = true;
this.nextPage = this.pageNum + 1;
}
}
}
}