前言

在使用Mybatis时,最头痛的就是写分页了,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真的不想花双倍的时间写 count 和 select,幸好我们有 pagehelper 分页插件,pagehelper 是一个强大实用的 MyBatis 分页插件,可以帮助我们快速的实现MyBatis分页功能,而且pagehelper有个优点是,分页和Mapper.xml完全解耦,并以插件的形式实现,对Mybatis执行的流程进行了强化,这有效的避免了我们需要直接写分页SQL语句来实现分页功能。

现在我把自己在项目中实现分页的方法总结如下:

1.导入Maven依赖

<!--分页插件-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

2.controller中分页方法

 /**
  * @Description: 分页查询
  * @Param: waterloggingPreventionDto
  * @return com.graphsafe.api.msg.RestMessage(封装的返回值信息)
  * @author songwp
  * @date 2021/11/2 15:00
  */
@PostMapping(value = "getListForPage", produces = "application/json;charset=UTF-8")
public RestMessage getListForPage(@RequestBody WaterloggingPreventionDto waterloggingPreventionDto){
WaterloggingPreventionVo result = new WaterloggingPreventionVo();
List<WaterloggingPrevention> list = waterloggingPreventionService.getListForPage(waterloggingPreventionDto);
result.setWaterloggingPreventionList(list);
if (null != waterloggingPreventionDto.getCount()){
result.setTotal(waterloggingPreventionDto.getCount().intValue());
}
return new RestMessage(result);
}

3.service业务方法的实现

@Override
public List<WaterloggingPrevention> getListForPage(WaterloggingPreventionDto waterloggingPreventionDto) {
List<WaterloggingPrevention> list = new ArrayList<>();
if (waterloggingPreventionDto.getPage() != null && waterloggingPreventionDto.getLimit() != null){
PageHelper.startPage(waterloggingPreventionDto.getPage(),waterloggingPreventionDto.getLimit());
list = waterloggingPreventionMapper.getListForPage(waterloggingPreventionDto);
PageInfo<WaterloggingPrevention> pageInfo = new PageInfo<>(list);
waterloggingPreventionDto.setCount(pageInfo.getTotal());
}else {
list = waterloggingPreventionMapper.getListForPage(waterloggingPreventionDto);
}
return list;
}

4.serviceye业务方法接口

/**
* @Description:
* @ClassName: WaterloggingPreventionService
* @Author: songwp
* @Date: 2021/10/16 13:35
*/
public interface WaterloggingPreventionService {
List<WaterloggingPrevention> getListForPage(WaterloggingPreventionDto waterloggingPreventionDto);
}

5.Mapper方法接口

/**
* @Description:
* @ClassName: WaterloggingPreventionMapper
* @Author: songwp
* @Date: 2021/10/16 13:30
*/
@Mapper
public interface WaterloggingPreventionMapper {
List<WaterloggingPrevention> getListForPage(WaterloggingPreventionDto waterloggingPreventionDto); }

6.参数实体类

/**
* @author songwp
* @Description:
* @date 2021/10/29:47
*/
@Data
public class WaterloggingPreventionDto { @ApiModelProperty(value = "名称")
private String name; //分页
@ApiModelProperty(value = "当前页码")
private Integer page; //当前页第一页是0 @ApiModelProperty(value = "每页条数")
private Integer limit;//每页步长 @ApiModelProperty(value = "总条数")
private Long count;//总条数
}

7.post数据测试分页展示:

基于SpringBoot项目MyBatis分页插件实现分页总结的更多相关文章

  1. Springboot集成mybatis通用Mapper与分页插件PageHelper

    插件介绍 通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 Example 相关的单表操作.通用 Mapper 是为了解决 MyBatis 使用 ...

  2. SpringBoot使用Mybatis注解开发教程-分页-动态sql

    代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...

  3. Springboot 使用PageHelper分页插件实现分页

    一.pom文件中引入依赖 二.application.properties中配置以下内容(二选一方案) 第一种:pagehelper.helper-dialect=mysqlpagehelper.re ...

  4. Mybatis的分页插件PageHelper分页失效的原因

    引用博客:个人博客地址:https://alexaccele.github.io/ PageHelper是Mybatis的一个很好的分页插件,但要使用它的分页功能需要注意一下几点 1.导入相关包,例如 ...

  5. mybatis generator插件系列--分页插件

    1.首先定义分页插件 MysqlPagePlugin.java package com.demo.mybatis.plugin; import org.mybatis.generator.api.Co ...

  6. 逆向工程文件example完美结合使用PageHelper分页插件及分页不成功原因

    原生的mybatis需要手写sql语句,项目数据库表多了之后,可以让你写sql语句写到手软,于是mybatis官方提供了mybatis-generator:mybatis逆向工程代码生成工具,用于简化 ...

  7. 在angular中利用分页插件进行分页

    必需:angular分页js和css  当然还有angular.js   还需要bootstrap的css angular.min.js (下面我直接把插件粘贴上去了,以免有的同学还要去找.是不是很贴 ...

  8. 基于springboot+thymeleaf+springDataJpa自带的分页插件实现完整的动态分页

    实现百度搜索使用的前五后四原则,效果如下. 下面贴出代码,复制到前端即可,只需要域中放置page对象就可以.(springdatajpa自带的page 注意:第一页是按0开始算的) <div c ...

  9. Mybatis的插件 PageHelper 分页查询使用方法

    参考:https://blog.csdn.net/ckc_666/article/details/79257028 Mybatis的一个插件,PageHelper,非常方便mybatis分页查询,国内 ...

随机推荐

  1. npm WARN ajv-keywords@2.1.1 requires a peer of ajv@^5.0.0 but none is installed. You must install peer dependencies yourself.

    解决: npm install -g npm-install-peers npm install -g npm npm i ajv 但是好像没啥用

  2. Java基础系列(5)- 使用IDEA开发

    IDEA开发 下载安装IDEA https://www.cnblogs.com/gltou/p/14956060.html 使用IDEA编写helloworld 踩坑总结 run的时候提示" ...

  3. Jmeter扩展组件开发(3) - 实现方法

    继承JavaSamplerClient,四种实现方法讲解 前提 JavaSamplerClient要把四种实现方法都继承,编译器才不会报错. com.demo(package包)右键新建一个secon ...

  4. java eclipse 使用随笔

    1,无法import java.awt. 等各种文件,解决办法:(在module-info.java文件中加入requires java,desktop这句话)

  5. 一文彻底掌握Apache Hudi异步Clustering部署

    1. 摘要 在之前的一篇博客中,我们介绍了Clustering(聚簇)的表服务来重新组织数据来提供更好的查询性能,而不用降低摄取速度,并且我们已经知道如何部署同步Clustering,本篇博客中,我们 ...

  6. Redis 高可用篇:你管这叫主从架构数据同步原理?

    在<Redis 核心篇:唯快不破的秘密>中,「码哥」揭秘了 Redis 五大数据类型底层的数据结构.IO 模型.线程模型.渐进式 rehash 掌握了 Redis 快的本质原因. 接着,在 ...

  7. NOIP 模拟 七十一

    最后一场多校模拟赛,好像是信心赛??不过考的不行..最近难题比较多,对题目的难度把握不够好,经常出现简单题跳过的现象. 100+100+20+40 T1 签到题(qiandao) 如果一个点的度数不是 ...

  8. efcore分表分库原理解析

    ShardingCore ShardingCore 易用.简单.高性能.普适性,是一款扩展针对efcore生态下的分表分库的扩展解决方案,支持efcore2+的所有版本,支持efcore2+的所有数据 ...

  9. Three 之 Animation 初印象

    Animation 初印象 动画效果 播放动画需要基本元素 AnimationMixer 一个对象所有动作的管理者 用于场景中特定对象的动画的播放器.一个对象可能有多个动作,Mixer 是用来管理所有 ...

  10. keystore password was incorrect

    一.问题由来 最近在部署后台系统项目的时候,希望给项目增加一些安全措施,在项目中添加了SSL证书,可是在自己添加完该证书后,测试启动项目立马报错. 报错信息如下: org.springframewor ...