分页工具类
import cn.hutool.core.lang.Assert; import com.github.pagehelper.PageInfo; import com.google.common.collect.Lists; import org.springframework.util.CollectionUtils; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class PageHelperUtil { /** * 根据类型T分页对象 生成对应的类型R的分页对象 * @param sourcePage 源分页对象 * @param mapper 转换规则 * @return * @param <T> 源数据类型 * @param <R> 目的数据类型 */ public static <T, R> PageInfo<R> convert(PageInfo<T> sourcePage, Function<T, R> mapper) { Assert.notNull(sourcePage, "源分页数据不能为空!"); List<T> sourcePageList = sourcePage.getList(); List<R> targetPageList; if (!CollectionUtils.isEmpty(sourcePageList)) { targetPageList = sourcePageList.stream().map(mapper).collect(Collectors.toList()); } else { targetPageList = Collections.emptyList(); } PageInfo<R> targetPage = PageInfo.of(targetPageList); targetPage.setPageSize(sourcePage.getPageSize()); targetPage.setPageNum(sourcePage.getPageNum()); targetPage.setTotal(sourcePage.getTotal()); return targetPage; } /** * 获取一个github分页 * * @param iterator iterator * @param pageNum 页数 * @param pageSize 页大小 * @param totalCount 总数据量 * @return 分页信息 */ public static <T> PageInfo<T> page(Iterator<T> iterator, int pageNum, int pageSize, long totalCount) { PageInfo<T> page = PageInfo.of(Lists.newArrayList(iterator), pageNum); page.setPageSize(pageSize); page.setPageNum(pageNum); page.setTotal(totalCount); return page; } /** * 获取一个github分页 * * @param list 返回数据 * @param pageNum 页数 * @param pageSize 页大小 * @param totalCount 总数据量 * @return 分页信息 */ public static <T> PageInfo<T> page(List<T> list, int pageNum, int pageSize, long totalCount) { PageInfo<T> page = PageInfo.of(list, pageNum); page.setPageSize(pageSize); page.setPageNum(pageNum); page.setTotal(totalCount); return page; } }
标签:github,pageNum,分页,param,page,import,PageHelper,PageInfo From: https://www.cnblogs.com/zhangshiwen/p/17476095.html