Java分页工具类及其使用(示例分享)
Pager.java
packagepers.kangxu.datautils.common;
importjava.io.Serializable;
importjava.util.List;
/**
*
*<b>分页通用类</b>
*
*@authorkangxu
*@param<T>
*
*/
publicclassPager<T>implementsSerializable{
/**
*
*/
privatestaticfinallongserialVersionUID=4542617637761955078L;
/**
*currentPage当前页
*/
privateintcurrentPage=1;
/**
*pageSize每页大小
*/
privateintpageSize=10;
/**
*pageTotal总页数
*/
privateintpageTotal;
/**
*recordTotal总条数
*/
privateintrecordTotal=0;
/**
*previousPage前一页
*/
privateintpreviousPage;
/**
*nextPage下一页
*/
privateintnextPage;
/**
*firstPage第一页
*/
privateintfirstPage=1;
/**
*lastPage最后一页
*/
privateintlastPage;
/**
*content每页的内容
*/
privateList<T>content;
//以下set方式是需要赋值的
/**
*设置当前页<br>
*
*@authorkangxu
*
*@paramcurrentPage
*/
publicvoidsetCurrentPage(intcurrentPage){
this.currentPage=currentPage;
}
/**
*设置每页大小,也可以不用赋值,默认大小为10条<br>
*
*@authorkangxu
*
*@parampageSize
*/
publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize;
}
/**
*设置总条数,默认为0<br>
*
*@authorkangxu
*
*@paramrecordTotal
*/
publicvoidsetRecordTotal(intrecordTotal){
this.recordTotal=recordTotal;
otherAttr();
}
/**
*设置分页内容<br>
*
*@authorkangxu
*
*@paramcontent
*/
publicvoidsetContent(List<T>content){
this.content=content;
}
/**
*设置其他参数
*
*@authorkangxu
*
*/
publicvoidotherAttr(){
//总页数
this.pageTotal=this.recordTotal%this.pageSize>0?this.recordTotal/this.pageSize+1:this.recordTotal/this.pageSize;
//第一页
this.firstPage=1;
//最后一页
this.lastPage=this.pageTotal;
//前一页
if(this.currentPage>1){
this.previousPage=this.currentPage-1;
}else{
this.previousPage=this.firstPage;
}
//下一页
if(this.currentPage<this.lastPage){
this.nextPage=this.currentPage+1;
}else{
this.nextPage=this.lastPage;
}
}
//放开私有属性
publicintgetCurrentPage(){
returncurrentPage;
}
publicintgetPageSize(){
returnpageSize;
}
publicintgetPageTotal(){
returnpageTotal;
}
publicintgetRecordTotal(){
returnrecordTotal;
}
publicintgetPreviousPage(){
returnpreviousPage;
}
publicintgetNextPage(){
returnnextPage;
}
publicintgetFirstPage(){
returnfirstPage;
}
publicintgetLastPage(){
returnlastPage;
}
publicList<T>getContent(){
returncontent;
}
@Override
publicStringtoString(){
return"Pager[currentPage="+currentPage+",pageSize="+pageSize
+",pageTotal="+pageTotal+",recordTotal="+recordTotal
+",previousPage="+previousPage+",nextPage="+nextPage
+",firstPage="+firstPage+",lastPage="+lastPage
+",content="+content+"]";
}
}
使用PagerTester.java
packagepers.kangxu.datautils.utils;
importjava.util.ArrayList;
importjava.util.List;
importpers.kangxu.datautils.common.Pager;
/**
*分页数据测试
*<b>
*
*</b>
*@authorkangxu
*
*/
publicclassPagerTester{
publicstaticvoidmain(String[]args){
Pager<String>pager=newPager<String>();
List<String>content=newArrayList<String>();
content.add("str1");
content.add("str2");
content.add("str3");
content.add("str4");
content.add("str5");
content.add("str6");
content.add("str7");
content.add("str8");
content.add("str9");
content.add("str10");
pager.setCurrentPage(1);
pager.setPageSize(10);
pager.setRecordTotal(62);
pager.setContent(content);
System.out.println(pager);
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!