网站首页 > 教程文章 正文
前言
我们在做页面查询的时候由于页面无法一次性展示所有的数据,所以采用一种分段式的展示策略—分页查询,实现分页查询的方式有很多种方式,比如sql中的limit,mybatisplus带的分页插件等等,这里我们介绍一下如何在SpringBoot中使用PageHelper插件实现分页查询。
正文
PageHelper
PageHelper是针对MyBatis最方便的分页插件PageHelper支持任何复杂的单表,多表查询。 它有以下特点:
- PageHelper不支持嵌套结果映射.
- PageHelper本质上是两次查询,第一次是对记录总数量的查询,第二次是对记录的查询。
- 对记录的查询是利用了mybatis提供的拦截器,取得ThreadLocal的pageSize和pageNo,重新拼装分页sql,完成分页。实际上是在sql后面拼接limit来实现的。
limit分页查询的弊端:
- 当limit offset rows中的offset很大时,会出现效率问题,所以数据规模大是不推荐使用PageHelper来实现分页。
- 实际的开发过程中我们采用自己手写两次查询,在第二次对记录的查询是采用子查询的方式来对性能进行优化。
select * from test where val=4 limit 300000,5
优化成:
select * from test a inner join (select id from test where val=4 limit 300000,5) b on a.id=b.id
所以,如果在数据量不大的情况下可以使用PageHelper,否则就不推荐使用PageHelper了。
SpringBoot使用PageHelper实现数据分页
Maven依赖
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
application.properties:配置类
#Paging assistant configuration
#配置数据库
pagehelper.helper-dialect=mysql
#页参数合理化
pagehelper.reasonable=true
#启用了分页,并且先执行了count后面的查询也拼接了limit
pagehelper.support-methods-arguments=true
#如果POJO或者Map中发现了countSql属性,就会作为count参数使用
pagehelper.params=count=countSql
#mybatis log
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
PageRequestPam:请求的抽象类
/**
* 分页模糊查询请求的封装类
*/
@Setter
@Getter
@ToString
public abstract class PageRequestPam implements Serializable {
private static final long serialVersionUID = 1L;
private int pageNum; //页面序号
private int pageSize; //页面大小
}
UserInfoForSearchRes:请求类
/**
* 这是用户查询的封装类
*/
@Setter
@Getter
@ToString
@ApiModel("用户查询请求类")
public class UserInfoForSearchRes extends PageRequestPam {
private static final long serialVersionUID = -5625703056478578435L;
@ApiModelProperty( notes = "用户名", example = "xiyuan666")
private String userName;
@ApiModelProperty( notes = "查询开始时间")
private Date searchStartTime;
@ApiModelProperty( notes = "查询结束时间")
private Date searchEndTime;
}
PageUtils:分页的结果工具类
public class PageUtils {
/**
* 将分页信息封装到统一的接口
* @param pam
* @param pageInfo
* @return
*/
public static Page getPageResult(PageRequestPam pam, PageInfo<?> pageInfo) {
Page page = new Page();
page.setPageNo(pageInfo.getPageNum());
page.setPageSize(pageInfo.getPageSize());
page.setTotalSize(pageInfo.getTotal());
page.setTotalPages(pageInfo.getPages());
page.setValue(pageInfo.getList());
return page;
}
}
Page:分页结果的封装类
/**
* 分页查询的结果封装类
*/
@Data
public class Page implements Serializable {
private static final long serialVersionUID = 1L;
private int pageNo;//当前页
private int pageSize;//当前页大小
private long totalSize;//记录总数
private int totalPages;//页码总数
private Object value;
}
验证
分页查询接口
@PostMapping("/queryUserListPage")
@ApiOperation(value = "用户分页模糊查询")
@ControllerMethodLog
public ResponseResult queryUserListPage(@RequestBody UserInfoForSearchRes userInfoForSearchRes) {
long startTime = System.currentTimeMillis(); //获取开始时间
int pageNum = userInfoForSearchRes.getPageNum();
int pageSize = userInfoForSearchRes.getPageSize();
PageHelper.startPage(pageNum, pageSize);
List<UserInfoPojo> list = userService.getUserInfoByPage(userInfoForSearchRes);
Page page= PageUtils.getPageResult(userInfoForSearchRes, new PageInfo<UserInfoPojo>(list));
long endTime = System.currentTimeMillis(); //获取结束时间
log.info("用户模块分页查询-总条数:" + list.size() + "用时:" + (endTime - startTime) + "ms");
return ResponseResult.success(page, ConstantsUtil.QUERY_SUCCESS);
}
通过knife4j访问分页查询接口
控制台打印信息
作者:溪源的奇思妙想
原文链接:https://blog.csdn.net/weixin_40990818/article/details/109478502
猜你喜欢
- 2024-12-25 mybatis-plus-join编码实现Join联表查询,真香!
- 2024-12-25 MyBatis-Plus中如何使用ResultMap
- 2024-12-25 通过Mybatis Plus实现代码生成器,常见接口实现讲解
- 2024-12-25 SpringBoot集成mybatis-plus springboot集成mybatisplus的配置
- 2024-12-25 MyBatis-Plus码之重器 lambda 表达式使用指南,开发效率瞬间提升80%
- 2024-12-25 MyBatis Plus—CRUD 接口 mybatis plus typehandler
- 2024-12-25 聊聊关于Mybatis分页操作PageHelper及实现原理?
- 2024-12-25 Springboot+MybatisPlus实现用户CRUD操作(后端实现)
- 2024-12-25 MybatisPlus方法详细使用,实现无SQL式开发
- 2024-12-25 mybatis-plus 团队新作 mybatis-mate 轻松搞定企业级数据处理
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)