实现分页功能的方式很多,其中较为便捷的就是分页插件,这里是我自己封装的分页工具类,便于以后拿来直接用。逻辑简单、易于理解、封装轻巧、便于使用,当然追求强大功能的项目还是去引分页插件的依赖吧。
代码:
import java
.util
.List
;
public class PageInfo<T> {
private int lastPage
;
private int currentPage
;
private int nextPage
;
private int totalItems
;
private int totalPage
;
private int pageSize
= 5;
private List
<T> data
;
public PageInfo(int totalItems
) {
this.totalItems
= totalItems
;
this.totalPage
= totalItems
% pageSize
== 0 ? (totalItems
/ pageSize
) : ((totalItems
/ pageSize
) + 1);
}
public void setTotalPage(int totalPage
) {
this.totalPage
= totalPage
;
}
public void setCurrentPage(int currentPage
) {
if (currentPage
>= 1 && currentPage
<= totalPage
) {
System
.out
.println("页码正常");
this.currentPage
= currentPage
;
} else if (currentPage
< 1) {
System
.out
.println("页码小于1 , 使用默认页码 1");
this.currentPage
= 1;
} else if (currentPage
> totalPage
) {
System
.out
.println("页码大于尾页 , 使用尾页 "+totalPage
);
this.currentPage
= totalPage
;
}
this.lastPage
= this.currentPage
- 1;
this.nextPage
= this.currentPage
+ 1;
}
public void setPageSize(int pageSize
) {
this.pageSize
= pageSize
;
this.totalPage
= totalItems
% pageSize
== 0 ? (totalItems
/ pageSize
) : ((totalItems
/ pageSize
) + 1);
setCurrentPage(this.currentPage
);
}
public void setData(List
<T> data
) {
this.data
= data
;
}
public int getLastPage() {
return lastPage
;
}
public int getCurrentPage() {
return currentPage
;
}
public int getNextPage() {
return nextPage
;
}
public int getTotalItems() {
return totalItems
;
}
public int getTotalPage() {
return totalPage
;
}
public int getPageSize() {
return pageSize
;
}
public List
<T> getData() {
return data
;
}
}
转载请注明原文地址: https://lol.8miu.com/read-20240.html