方法一:
1.pom.xml依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency><dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> <exclusions> <exclusion> <artifactId>mybatis-spring</artifactId> <groupId>org.mybatis</groupId> </exclusion> <exclusion> <artifactId>mybatis</artifactId> <groupId>org.mybatis</groupId> </exclusion> </exclusions> </dependency>
PageHelper.startPage(dto.getPageNum(),dto.getPageSize()); List<DataElement> dataElements = baseMapper.elementList(dto); PageInfo<DataElement> dataElementPageInfo = new PageInfo<>(dataElements);
方法二:
1.工具类
package com.messageadapter.util; import com.messageadapter.common.api.ResultPageObject; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; public class PageUtil<E> { /** * 分页 * * @param list 数据 * @param pageNum 页数 * @param pageSize 条数 * @return */ public ResultPageObject listPagination(List<E> list, Integer pageNum, Integer pageSize) { ResultPageObject resultPageObject = new ResultPageObject(); if (CollectionUtils.isEmpty(list)) { resultPageObject.setCode(200); resultPageObject.setErrMsg("无数据"); return resultPageObject; } final int total = list.size(); int totalSize = pageNum * pageSize; if (totalSize > total) { totalSize = total; } int start = (pageNum - 1) * pageSize; List<E> newArray = new ArrayList<>(pageSize); for (int i = start; i < totalSize; i++) { newArray.add(list.get(i)); } resultPageObject.setCode(200); resultPageObject.setPageCurrent(pageNum); resultPageObject.setPageSize(pageSize); resultPageObject.setTotal(Long.parseLong(String.valueOf(total))); int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1; resultPageObject.setTotalPages(Long.parseLong(String.valueOf(totalPage))); resultPageObject.setData(newArray); return resultPageObject; } }
import io.swagger.annotations.*; import lombok.*; /** * @description:页面对象 * @time: 2019/11/6 14:13 */ @Data @ApiModel public class ResultPageObject { @ApiModelProperty(value = "状态码") private Integer code; @ApiModelProperty(value = "每页条数") private Integer pageSize; @ApiModelProperty(value = "总条数") private Long total; @ApiModelProperty(value = "数据") private Object data; @ApiModelProperty(value = "当前页") private Integer pageCurrent; @ApiModelProperty(value = "总页数") private Long totalPages; private String errMsg; public ResultPageObject() { } public ResultPageObject(Integer code, Integer pageSize, Long total, Object data, Integer pageCurrent, Long totalPages) { this.code = code; this.pageSize = pageSize; this.total = total; this.data = data; this.pageCurrent = pageCurrent; this.totalPages = totalPages; } public ResultPageObject(Integer code, String errMsg) { this.code = code; this.errMsg = errMsg; } public static ResultPageObject successReturn(Integer pageSize, Long total, Object data, Integer pageCurrent, Long totalPages) { return new ResultPageObject(200, pageSize, total, data, pageCurrent, totalPages); } /** * @param message 错误信息 * @Description:// * @Param: * @Return: * @Author: wzw * @Date: 2019/11/25 18:04 */ public static ResultPageObject errorPageObject(String message) { return new ResultPageObject(500, message); } }
PageUtil pageUtil = new PageUtil(); ResultPageObject resultPageObject = pageUtil.listPagination(dataElements, dto.getPageNum(), dto.getPageSize());