分页查询往往需要使用到查询工具类
package cn.edu.nwsuaf.GYL.query;
import java.util.ArrayList;
import java.util.List;
public class PageResult<T> {
//这是一个分页查询相关的类,他需要具有泛型的功能
//功能
//1:页面上显示分页
//2:页面上显示数据
// 主表的当前页码
private int currentPage;
// 子表的当前页码
private int currentPage_zhib;
//提供了set和get方法
public int getCurrentPage_zhib() {
return currentPage_zhib;
}
public void setCurrentPage_zhib(int currentPage_zhib) {
this.currentPage_zhib = currentPage_zhib;
}
// 一页显示的条数
private int pageSize;
// 总条数,查询出来的
private int totalRows;
// 总页数:计算出来的
private int totalPages;
// 当前页的数据,当前页的数据是不确定的,因此需要使用到泛型
private List<T> rows = new ArrayList<T>();
public PageResult() {
}
//通过构造函数将需要的数据传入,当前页,页面的大小,以及总共的条数记录,这个总的条数记录是从数据库里边
//查询出来的
public PageResult(int currentPage, int pageSize, int totalRows) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalRows = totalRows;
// currentPage和pageSize最小值必须是1,在向上翻页的时候页面值会减小
//并且当前页显示的数据也有,但是当前页的页面最小也只能是1,不能小于1
this.currentPage = Math.max(this.currentPage, 1);
this.pageSize = Math.max(this.pageSize, 1);
// 总页数:计算出来,这是一个计算方法,有几种同的计算方法,
//比如:一页显示5条,总共24条,那么(24+5-1)/5=5页,这实际就是在不能为整数
//的时候就需要多加一页
this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize;
// 错误处理
// 当前页面不能大于总页数
//再向下翻页的时候,当前页面的页数大小,不能大于总的页数
//例如:只有五页,当你向第六页翻的时候,就还是第五页
this.currentPage = Math.min(this.currentPage, this.totalPages);
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
@Override
public String toString() {
return "PageResult [currentPage=" + currentPage + ", pageSize="
+ pageSize + ", totalRows=" + totalRows + ", totalPages="
+ totalPages + ", rows=" + rows.size() + "]";
}
}