/**
* 对象 分页请求和响应实体 TO
*
* @author Administrator 对象业务层封装 提供setter 计算的数据 可以直接提供getter 计算出来
*/
public class Pagination<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6323877942746161281L;
private Integer pageSize = 10;// 每页显示记录数 固定不动
private Integer pageNo = 1;
private Long totalCount = 0L;
private String orderBy = "createTime";
private String direction = "desc";
private List<T> data = new ArrayList<T>();
private int pageNum = 1;// 当前页码 每一次请求 来自 web 接受请求
private int totalPages;// 总页码 计算出来 : 总记录数 和 每页显示记录数
private int beforePage;// 上一页 当前页码 -1
private int nextPage;// 下一页 当前页码+1
private int pageBar[];// 每次查询 分页栏每一个显示页码 6----15 计算出来 总页码+当前页码计算出来
private List<T> pageRows;// 每一次查询 分页记录数 // 数据库查询 select * from xxx limit ?,?
private long totalCounts;// 总记录数 // 来自数据查询 ...业务层封装
private int startIndex;// 分页查询的起始记录数
private int size = 5;
private Long allCase;
private Long ownCase;
private Long otherCase;
private Long noCase;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPosStart() {
return (pageNo - 1) * pageSize;
}
public String getOrderBy() {
return orderBy;
}
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
public Long getTotalCount() {
return totalCount;
}
public void setTotalCount(Long totalCount) {
this.totalCount = totalCount;
}
public Long getPages() {
return (totalCount - 1) / pageSize + 1;
}
// 起始记录的计算
public int getStartIndex() {
this.startIndex = (this.pageNum - 1) * this.size;
return this.startIndex;// 计算出分页查询的起始记录数
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
/**
*
* 总页码
*
*
*
* @return
*
*/
public int getTotalPages() {
// 数学计算 总记录数 以及 每页显示记录数
// 10 120 12 10 119 12 10 121 13
this.totalPages = ((int) this.totalCounts + this.size - 1) / this.size;
return this.totalPages;
}
/**
*
* 上一页
*
*
*
* @return
*
*/
public int getBeforePage() {
this.beforePage = this.pageNum - 1;
if (this.beforePage <= 0) {
this.beforePage = 1;
}
return this.beforePage;
}
// 下一页
public int getNextPage() {
this.nextPage = this.pageNum + 1;
if (this.nextPage >= this.totalPages) {
this.nextPage = this.totalPages;
}
return this.nextPage;
}
/*
*
* 页码栏数值 6---15
*
*/
public int[] getPageBar() {
// 由当前页码 总页码 计算 规则 前五后四规则 基于 总页码>10 每次显示10个页码
// 计算起始页码 数字 以及 末尾 页码数字
int beginPage;// 起始页码 6 11
int endPage;// 末尾数字 15
// 1: 如果总页码 小于 10
if (this.totalPages <= 10) {
// 没有前五后四原则
beginPage = 1;
endPage = this.totalPages;
} else {
// 总页码大于10 前五后四
beginPage = this.pageNum - 5;
endPage = this.pageNum + 4;
// 排除 小于 0 大于总页码情况
if (beginPage <= 0) {
beginPage = 1;
endPage = beginPage + 9;
}
if (endPage >= this.totalPages) {
endPage = this.totalPages;
beginPage = this.totalPages - 9;
}
}
// 定好 起始数字 末尾数字 计算 beginPage 4 pageNum 9 endPage = 13
this.pageBar = new int[endPage - beginPage + 1];
int index = 0;
for (int i = beginPage; i <= endPage; i++) {
this.pageBar[index++] = i;
}
// 分页栏页码完成封装了!
return this.pageBar;
}
public List<T> getPageRows() {
return pageRows;
}
public void setPageRows(List<T> pageRows) {
this.pageRows = pageRows;
}
public long getTotalCounts() {
return totalCounts;
}
public void setTotalCounts(long totalCounts) {
this.totalCounts = totalCounts;
}
public Long getAllCase() {
return allCase;
}
public void setAllCase(Long allCase) {
this.allCase = allCase;
}
public Long getOwnCase() {
return ownCase;
}
public void setOwnCase(Long ownCase) {
this.ownCase = ownCase;
}
public Long getOtherCase() {
return otherCase;
}
public void setOtherCase(Long otherCase) {
this.otherCase = otherCase;
}
public Long getNoCase() {
return noCase;
}
public void setNoCase(Long noCase) {
this.noCase = noCase;
}
}
java分页工具类
最新推荐文章于 2024-08-13 09:57:18 发布