java分页工具类
在最近的项目中需要用到分页,所以自己写了个分页工具。
主要的原理就是将数据库取出的数据存储到List中,然后以设置的每页条数进行逐页处理。
一共分为两个类,Page类用于存储页面信息,Pagination类用于页面的操作,例如上一页、下一页、跳转等。
经验不足,写的代码很罗嗦,而且重复无用的东西太多,引以为戒
Page类:
import java.util.List; public class Page<T> { private int totalPage; //总页数 private int currentPage; //当前页数 private int totalCount; //内容总条数 private List<T> currentContent;//当前页的内容 public Page(){} public Page(int totalPage,int currentPage,int totalCount,List<T> currentContent){ this.totalPage=totalPage; this.currentPage=currentPage; this.totalCount=totalCount; this.currentContent=currentContent; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getCurrentContent() { return currentContent; } public void setCurrentContent(List<T> currentContent) { this.currentContent = currentContent; } }
Pagination类:
import java.util.LinkedList; import java.util.List; /** * * @author wangle * @date 2015.8.16 * @param <T> 页面显示的内容 * @description 分页封装类 */ public class Pagination<T> { private int totalPage; //总页数 private int currentPage; //当前页数 private int totalCount; //内容总条数 private final int PAGE_SIZE=10; //每页显示的条数 private List<T> allContent; //所有的内容 private List<T> currentContent; //当前页的内容 /** * @constructor 构造函数 */ public Pagination(){} /** * @constructor * @param content 初始化的数据 */ public Pagination(List<T> content){ this.allContent=content; this.totalCount=content.size(); this.currentPage=1; if(totalCount<=PAGE_SIZE){ totalPage=1; currentContent=allContent; } else if(totalCount%PAGE_SIZE==0){ totalPage=totalCount/PAGE_SIZE; } else{ totalPage=totalCount/PAGE_SIZE+1; } } /** * * @return 返回下一页 */ public Page<T> next(){ if(totalPage!=1){ if(currentPage<totalPage-1){ List<T> list = new LinkedList<T>(); for(int i=0;i<PAGE_SIZE;i++){ list.add(allContent.get(PAGE_SIZE*currentPage+i)); } currentPage++; currentContent=list; return new Page<T>(totalPage,currentPage,totalCount,currentContent); } else if(currentPage==totalPage-1){ LinkedList<T> list = new LinkedList<T>(); for(int i=0;i<((totalCount%PAGE_SIZE==0)?PAGE_SIZE:totalCount%PAGE_SIZE);i++){ list.add(allContent.get(PAGE_SIZE*currentPage+i)); } currentPage++; currentContent=list; return new Page<T>(totalPage,currentPage,totalCount,currentContent); } else{ return null; } } else{ return new Page<T>(totalPage,currentPage,totalCount,currentContent); } } /** * * @return 返回上一页 */ public Page<T> last(){ if(totalPage!=1){ if(currentPage>1){ currentPage--; LinkedList<T> list = new LinkedList<T>(); for(int i=0;i<PAGE_SIZE;i++){ list.add(allContent.get(PAGE_SIZE*(currentPage-1)+i)); } currentContent=list; return new Page<T>(totalPage,currentPage,totalCount,currentContent); } else{ LinkedList<T> list = new LinkedList<T>(); for(int i=0;i<((totalCount%PAGE_SIZE==0)?PAGE_SIZE:totalCount%PAGE_SIZE);i++){ list.add(allContent.get(PAGE_SIZE*(currentPage-1)+i)); } currentContent=list; return new Page<T>(totalPage,currentPage,totalCount,currentContent); } } else{ return new Page<T>(totalPage,currentPage,totalCount,currentContent); } } /** * * @param number 要跳转的页数 * @return 返回页面内容 */ public Page<T> getByPageNumber(int number){ if(number<totalPage){ LinkedList<T> list = new LinkedList<T>(); for(int i=0;i<PAGE_SIZE;i++){ list.add(allContent.get(PAGE_SIZE*(number-1)+i)); } currentContent=list; currentPage=number; return new Page<T>(totalPage,currentPage,totalCount,currentContent); } else if(number>totalPage){ return null; } else{ LinkedList<T> list = new LinkedList<T>(); for(int i=0;i<((totalCount%PAGE_SIZE==0)?PAGE_SIZE:totalCount%PAGE_SIZE);i++){ list.add(allContent.get(PAGE_SIZE*(number-1)+i)); } currentContent=list; currentPage=number; return new Page<T>(totalPage,currentPage,totalCount,currentContent); } } /** * * @return 返回第一页 */ public Page<T> getFirstPage(){ currentPage=1; return getByPageNumber(1); } /** * * @return 返回最后一页 */ public Page<T> getLastPage(){ currentPage=totalPage; return getByPageNumber(totalPage); } //getter和setter public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPAGE_SIZE() { return PAGE_SIZE; } public List<T> getAllContent() { return allContent; } public void setAllContent(List<T> content){ this.allContent=content; this.totalCount=content.size(); this.currentPage=1; if(totalCount<=PAGE_SIZE){ totalPage=1; currentContent=allContent; } else if(totalCount%PAGE_SIZE==0){ totalPage=totalCount/PAGE_SIZE; } else{ totalPage=totalCount/PAGE_SIZE+1; } } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getCurrentConetnt() { return currentContent; } }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: page分页工具类 数据库端
- 下一篇: Java web访问MySql数据库分页查询