在Mybatis中,如果想实现分页是比较麻烦的,首先需要先查询出总的条数,然后再修改mapper.xml,为sql添加limit指令。

幸运的是现在已经不需要这么麻烦了,刘大牛实现了一个超牛的分页工具类(https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md)

到底有多牛呢?现在一个分页实现只需要2步,像下面这样:

PageHelper.startPage(pageNum, pageSize);
List<Bean> list = mapper.selectByExample(example);
return new PageInfo<>(list);

是不是跃跃欲试了?let's begin!

maven依赖

<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>

在src/main/resources中需要创建META-INF文件夹,其中存放spring-devtools.properties文件,具体内容如下:

restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar

application.yml配置信息

#mybatis
mybatis:
#指定领域类的路径
typeAliasesPackage: com.cky.domain
#指定mapper.xml的路径
mapperLocations: classpath:mapper/*.xml mapper:
mappers:
#此处填写需要分页的Mapper的全路径类名。例如com.cky.xxxMapper
- xxxxx
- xxxxx
not-empty: false
identity: MYSQL pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql

在Application添加@MapperScan("com.cky.mapper")指定mapper的路径

使用:

在任意的Mapper查询方法前后分别添加如下两句:需要注意的是PageHelper必须紧挨着Mapper查询方法才能使PageHelper生效,当我们把list放入PageInfo 中时,他会自动计算分页所需要的信息。

PageHelper.startPage(pageNum, pageSize);
List<Bean> list = mapper.selectByExample(example);
return new PageInfo<>(list);

本实例只讲了springboot的集成,像spring的集成可以查看上面提供的github网址,其中的教程也十分好,中文的哦!

SpringBoot集成Mybatis-PageHelper分页工具类,实现3步完成分页的更多相关文章

  1. c#分页工具类,完美实现List分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Proje ...

  2. Springboot集成Mybatis+PageHelper

    1.Springboot项目引入mysql和mybatis的依赖: <dependency> <groupId>org.mybatis.spring.boot</grou ...

  3. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  4. springboot整合mybatis+pageHelper

    springboot整合mybatis+pageHelper 〇.搭建sporingboot环境,已经整合mybatis环境,本篇主要是添加pageHelper工具 一.添加依赖 <!-- 分页 ...

  5. 0120 springboot集成Mybatis和代码生成器

    在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...

  6. BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析

    重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...

  7. SpringBoot集成MyBatis底层原理及简易实现

    MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...

  8. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  9. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

随机推荐

  1. Code Forces 21C Stripe 2

    C. Stripe 2 time limit per test 1 second memory limit per test 64 megabytes input standard input out ...

  2. 2017-2018-2 20165330 实验四《Android程序设计》实验报告

    下载与安装Android Studio 下载地址:Download Android Studio 安装教程参考Android开发简易教程 实验内容 码云链接 任务一 参考<Java和Androi ...

  3. ZOJ3690—Choosing number

    题目链接:https://vjudge.net/problem/ZOJ-3690 题目意思: 有n个人,每个人可以从m个数中选取其中的一个数,而且如果两个相邻的数相同,则这个数大于等于k,问这样的数一 ...

  4. /proc/iomem和/proc/ioports对应的fops

    /proc/iomem和/proc/ioports对应的fops static int __init ioresources_init(void) {     struct proc_dir_entr ...

  5. angular(二)

    angularjs第二章 自定义指令 scope 控制器 AngularJS控制器控制AngularJS应用程序的数据,是常规的JavaScript对象. ng-controller指令就是用来定义应 ...

  6. Xshell 连接虚拟机特别慢 解决方案

    由于各种原因,xshell连接虚拟机的rhel或者CentOS都几乎是龟速...... 今天专门查了一下解决方案: 原来是ssh的服务端在连接时会自动检测dns环境是否一致导致的,修改为不检测即可,操 ...

  7. Gunner II--hdu5233(map&vector/二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...

  8. Proud Merchants---hdu3466(有01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3466 与顺序有关的01背包. 如果一个物品p = 5,q = 7,一个物品p = 5,q = 9,如果 ...

  9. 设计模式中类的关系UML

    在java以及其他的面向对象设计模式中,类与类之间主要有6种关系,他们分别是:依赖.关联.聚合.组合.继承.实现.他们的耦合度依次增强. 1. 依赖(Dependence)  依赖关系的定义为:对于两 ...

  10. CS224n(一)

    个人博客地址: https://yifdu.github.io/2018/10/30/CS224n%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89/#more