分页信息类用来封装分页的所有信息,如每页显示多少记录,总记录数、总页数、当前页、是否有上一页等信息。
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) {
this .everyPage = everyPage;
this .totalCount = totalCount;
this .totalPage = totalPage;
this .currentPage = currentPage;
this .beginIndex = beginIndex;
this .hasPrePage = hasPrePage;
this .hasNextPage = hasNextPage;
}
在进行分页的时候,必须知道3个信息,分别是当前页、每页显示记录数以及总记录数。只要知道了这3个信息,就可以通过计算得到分页的其他信息。这部分操作就交给分页辅助类来完成了。
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 ;
}
}
DaoImpl中根据每页显示记录数,取得该页显示的记录,存入list中
int start =page.getBeginIndex();
int end =page.getBeginIndex()+page.getEveryPage();
if(end >list.size ())
end =list.size ();
List payinlist = list.subList(start , end );
return payinlist;