/**
* pagehelper 手动分页
* @param currentPage 当前页
* @param pageSize
* @param list
* @param <T>
* @return
*/
public static <T> PageInfo<T> getPageInfo(int currentPage, int pageSize, List<T> list) {
int total = list.size();
if (total > pageSize) {
int toIndex = pageSize * currentPage;
if (toIndex > total) {
toIndex = total;
}
list = list.subList(pageSize * (currentPage - 1), toIndex);
}
Page<T> page = new Page<>(currentPage, pageSize);
page.addAll(list);
page.setPages((total + pageSize - 1) / pageSize);
page.setTotal(total);
PageInfo<T> pageInfo = new PageInfo<>(page);
return pageInfo;
}
例子:
@Test
public void test1(){
List<Integer> list = new ArrayList<>();
for(int i=1;i<31;i++){
list.add(i);
}
System.out.println(list);
Page<Integer> pageList = getPageList(1, 15, list);
System.out.println(pageList.getRecords());
}