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

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

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

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

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

maven依赖

  1. <!--mybatis-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>1.3.1</version>
  6. </dependency>
  7. <!--mapper-->
  8. <dependency>
  9. <groupId>tk.mybatis</groupId>
  10. <artifactId>mapper-spring-boot-starter</artifactId>
  11. <version>1.1.4</version>
  12. </dependency>
  13. <!--pagehelper-->
  14. <dependency>
  15. <groupId>com.github.pagehelper</groupId>
  16. <artifactId>pagehelper-spring-boot-starter</artifactId>
  17. <version>1.2.1</version>
  18. </dependency>

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

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

application.yml配置信息

  1. #mybatis
  2. mybatis:
  3. #指定领域类的路径
  4. typeAliasesPackage: com.cky.domain
  5. #指定mapper.xml的路径
  6. mapperLocations: classpath:mapper/*.xml
  7.  
  8. mapper:
  9. mappers:
  10. #此处填写需要分页的Mapper的全路径类名。例如com.cky.xxxMapper
  11. - xxxxx
  12. - xxxxx
  13. not-empty: false
  14. identity: MYSQL
  15.  
  16. pagehelper:
  17. helperDialect: mysql
  18. reasonable: true
  19. supportMethodsArguments: true
  20. params: count=countSql

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

使用:

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

  1. PageHelper.startPage(pageNum, pageSize);
  2. List<Bean> list = mapper.selectByExample(example);
  3. 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. java常用队列分析

    一.ArrayBlockingQueue 首先看一段源码: public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...

  2. 谨防in、or 公用性能问题

    今天遇到一个奇葩的问题:在where条件中用了 m in(×××) or m>=10,查询直接超时,我看了一下,数据库中就2万条数据 我将查询改为了union all 结果就不超时了

  3. TabBar变透明

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];

  4. 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则

    负载均衡-Nginx中文文档 http://www.nginx.cn/doc/example/loadbanlance.html 负载均衡 一个简单的负载均衡的示例,把www.domain.com均衡 ...

  5. type为number的<input>标签 type和size属性失效

    html5中input的type属性增的可取值新增几种,对于不支持这几种新增值的浏览器会统一解析为text类型. Firefox.ie9不支持

  6. python重建二叉树

    # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None ...

  7. 微信js分享朋友圈(二)

    近期又用到微信分享的功能了.虽然不是第一次用了,依然我又有幸踩到了一个坑,所以分享一下吧. 根据微信sdk写的代码一步步很顺利,但是后面就是获取微信返回的分享结果的回调的时候IOS老是有问题,然后就网 ...

  8. python 字符串的格式化

    python字符串的格式化分为两种:1)% 方式    2)str.format()  方式. str.format() 是比 % 较新的方式, 大多数的 Python 代码仍然使用 % 操作符.但最 ...

  9. 011-HQL中级1-Hive快捷查询:不启用Mapreduce job启用Fetch task三种方式介绍

    如果你想查询某个表的某一列,Hive默认是会启用MapReduce Job来完成这个任务,如下: hive; Total MapReduce jobs Launching Job out since ...

  10. Spark的Driver节点和Executor节点

    转载自:http://blog.sina.com.cn/s/blog_15fc03d810102wto0.html 1.驱动器节点(Driver) Spark的驱动器是执行开发程序中的 main方法的 ...