基于SpringBoot项目MyBatis分页插件实现分页总结
前言
在使用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分页插件实现分页总结的更多相关文章
- Springboot集成mybatis通用Mapper与分页插件PageHelper
插件介绍 通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 Example 相关的单表操作.通用 Mapper 是为了解决 MyBatis 使用 ...
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...
- Springboot 使用PageHelper分页插件实现分页
一.pom文件中引入依赖 二.application.properties中配置以下内容(二选一方案) 第一种:pagehelper.helper-dialect=mysqlpagehelper.re ...
- Mybatis的分页插件PageHelper分页失效的原因
引用博客:个人博客地址:https://alexaccele.github.io/ PageHelper是Mybatis的一个很好的分页插件,但要使用它的分页功能需要注意一下几点 1.导入相关包,例如 ...
- mybatis generator插件系列--分页插件
1.首先定义分页插件 MysqlPagePlugin.java package com.demo.mybatis.plugin; import org.mybatis.generator.api.Co ...
- 逆向工程文件example完美结合使用PageHelper分页插件及分页不成功原因
原生的mybatis需要手写sql语句,项目数据库表多了之后,可以让你写sql语句写到手软,于是mybatis官方提供了mybatis-generator:mybatis逆向工程代码生成工具,用于简化 ...
- 在angular中利用分页插件进行分页
必需:angular分页js和css 当然还有angular.js 还需要bootstrap的css angular.min.js (下面我直接把插件粘贴上去了,以免有的同学还要去找.是不是很贴 ...
- 基于springboot+thymeleaf+springDataJpa自带的分页插件实现完整的动态分页
实现百度搜索使用的前五后四原则,效果如下. 下面贴出代码,复制到前端即可,只需要域中放置page对象就可以.(springdatajpa自带的page 注意:第一页是按0开始算的) <div c ...
- Mybatis的插件 PageHelper 分页查询使用方法
参考:https://blog.csdn.net/ckc_666/article/details/79257028 Mybatis的一个插件,PageHelper,非常方便mybatis分页查询,国内 ...
随机推荐
- 【PHP数据结构】栈的相关逻辑操作
对于逻辑结构来说,我们也是从最简单的开始.堆栈.队列,这两个词对于大部分人都不会陌生,但是,堆和栈其实是两个东西.在面试的时候千万不要被面试官绕晕了.堆是一种树结构,或者说是完全二叉树的结构.而今天, ...
- 总结了下PHPExcel官方读取的几个例子
1.使用 PHPExcel_IOFactory 读取文件 $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 2.使用一个特定的读取类,读 ...
- 对代理IP进行检测是否可用
第一种方法是使用telnetlib import telnetlib import requests from lxml import etree #解析此url页面的IP url = 'http:/ ...
- HTML 网页开发、CSS 基础语法——十二.CSS选择器
选择器 基础选择器:标签选择器,id选择器,类选择器,通配符选择器 高级选择器:后代选择器,交集选择器,并集选择器 1. 标签选择器: • 优点:可以选中所有的同名标签,设置所有同名标签的公共样式. ...
- Wannafly挑战赛10F-小H和遗迹【Trie,树状数组】
正题 题目链接:https://ac.nowcoder.com/acm/contest/72/F 题目大意 \(n\)个字符串,包括小写字母和\(\#\).其中\(\#\)可以替换为任意字符串.求有多 ...
- 使用Jacoco统计服务端代码覆盖情况实践
一.背景 随着需求的迭代,需求增加的同时,有可能会伴随着一些功能的下线.如果不对系统已经不用的代码进行梳理并删除不需要的代码,那么就会增加系统维护成本以及理解成本.但经历比较长的迭代以及系统交接,可能 ...
- 定制个机器人帮你和Ta聊天
自动聊天示例 这是基于200万聊天记录训练出来的,你可以用自己和女朋友的记录训练了试试效果 至于微信机器人怎么用,你可以 GitHub 搜搜看哈 聊天1: user: 在吗? bot: 在 user: ...
- C++核心编程 4 类和对象-封装
C++面向对象的三大特性:封装.继承.多态 C++认为万事万物皆为对象,对象上有其属性和行为 封装 意义:1.将属性和行为作为一个整体,表现生活中的事物 语法: class 类名{ 访问权限:属性 ...
- 教你 4 步搭建弹性可扩展的 WebAPI
作者 | 萧起 阿里云云原生团队 本文整理自<Serverless 技术公开课>,关注"Serverless"公众号,回复"入门",即可获取 Se ...
- 洛谷3320 SDOI2015寻宝游戏(set+dfs序)(反向迭代器的注意事项!)
被\(STL\)坑害了一个晚上,真的菜的没救了啊. 准确的说是一个叫\(reverse\ iterator\)的东西,就是我们经常用的\(rbegin()\) 有一个非常重要的性质 在反向迭代器中,+ ...