# **分页配置** 配置代码 此处不需要改动 ~~~ package com.common; import com.constants.Constants; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data public class PageParamRequest { @ApiModelProperty(value = "页码", example= Constants.DEFAULT_PAGE + "") private int page = Constants.DEFAULT_PAGE; @ApiModelProperty(value = "每页数量", example = Constants.DEFAULT_LIMIT + "") private int limit = Constants.DEFAULT_LIMIT; } ~~~ 配置常量 ~~~ package com.constants; public class Constants { //默认分页页码 public static final int DEFAULT_PAGE = 1; //默认分页每页数量 public static final int DEFAULT_LIMIT = 20; //此处省略无数其余常量配置 } ~~~ 1. 如果想修改默认分页数值,请修改 DEFAULT_LIMIT 的值即可,这块会在前端不传递分页参数的时候起作用,或者自己new 分页参数类的时候起作用。 2. 分页参数的最终值以前端传递为准 3. 需要分页的时候在 *ServiceImpl文件的具体方法里,执行sql之前加一行代码 ~~~ PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit()); ~~~ 4. 如果最终方法返回的类和当前分页的时候映射的类不相同,例如当前业务是 class a,需要返回 class b,那么需要处理下数据。下文看文章这块如何处理 ~~~ /** * 文章列表 * @param request ArticleSearchRequest 请求参数 * @param pageParamRequest 分页类参数 * @author Mr.Zhang * @since 2020-04-18 * @return List<Article> */ @Override public PageInfo<ArticleVo> getList(ArticleSearchRequest request, PageParamRequest pageParamRequest) { //需要做赋值操作 Page<Article> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit()); LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>(); if(StringUtils.isNotBlank(request.getCid())){ lambdaQueryWrapper.eq(Article::getCid, request.getCid()); } if(!StringUtils.isBlank(request.getKeywords())){ lambdaQueryWrapper.and(i -> i.or().like(Article::getTitle, request.getKeywords()) .or().like(Article::getAuthor, request.getKeywords()) .or().like(Article::getSynopsis, request.getKeywords()) .or().like(Article::getShareTitle, request.getKeywords()) .or().like(Article::getShareSynopsis, request.getKeywords())); } if(request.getIsBanner() != null){ lambdaQueryWrapper.eq(Article::getIsBanner, request.getIsBanner()); } if(request.getIsHot() != null){ lambdaQueryWrapper.eq(Article::getIsHot, request.getIsHot()); } if(request.getHide() != null){ lambdaQueryWrapper.eq(Article::getHide, request.getHide()); } if(request.getStatus() != null){ lambdaQueryWrapper.eq(Article::getStatus, request.getStatus()); } if(null != request.getIsHaveMediaId()){ lambdaQueryWrapper.isNotNull(Article::getMediaId).ne(Article::getMediaId, ""); } lambdaQueryWrapper.orderByDesc(Article::getSort).orderByDesc(Article::getVisit).orderByDesc(Article::getCreateTime); List<Article> articleList = dao.selectList(lambdaQueryWrapper); ArrayList<ArticleVo> articleVoArrayList = new ArrayList<>(); if(articleList.size() < 1){ return CommonPage.copyPageInfo(articlePage, articleVoArrayList); } for (Article article : articleList) { ArticleVo articleVo = new ArticleVo(); BeanUtils.copyProperties(article, articleVo); if(!StringUtils.isBlank(article.getImageInput()) ){ articleVo.setImageInput(CrmebUtil.jsonToListString(article.getImageInput())); articleVo.setImageInputs(article.getImageInput()); } articleVoArrayList.add(articleVo); } //需要通过复制转化分页类指定的对象 return CommonPage.copyPageInfo(articlePage, articleVoArrayList); } ~~~ 分页操作类 ~~~ package com.common; import com.constants.Constants; import lombok.Data; import org.springframework.beans.BeanUtils; import org.springframework.data.domain.Page; import com.github.pagehelper.PageInfo; import java.util.ArrayList; import java.util.List; @Data public class CommonPage<T> { private Integer page = Constants.DEFAULT_PAGE; private Integer limit = Constants.DEFAULT_LIMIT; private Integer totalPage = 0; private Long total = 0L ; private List<T> list = new ArrayList<>(); /** * 将PageHelper分页后的list转为分页信息 */ public static <T> CommonPage<T> restPage(List<T> list) { CommonPage<T> result = new CommonPage<T>(); PageInfo<T> pageInfo = new PageInfo<T>(list); result.setTotalPage(pageInfo.getPages()); result.setPage(pageInfo.getPageNum()); result.setLimit(pageInfo.getPageSize()); result.setTotal(pageInfo.getTotal()); result.setList(pageInfo.getList()); return result; } /** * 将SpringData分页后的list转为分页信息 */ public static <T> CommonPage<T> restPage(Page<T> pageInfo) { CommonPage<T> result = new CommonPage<T>(); result.setTotalPage(pageInfo.getTotalPages()); result.setPage(pageInfo.getNumber()); result.setLimit(pageInfo.getSize()); result.setTotal(pageInfo.getTotalElements()); result.setList(pageInfo.getContent()); return result; } /** * 将PageHelper分页后的 PageInfo 转为分页信息 */ public static <T> CommonPage<T> restPage(PageInfo<T> pageInfo) { CommonPage<T> result = new CommonPage<T>(); result.setTotalPage(pageInfo.getPages()); result.setPage(pageInfo.getPageNum()); result.setLimit(pageInfo.getPageSize()); result.setTotal(pageInfo.getTotal()); result.setList(pageInfo.getList()); return result; } /** * 对象A复制对象B的分页信息 //TODO 多次数据查询导致分页数据异常解决办法 */ public static <T> PageInfo<T> copyPageInfo(com.github.pagehelper.Page originPageInfo, List<T> list) { PageInfo<T> pageInfo = new PageInfo<>(list); BeanUtils.copyProperties(originPageInfo, pageInfo, "list"); return pageInfo; } } ~~~ 在控制器里需要用下面代码来接受分页类的信息作为数据输出 ~~~ CommonPage.restPage(articleService.getList(request, pageParamRequest)) ~~~ 对外输出JSON下面自动封装 ~~~ /** * 分页显示文章管理表 * @param request ArticleSearchRequest 搜索条件 * @param pageParamRequest 分页参数 * @author Mr.Zhang * @since 2020-04-18 */ @ApiOperation(value = "分页列表") @RequestMapping(value = "/list", method = RequestMethod.GET) @ApiImplicitParam(name="keywords", value="搜索关键字") public CommonResult<CommonPage<ArticleVo>> getList(@Validated ArticleSearchRequest request, @Validated PageParamRequest pageParamRequest){ return CommonResult.success(CommonPage.restPage(articleService.getList(request, pageParamRequest))); } ~~~