项目中有时候遇到list多种来源,不能使用PageInfo在查询数据库时分页,需要查询全部之后,手动分页。
//list自定义排序
ComparatorUser comparator=new ComparatorUser();
Collections.sort(list, comparator);
//list分页开始
int fromIndex = 0;
int toIndex = 0;
int total = list.size(); //取出总记录数
if(total/pageSize == 0 && total%pageSize > 0){
fromIndex = 0;
toIndex = total;
}else{
if(total/pageSize >= 1 && total % pageSize >= 0){
fromIndex = pageSize * (pageIndex-1);
if(pageSize * pageIndex >= total){
toIndex = total;
}else{
toIndex = pageSize * pageIndex;
}
}
}
list = list.subList(fromIndex, toIndex);
PageInfo<User> pageinfo = new PageInfo<User>(list);
pageinfo = PageHelperTool.initPageInfoObj(pageIndex,total,pageSize,pageinfo);
import java.util.Comparator;
/**
* @author yang
* @Title: ComparatorUser .java
* @Package
* @Description: 自定义排序
* @date 2019年07月09日 10:01
*/
public class ComparatorUser implements Comparator {
public int compare(Object arg0, Object arg1) {
User user1=(User)arg0;
User user2=(User)arg1;
//首先比较年龄,如果年龄相同,则比较名字
int flag=user1.getAge().compareTo(user2.getAge());
if(flag==0){
return user1.getName().compareTo(user2.getName());
}else{
return flag;
}
}
}
//分页工具类
import com.github.pagehelper.PageInfo;
/**
* @author yang
* @Title: PageHelperTool.java
* @Package
* @Description: 分页工具类
* @date 2019年07月09日 10:01
*/
public class PageHelperTool {
/**
* PageHelper中,如果当前页是最后一页,则返回的nextPage是0,即首页,而有时我们需要最后一页的nextPage是lastPage,因此写此方法
* @param currentPage
* @param pageObj
* @return
*/
public static <T> PageInfo<T> lastPageSetNextPage(int currentPage, PageInfo<T> pageObj) {
if (currentPage == pageObj.getPages()) {
pageObj.setNextPage(pageObj.getPages());
}
return pageObj;
}
public static <T> PageInfo<T> initPageInfoObj(int currentPage, int total, int pageSize, PageInfo<T> pageInfo) {
pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage);
pageInfo.setTotal(total);
pageInfo.setPageNum(currentPage);
pageInfo.setPages((total + pageSize - 1) / pageSize);
pageInfo.setLastPage((total + pageSize - 1) / pageSize);
pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage);
pageInfo.setIsFirstPage(currentPage == 1 ? true : false);
pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize ? true : false);
pageInfo.setHasPreviousPage(currentPage == 1 ? false : true);
pageInfo.setHasNextPage(currentPage == (total + pageSize - 1) / pageSize ? false : true);
return calcNavigatepageNums(pageInfo);
}
private static <T> PageInfo<T> calcNavigatepageNums(PageInfo<T> pageInfo) {
//当总页数小于或等于导航页码数时
if (pageInfo.getPages() <= pageInfo.getNavigatePages()) {
pageInfo.setNavigatepageNums(new int[pageInfo.getPages()]);
for (int i = 0; i < pageInfo.getPages(); i++) {
pageInfo.getNavigatepageNums()[i] = i + 1;
}
} else { //当总页数大于导航页码数时
pageInfo.setNavigatepageNums(new int[pageInfo.getNavigatePages()]);
int startNum = pageInfo.getPageNum() - pageInfo.getNavigatePages() / 2;
int endNum = pageInfo.getPageNum() + pageInfo.getNavigatePages() / 2;
if (startNum < 1) {
startNum = 1;
//(最前navigatePages页
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
pageInfo.getNavigatepageNums()[i] = startNum++;
}
} else if (endNum > pageInfo.getPages()) {
endNum = pageInfo.getPages();
//最后navigatePages页
for (int i = pageInfo.getNavigatePages() - 1; i >= 0; i--) {
pageInfo.getNavigatepageNums()[i] = endNum--;
}
} else {
//所有中间页
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
pageInfo.getNavigatepageNums()[i] = startNum++;
}
}
}
return pageInfo;
}
}