不错的Page类
package com.sanqing.util; public class Page { private int everyPage; //每页显示记录数 private int totalCount; //总记录数 private int totalPage; //总页数 private int currentPage; //当前页 private int beginIndex; //查询起始点 private boolean hasPrePage; //是否有上一页 private boolean hasNextPage; //是否有下一页 public Page(int everyPage, int totalCount, int totalPage, int currentPage, int beginIndex, boolean hasPrePage, boolean hasNextPage) { super(); this.everyPage = everyPage; this.totalCount = totalCount; this.totalPage = totalPage; this.currentPage = currentPage; this.beginIndex = beginIndex; this.hasPrePage = hasPrePage; this.hasNextPage = hasNextPage; } public Page(){} /** * @return the everyPage */ public int getEveryPage() { return everyPage; } /** * @param everyPage the everyPage to set */ public void setEveryPage(int everyPage) { this.everyPage = everyPage; } /** * @return the totalCount */ public int getTotalCount() { return totalCount; } /** * @param totalCount the totalCount to set */ public void setTotalCount(int totalCount) { this.totalCount = totalCount; } /** * @return the totalPage */ public int getTotalPage() { return totalPage; } /** * @param totalPage the totalPage to set */ public void setTotalPage(int totalPage) { this.totalPage = totalPage; } /** * @return the currentPage */ public int getCurrentPage() { return currentPage; } /** * @param currentPage the currentPage to set */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } /** * @return the beginIndex */ public int getBeginIndex() { return beginIndex; } /** * @param beginIndex the beginIndex to set */ public void setBeginIndex(int beginIndex) { this.beginIndex = beginIndex; } /** * @return the hasPrePage */ public boolean isHasPrePage() { return hasPrePage; } /** * @param hasPrePage the hasPrePage to set */ public void setHasPrePage(boolean hasPrePage) { this.hasPrePage = hasPrePage; } /** * @return the hasNextPage */ public boolean isHasNextPage() { return hasNextPage; } /** * @param hasNextPage the hasNextPage to set */ public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; } }
首先定出Page类的属性,
package com.sanqing.util; /* * 分页信息辅助类 */ public class PageUtil { public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象 everyPage = getEveryPage(everyPage); currentPage = getCurrentPage(currentPage); int totalPage = getTotalPage(everyPage, totalCount); int beginIndex = getBeginIndex(everyPage, currentPage); boolean hasPrePage = getHasPrePage(currentPage); boolean hasNextPage = getHasNextPage(totalPage, currentPage); return new Page(everyPage, totalCount, totalPage, currentPage, beginIndex, hasPrePage, hasNextPage); } public static int getEveryPage(int everyPage) { //获得每页显示记录数 return everyPage == 0 ? 10 : everyPage; } public static int getCurrentPage(int currentPage) { //获得当前页 return currentPage == 0 ? 1 : currentPage; } public static int getTotalPage(int everyPage,int totalCount) {//获得总页数 int totalPage = 0; if(totalCount != 0 &&totalCount % everyPage == 0) { totalPage = totalCount / everyPage; } else { totalPage = totalCount / everyPage + 1; } return totalPage; } public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置 return (currentPage - 1) * everyPage; } public static boolean getHasPrePage(int currentPage) {//获得是否有上一页 return currentPage == 1 ? false : true; } public static boolean getHasNextPage(int totalPage, int currentPage) { //获得是否有下一页 return currentPage == totalPage || totalPage == 0 ? false : true; } }
然后用一个静态方法生成实例