public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 当前页码数
*/
private int pageNum;
/**
* 每页显示的记录数
*/
private int pageSize;
/**
* 分页下标
*/
private int limitIndex;
/**
* 开始行号
*/
private long startRow;
/**
* 结束行号
*/
private long endRow;
/**
* 总记录数
*/
private long rows;
/**
* 总页码数
*/
private int pages;
/**
* 首页页码
*/
private int firstPage;
/**
* 上一页页码
*/
private int previousPage;
/**
* 下一页页码
*/
private int nextPage;
/**
* 尾页页码
*/
private int lastPage;
/**
* 是否有上一页
*/
private boolean hasPreviousPage = false;
/**
* 是否有下一页
*/
private boolean hasNextPage = false;
/**
* 是否为首页
*/
private boolean ifFirstPage = false;
/**
* 是否为最后一页
*/
private boolean ifLastPage = false;
// 分页显示的页数,比如在页数上显示1,2,3,4,5.start就是1,end就是5
private int start = 1;
private int end = 5;
private int[] navigation;
// 存放本次分页查询出来的数据
private List<T> list;
public static PageInfo pageInfoBuild(long rows) {
int curPage = ServletUtils.getParameterToInt(ProjectConstants.PAGE_NUM);
int pageRow = ServletUtils.getParameterToInt(ProjectConstants.PAGE_SIZE);
return new PageInfo(curPage, pageRow, rows);
}
public static PageInfo pageInfoBuild(int curPage, long rows) {
int pageRow = ServletUtils.getParameterToInt(ProjectConstants.PAGE_SIZE);
return new PageInfo(curPage, pageRow, rows);
}
public static PageInfo pageInfoBuild(int curPage, int pageRow, long rows) {
return new PageInfo(curPage, pageRow, rows);
}
public PageInfo(int curPage, int pageRow, long rows) {
// 当前页码
this.pageNum = curPage;
// 每页显示的记录数
this.pageSize = pageRow;
// 总记录数
this.rows = rows;
// 分页下标
this.limitIndex = (this.pageNum - 1) * this.pageSize;
// 总页码数
this.pages = (int) Math.ceil(this.rows * 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 > rows) {
this.endRow = rows;
}
//设置首页
this.firstPage = 1;
//设置尾页
this.lastPage = this.pages;
//是否为第一页
this.ifFirstPage = this.pageNum == this.firstPage;
//是否为最后一页
this.ifLastPage = 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;
}
// 显示页数的算法
if (this.pages <= 5) {// 总页数小于5页
this.end = this.pages;
} else {
// 总页数是大于5的,那么就要根据当前是第几页来判断显示的start和end
this.start = pageNum - 2;
this.end = pageNum + 2;
if (start <= 0) {
this.start = 1;
this.end = 5;
}
if (end > this.pages) {
this.end = this.pages;
this.start = end - 4;
}
}
int temp = 0;
navigation = new int[(end - start) + 1];
for (int x = start; x <= end; x++) {
navigation[temp] = x;
temp++;
}
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getLimitIndex() {
return limitIndex;
}
public void setLimitIndex(int limitIndex) {
this.limitIndex = limitIndex;
}
public long getStartRow() {
return startRow;
}
public void setStartRow(long startRow) {
this.startRow = startRow;
}
public long getEndRow() {
return endRow;
}
public void setEndRow(long endRow) {
this.endRow = endRow;
}
public long getRows() {
return rows;
}
public void setRows(long rows) {
this.rows = rows;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getPreviousPage() {
return previousPage;
}
public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isIfFirstPage() {
return ifFirstPage;
}
public void setIfFirstPage(boolean ifFirstPage) {
this.ifFirstPage = ifFirstPage;
}
public boolean isIfLastPage() {
return ifLastPage;
}
public void setIfLastPage(boolean ifLastPage) {
this.ifLastPage = ifLastPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int[] getNavigation() {
return navigation;
}
public void setNavigation(int[] navigation) {
this.navigation = navigation;
}
@Override
public String toString() {
return "PageInfo{" +
"pageNum=" + pageNum +
", pageSize=" + pageSize +
", limitIndex=" + limitIndex +
", startRow=" + startRow +
", endRow=" + endRow +
", rows=" + rows +
", pages=" + pages +
", firstPage=" + firstPage +
", previousPage=" + previousPage +
", nextPage=" + nextPage +
", lastPage=" + lastPage +
", hasPreviousPage=" + hasPreviousPage +
", hasNextPage=" + hasNextPage +
", ifFirstPage=" + ifFirstPage +
", ifLastPage=" + ifLastPage +
", start=" + start +
", end=" + end +
", navigation=" + Arrays.toString(navigation) +
", list=" + list +
'}';
}
}
Java 分页计算
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和...
- 1.Mybatis分页介绍 我们在项目中经常会查询数据列表,数据库不会显示所有的数据,而是分页显示,Mybatis...
- 场景-总体思路 微服务跨库,通过接口调用 因分表分库无法通过xml里的sql进行联表管理查询 字段中文模糊查询,而...
- 一. searchable排序顺序 实例代码 这样放先根据createTime排再是lostState,放在上面的...