1. /**
    2. * pagehelper 手动分页
    3. * @param currentPage 当前页
    4. * @param pageSize
    5. * @param list
    6. * @param <T>
    7. * @return
    8. */
    9. public static <T> PageInfo<T> getPageInfo(int currentPage, int pageSize, List<T> list) {
    10. int total = list.size();
    11. if (total > pageSize) {
    12. int toIndex = pageSize * currentPage;
    13. if (toIndex > total) {
    14. toIndex = total;
    15. }
    16. list = list.subList(pageSize * (currentPage - 1), toIndex);
    17. }
    18. Page<T> page = new Page<>(currentPage, pageSize);
    19. page.addAll(list);
    20. page.setPages((total + pageSize - 1) / pageSize);
    21. page.setTotal(total);
    22. PageInfo<T> pageInfo = new PageInfo<>(page);
    23. return pageInfo;
    24. }

    例子:

    1. @Test
    2. public void test1(){
    3. List<Integer> list = new ArrayList<>();
    4. for(int i=1;i<31;i++){
    5. list.add(i);
    6. }
    7. System.out.println(list);
    8. Page<Integer> pageList = getPageList(1, 15, list);
    9. System.out.println(pageList.getRecords());
    10. }