SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询
前言:本文档使用的是 SpringBoot,如果是 Spring 还需要在 MyBatis 配置 xml 中配置拦截器,并且 PageHelper 是针对 MyBatis 的,MyBatis 的集成不在本文档中叙述,请先集成 MyBatis。
一、引入PageHelper分页插件
引入的方式有两种,可以是导入Jar包,也可以使用 Maven 来构建,本文档将使用 Maven 来构建项目,如果你希望使用导入Jar包的方式进行集成,请从下面的链接下载Jar包:
https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/
http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/
在 pox.xml 中添加PageHelper分页插件依赖
<!-- 分页插件pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
由于是SpringBoot项目,这里使用 pagehelper-spring-boot-starter ,如果是 spring 应使用 pagehelper
二、配置application.yml
# 分页配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
改代码不加入,程序也可以正常运行,请根据实际情况进行添加。
三、在service层中使用PageHelper插件
(1)最简单的分页查询,直接传入
public List<Article> selectArticleListByTag(TTag tag, int page, int rows) {
//使用分页插件,核心代码就这一行,页数、每页行数
PageHelper.startPage(page, rows);
//在 mapper.xml 中不要加 limit 分页,插件会自动拦截和添加 limit 分页
return articleMapper.selectArticleListByTag(tag);
}
分页的同时,获取总数量,从而计算总页数
(2)EasyUI为例
由于我使用了EasyUI,DataGrid控件的分页需要返回总行数,所以拿来展示比较经典
先定义一个 Page 变量,这个类在com.github.pagehelper.Page,然后依然是 PageHelper.startPage(page, rows),然后进行正常的查询,在查询后pages.getTotal()就可以获取到总行数了,拿到总行数,就可以计算出总页数
EasyUIDataGrid.java:
package com.sun123.springboot.entity.easyui;
import java.util.List;
public class EasyUIDataGrid {
//当前页显示的数据 ?表示任意类型
private List<?> rows;
//表中总个数,不是总页数
private long total;
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
}
UserService.java:
//查询所有学生信息
EasyUIDataGrid showAllStudents(int page, int rows);
UserServiceImpl.java:
public EasyUIDataGrid showAllStudents(int page, int rows) {
EasyUIDataGrid dataGrid = new EasyUIDataGrid();
Page pages = PageHelper.startPage(page,rows);
List<User> userList = userMapper.selectByExample(null);
dataGrid.setRows(userList);
dataGrid.setTotal(pages.getTotal());
return dataGrid;
}
AdminController.java:
@PostMapping("/showAllStudents")
@ResponseBody
public EasyUIDataGrid showAllStudent(int page, int rows){
EasyUIDataGrid dataGrid = userService.showAllStudents(page, rows);
return dataGrid;
}
四、关于页数传入异常的情况
有同学会担心,如果传入的页数是负数或者大于总页数会不会报错?是否需要进行边界检查?
其实,如果传入的页数是负数,PageHelper 会展示第一页的数据;如果传入的页数大于总页数,PageHelper会展示最后一页的数据。并不会报错,所以使用这个插件比较省心,不用担心异常的页数传入导致业务抛出异常。
参考:https://www.neilren.com/Article/1003276
SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询的更多相关文章
- ssm下使用分页插件PageHelper进行分页
1. 导入maven依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...
- 基于Mybatis分页插件PageHelper
基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 ...
- SpringBoot集成MyBatis的分页插件 PageHelper
首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...
- SpringBoot添加对Mybatis分页插件PageHelper的支持
1.修改maven配置文件pom.xml,添加对pageHelper的支持: <!--pagehelper--> <dependency> <groupId>com ...
- SpringBoot Mybatis 分页插件PageHelper
添加maven配置: <!-- 分布插件 --> <dependency> <groupId>com.github.pagehelper</groupId&g ...
- Springboot集成mybatis通用Mapper与分页插件PageHelper
插件介绍 通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 Example 相关的单表操作.通用 Mapper 是为了解决 MyBatis 使用 ...
- Mybatis的分页插件PageHelper
Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina. ...
- mybatis分页插件PageHelper的使用(转)
Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_P ...
- MyBatis学习总结_17_Mybatis分页插件PageHelper
如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...
随机推荐
- iis8.0 https配置教程
打开iis>选择左侧根>点击右侧服务器证书 打开界面后 空白处点击右键选择导入 成功导入证书 选择需要绑定证书的网站点击选择>编辑绑定>ssl证书请选择您导入的证书 点击SSL ...
- apache安装及相应配置
给公司装过环境,自己也装过自己的服务器环境.但是每次都是现谷歌,毕竟每个人遇到的问题都不一样,还是记录下,以防忘记 一.安装 Centos7默认已经安装httpd服务,只是没有启动.如果你需要全新安装 ...
- thinkphp中用ajax对数据库进行操作
删除和查看详情操作的共同语句:就是怎么显示表? 1.在主体中写表的开头行,想要显示的内容,并且加载数据也要显示的地方也建张表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 & ...
- centos6.5mini版安装及配置
1.安装选择界面,这个选第一个 2.镜像完整性检查,一般都是跳过SKIP 3.欢迎界面,进入安装了 4.语言选择,这个是没有中文的,用默认的英文就行 5.键盘布局,用默认的us 6.这里会给一个警告, ...
- Linux学习笔记:【004】Linux内核代码风格
Chinese translated version of Documentation/CodingStyle If you have any comment or update to the c ...
- 使用JMeter进行一次简单的带json数据的post请求测试,json可配置参数
配置: 1.新建一个线程组: 然后设置线程数.运行时间.重复次数. 2.新建Http请求: 设置服务器域名,路径,方法,编码格式,数据内容. 可以在函数助手中,编辑所需要的变量,比如本例中的随机生成电 ...
- 声源测向: TDOA-GCC-PATH方法
收藏链接: http://www.funcwj.cn/2018/05/10/gcc-phat-for-tdoa-estimate/
- ubuntu主题收集
ubuntu主题收集 一些cmd常用命令: 任务栏底部,进入Dash并打开终端,命令最后一个是参数可选 ( Left | Bottom ) gsettings set com.canonical.Un ...
- 026、一张图搞懂docker(2019-01-21 周一)
参考https://www.cnblogs.com/CloudMan6/p/6961665.html
- eclipse竖向选择快捷键
eclipse的编辑器自带竖向选择功能,在mac上的开启和关闭方法都是command+option+a,在windows下则为alt+shift+a