一、全部的gradle引入

// https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter
compile (group: 'com.github.pagehelper', name: 'pagehelper-spring-boot-starter', version: '1.2.10'){
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
} // https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5' // https://mvnrepository.com/artifact/com.zaxxer/HikariCP
compile group: 'com.zaxxer', name: 'HikariCP', version: '3.3.1'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15' // https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
compile (group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.0.0'){
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
} // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
compile (group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.2.RELEASE'){
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json'
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2', version: '2.1.2.RELEASE'

二、PageHelper的配置
   application.yml

pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql

三、使用实例

public List<ChatAgentPO> getAgentAllPage(int start){
PageHelper.startPage(start, );
return dao.selectAllPage();
}

四、更多参考官网

五、一个注意点

PageHelper 方法使用了静态的 ThreadLocal 参数,分页参数和线程是绑定的。
只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的。因为 PageHelper 在 finally 代码段中自动清除了 ThreadLocal 存储的对象。
如果代码在进入 Executor 前发生异常,就会导致线程不可用,这属于人为的 Bug(例如接口方法和 XML 中的不匹配,导致找不到 MappedStatement 时), 这种情况由于线程不可用,也不会导致 ThreadLocal 参数被错误的使用。
但是如果你写出下面这样的代码,就是不安全的用法:
PageHelper.startPage(1, 10);
List<Country> list;
if(param1 != null){
list = countryMapper.selectIf(param1);
} else {
list = new ArrayList<Country>();
}
这种情况下由于 param1 存在 null 的情况,就会导致 PageHelper 生产了一个分页参数,但是没有被消费,这个参数就会一直保留在这个线程上。当这个线程再次被使用时,就可能导致不该分页的方法去消费这个分页参数,这就产生了莫名其妙的分页。
上面这个代码,应该写成下面这个样子:
List<Country> list;
if(param1 != null){
PageHelper.startPage(1, 10);
list = countryMapper.selectIf(param1);
} else {
list = new ArrayList<Country>();
}
这种写法就能保证安全。
如果你对此不放心,你可以手动清理 ThreadLocal 存储的分页参数,可以像下面这样使用:
List<Country> list;
if(param1 != null){
PageHelper.startPage(1, 10);
try{
list = countryMapper.selectAll();
} finally {
PageHelper.clearPage();
}
} else {
list = new ArrayList<Country>();
}

Springboot+Mybatis整合PageHelper的更多相关文章

  1. 7、SpringBoot+Mybatis整合------PageHelper简单分页

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/1d30d2a573ce6784149a28af9b ...

  2. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  3. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  4. SpringBoot+Mybatis整合入门(一)

    SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...

  5. SpringBoot集成整合pageHelper分页插件

    今天来讲讲springboot 集成 pagehelper插件, 引入jar 依赖包 <!-- 分页插件 --><dependency> <groupId>com. ...

  6. SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页

    前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了 ...

  7. springboot+mybatis使用PageHelper分页

    项目结构和spring搭建mybatis请参考springboot整合mybatis.在这个基础上配置分页. 一:导入PageHelper依赖 <dependency> <group ...

  8. springboot/Mybatis整合

    正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper ...

  9. SpringBoot+Mybatis整合实例

    前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...

随机推荐

  1. 2018 icpc 青岛网络赛 J.Press the Button

    Press the Button Time Limit: 1 Second      Memory Limit: 131072 KB BaoBao and DreamGrid are playing ...

  2. soul

    mark,数据使用gzip加密,因为公司网络卡,明天后天看.

  3. js基础梳理-关于this常见指向问题的分析

    首先,依然回顾<js基础梳理-究竟什么是执行上下文栈(执行栈),执行上下文(可执行代码)?>中的 3.执行上下文的生命周期 3.1 创建阶段 生成变量对象(Variable object, ...

  4. python & MySQLdb(one)

    python开发过程中用到数据库无外乎MYSQL,Mangodb,redis三种,三者数据库使用可能存在差异,但在一些基础的语句使用时都是大同小异的,这阶段学习了一些基础操作,记录下 add: # - ...

  5. inner join 内联与left join 左联

    今天老板把我叫过去,给我分析了一下我写的存储过程[捂脸羞愧中...],因为又临时加了个需求需要关联另外一个视图,我写成了内联,所以读取出来的数据少了好多. select t1.MOTCARRIERNA ...

  6. AE插入音乐

    将音乐文件(如MP3文件)直接拖拽到工程里就可以. 然后添加到合成里. 点击内容预览 ,就自动播放了. 在合成窗口里面,我们可以对于音频文件进行拖动,以及裁剪等操作,但是需要注意的是AE里面不能预览声 ...

  7. Your ApplicationContext is unlikely to start due to a @ComponentScan of the default

    问题:** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau ...

  8. BKDR Hash 函数实现

    K&R一书中提出的BKDR Hash算法,这里给出C函数实现,实际用的时候分布比较好而且实现简单.唯一不明白的就是为什么选择131这种模式的数字作为种子,隐隐有沃尔夫勒姆31号自动机的似曾相识 ...

  9. python系统编程(十一)

    同步应用 多个线程有序执行 from threading import Thread,Lock from time import sleep class Task1(Thread): def run( ...

  10. 编程菜鸟的日记-初学尝试编程-寻找2到n之间的素数并输出

    //输入一个整数n,输出2到n之间的具体素数值 #include <iostream> #include <algorithm> #include <cmath> ...