前言
使用pagehelper进行分页操作
一、pagehelper是什么?
pagehelper作为MP的分页插件
二、使用步骤
1.引入依赖
代码如下(示例):
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
2.访问接口
代码如下(示例):
@PostMapping("/selectAllByCondition")
public Result<Page<ListDb>> selectAll(@RequestBody JSONObject jsonObject,
@RequestHeader String token){
PageInfo<ListDb> listDBPageInfo = listDBService.selectByCondition(jsonObject);
return Result.of(ResultStatus.SUCCESS, Page.of(listDBPageInfo.getList(), listDBPageInfo.getTotal()));
}
3.返回分页实体类
package cn.yun.industrial.common.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.util.List;
/**
* 分页数据实体类
*
* @author wangxiaoyang
* @date 2020/12/19
*/
@Data
@ToString
@EqualsAndHashCode
public class Page<T> {
/**
* 数据列表
*/
private List<T> list;
/**
* 数据总数
*/
private Long size;
public static <T> Page<T> of(List<T> list, Long size) {
Page<T> page = new Page<>();
page.setList(list);
page.setSize(size);
return page;
}
}
4.服务类
package cn.yun.industrial.service;
import cn.yun.industrial.entity.list.ListDb;
import cn.yun.industrial.entity.list.ListDbVo;
import cn.yun.industrial.entity.list.UnlistDb;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
public interface ListDBService {
PageInfo<ListDb> selectByCondition(JSONObject jsonObject)
}
4.服务实现类
// 执行查询
List<ListDb> list = listDbMapper.selectList(queryWrapper);
// 返回分页信息
return PageInfo.of(list);