项目中有时候遇到list多种来源,不能使用PageInfo在查询数据库时分页,需要查询全部之后,手动分页。
//手动分页的分割起始下标Integer fromIndex = 0;//手动分页的分割结尾下标Integer toIndex = 0;Integer total = arrayList.size();if(total/pageSize == 0 && total%pageSize > 0){fromIndex = 0;toIndex = total;}else{if(total/pageSize >= 1 && total % pageSize >= 0){fromIndex = pageSize * (pageNum-1);if(pageSize * pageNum >= total){toIndex = total;}else{toIndex = pageSize * pageNum;}}}//开启分页PageHelper.startPage(pageNum, pageSize);List<ModelStorageByUserAndType> list = arrayList.subList(fromIndex, toIndex);PageInfo<ModelStorageByUserAndType> pageInfo = new PageInfo<>(list);pageInfo.setTotal(total);//非常重要,作用是手动清理ThreadLocal存储的分页参,防止分页失效,参数乱套的情况PageHelper.clearPage();