在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. mysql 字符集研究

    一.创建一个测试数据库 及一个测试用的表.均使用默认的编码方式. show variables like 'char%': mysql> show variables like 'char%'; ...

  2. use Properties objects to maintain its configuration Writing Reading System Properties 维护配置 系统变量

    System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment) https:/ ...

  3. echarts容器动态设置高度

    测试提了bug,柱状图数据多的情况下,都叠到了一起,效果如下图. 要解决这个bug,首先想到的是让柱状图的容器自适应高度.于是,把原本div上写固定的高度去掉. <div id="my ...

  4. 提交任务到spark master -- 分布式计算系统spark学习(四)

    部署暂时先用默认配置,我们来看看如何提交计算程序到spark上面. 拿官方的Python的测试程序搞一下. qpzhang@qpzhangdeMac-mini:~/project/spark-1.3. ...

  5. Python中的编码与解码(转)

    Python中的字符编码与解码困扰了我很久了,一直没有认真整理过,这次下静下心来整理了一下我对方面知识的理解. 文章中对有些知识没有做深入的探讨,一是我自己也没有去深入的了解,例如各种编码方案的实现方 ...

  6. dos常用命令【总结】

    win7下有很多有用的dos命令,现在总结如下: 延伸:Linux常用命令[总结] 命令 作用 其他 ping 检查和另一台主机的连通性  ping  127.0.0.1   telnet 检查连通性 ...

  7. 单例Singleton模式的两种实现方法

    在设计模式中,有一种叫Singleton模式的,用它可以实现一次只运行一个实例.就是说在程序运行期间,某个类只能有一个实例在运行.这种模式用途比较广泛,会经常用到,下面是Singleton模式的两种实 ...

  8. Spark SQL中UDF和UDAF

    转载自:https://blog.csdn.net/u012297062/article/details/52227909 UDF: User Defined Function,用户自定义的函数,函数 ...

  9. django之自定义软删除Model

    软删除 简单的说,就是当执行删除操作的时候,不正真执行删除操作,而是在逻辑上删除一条记录.这样做的好处是可以统计数据,可以进行恢复操作等等. 预备知识 Managers Managers 是djang ...

  10. 推荐系统第4周--- 基于频繁模式的推荐系统和关联规则挖掘Apriori算法

    数据挖掘:关联规则挖掘