Mybatis-plus把List数据分页

一、编写工具类:

/**
 * @project
 * @Description  多表联查-分页
 * @Author songwp
 * @Date 2022/8/8 10:31
 * @Version 1.0.0
 **/
public class PageUtils {

    public static Page<T> getPages(Integer pageNo, Integer pageSize, List list){
        Page<T> page = new Page<T>();
        int size = list.size();
        if(pageSize > size){
            pageSize = size;
        }
        // 求出最大页数,防止currentPage越界
        int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;
        if(pageNo > maxPage){
            pageNo = maxPage;
        }
        // 当前页第一条数据下标
        int currentIndex = pageNo > 1 ? (pageNo -1) * pageSize : 0;
        List pageList = new ArrayList<>();
        // 将当前页的数据放进pageList
        for (int i = 0; i < pageSize && currentIndex + i < size; i++) {
            pageList.add(list.get(currentIndex + i));
        }
        page.setCurrent(pageNo).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        return page;
    }

    /**
     * @Description:转换为 IPage 对象
     * @Author: songwp
     */
    public static <T, E> IPage<T> copy(IPage page, List<E> sourceList, Class<T> targetClazz) {
        IPage pageResult = new Page(page.getCurrent(),page.getSize(),page.getTotal());
        pageResult.setPages(page.getPages());
        List<T> records = BeanUtil.copyToList(sourceList,targetClazz);
        pageResult.setRecords(records);
        return pageResult;
    }

    /**
     * @Description:转换为 IPage 对象
     * @Author: songwp
     */
    public static <T, E> IPage<T> copy(IPage page, Class<T> targetClazz) {
        return copy(page,page.getRecords(),targetClazz);
    }

二、调用测试数据显示:

{
  "records":[  {

            "year": "2022",
            "month": "2",
            "sendTime": "2022-08-05T10:48:13.000+0000",
            "haveSent": 4,
            "totalSend": 5,
            "totalSalary": 1000010.0,
            "uploadId": "1555505949937033218",
            "pushStatus": 1
          }
        ],
        "total": "2",
        "size": "1",
        "current": "0",
        "orders": [],
        "optimizeCountSql": true,
        "searchCount": true,
        "countId": null,
        "maxLimit": null,
        "pages": "2"
}

posted @ 2022-08-08 11:21  [奋斗]  阅读(1597)  评论(0编辑  收藏  举报