java分页工具类
2015-09-17 19:31
337 查看
在最近的项目中需要用到分页,所以自己写了个分页工具。
主要的原理就是将数据库取出的数据存储到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类:
主要的原理就是将数据库取出的数据存储到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; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android之获取手机上的图片和视频缩略图thumbnails
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序