分页对象
分页对象的构建:
import java.util.List; /** * 封装一个页面类,也有的叫Pegenation 用户每次查询都创建这个对象 * @author Administrator */ public class PageUtils { private int startRow;// 查询数据库是,当前页起始的第一条数据的索引 private int pageSize; // 一个页面显示多少条数据 private int currPage; // 当前页面的号码
private int totalData; // 总共多少条数据 private int totalPage; // 分页查看数据时总的页数
private int nextPage; // 下一页 private int prevPage; // 上一页
private int firstPage; // 首页 private int lastPage; // 尾页
private int navCount; // 导航条数目 private int startNav; // 导航条开始号码 private int endNav; // 导航条结束号码
private List pageData; // 当前页面的数据放在集合中
public PageUtils(int pageSize, int currPage, int navCount, int totalData) { this.currPage = currPage; // 当前页 this.pageSize = pageSize; // 每页多少条数据 this.navCount = navCount; // 导航条数目 this.totalData = totalData; // 总多少条数据
this.totalPage = (int) Math.ceil(totalData * 1.0 / pageSize); // 总页数 this.firstPage = 1; // 首页 this.lastPage = this.totalPage; // 尾页
if (this.navCount >= this.totalPage) { // 导航条数目大于总页数时,让导航条页数等于总页数 this.navCount = this.totalPage; }
if (0 > currPage) { // 当前页号码小于0,就让当前页号码为第一页 this.currPage = 1; }
if (currPage > (int) Math.ceil(totalData * 1.0 / pageSize)) { // 当前页号码大于总页数,就让当前页号码为最后一页数 this.currPage = this.lastPage; }
this.startRow = (this.currPage - 1) * pageSize; // 当前页起始的第一条数据的号码
this.prevPage = (this.currPage > 1 ? this.currPage - 1 : 1); // 上一页 this.nextPage = (this.currPage < lastPage ? this.currPage + 1 : this.currPage); // 下一页
this.startNav = this.currPage - this.navCount / 2; // 导航条开始页码 this.endNav = this.startNav + this.navCount - 1; // 导航条结束页码
if (this.currPage <= this.navCount / 2) { // 导航条开始页码过小时 this.startNav = this.firstPage; this.endNav = this.navCount; }
if (this.currPage >= (this.totalPage - this.navCount / 2 + 1)) { // 导航条开始页码过大时 this.startNav = this.totalPage - this.navCount + 1; this.endNav = this.totalPage; }
}
public int getStartRow() { return startRow; }
public void setStartRow(int startRow) { this.startRow = startRow; }
public int getPageSize() { return pageSize; }
public void setPageSize(int pageSize) { this.pageSize = pageSize; }
public int getCurrPage() { return currPage; }
public void setCurrPage(int currPage) { this.currPage = currPage; }
public int getTotalData() { return totalData; }
public void setTotalData(int totalData) { this.totalData = totalData; }
public int getTotalPage() { return totalPage; }
public void setTotalPage(int totalPage) { this.totalPage = totalPage; }
public int getNextPage() { return nextPage; }
public void setNextPage(int nextPage) { this.nextPage = nextPage; }
public int getPrevPage() { return prevPage; }
public void setPrevPage(int prevPage) { this.prevPage = prevPage; }
public int getFirstPage() { return firstPage; }
public void setFirstPage(int firstPage) { this.firstPage = firstPage; }
public int getLastPage() { return lastPage; }
public void setLastPage(int lastPage) { this.lastPage = lastPage; }
public int getNavCount() { return navCount; }
public void setNavCount(int navCount) { this.navCount = navCount; }
public int getStartNav() { return startNav; }
public void setStartNav(int statrNav) { this.startNav = statrNav; }
public int getEndNav() { return endNav; }
public void setEndNav(int endNav) { this.endNav = endNav; }
public List getPageData() { return pageData; }
public void setPageData(List pageData) { this.pageData = pageData; }
// 测试 public static void main(String[] args) { //设置每页数据pageSize为10条,当前页currPage为5, 导航条数目navCount为10个,totalData一个有56条数据 PageUtils pageUtil = new PageUtils(10, 5, 10, 56); System.out.println("总页数" + pageUtil.totalPage); System.out.println("首页" + pageUtil.firstPage); System.out.println("尾页" + pageUtil.lastPage); System.out.println("当前页" + pageUtil.currPage); System.out.println("其实记录条" + pageUtil.startRow); System.out.println("上一页" + pageUtil.prevPage); System.out.println("下一页" + pageUtil.nextPage); System.out.println("导航条数目" + pageUtil.navCount); System.out.println("导航条开始页码" + pageUtil.startNav); System.out.println("导航条结束页码" + pageUtil.endNav); } }
|