原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html

SpringBoot整合MyBatis分页插件PageHelper

步骤

第一步:首先整合MyBatis

参照之前SpringBoot整合系列-整合MyBatis

第二步:添加必要的依赖

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper</artifactId>
  4. <version>4.1.6</version>
  5. </dependency>

第三步:添加必要的配置

第四步:添加必要的配置类

  1. @Configuration
  2. public class PageHelperConfig {
  3. @Bean
  4. public PageHelper pageHelper(){
  5. PageHelper pageHelper = new PageHelper();
  6. Properties properties = new Properties();
  7. properties.setProperty("offsetAsPageNum","true");
  8. properties.setProperty("rowBoundsWithCount","true");
  9. properties.setProperty("reasonable","true");
  10. properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
  11. pageHelper.setProperties(properties);
  12. return pageHelper;
  13. }
  14. }

第五步:使用插件

6-1 定义mapper,延用之前的mapper

BookRepository.xml

  1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  2. <mapper namespace="com.example.springbootdemo.mapper.BookRepository">
  3. <!--省略多余内容-->
  4. <select id="getBooks" resultMap="bookResultMap">
  5. select * from BOOK WHERE 1=1
  6. <if test="bookId != null">
  7. and BOOK_ID = #{bookId}
  8. </if>
  9. <if test="pageNum != null">
  10. and PAGE_NUM = #{pageNum}
  11. </if>
  12. <if test="bookType != null">
  13. and BOOK_TYPE = #{bookType}
  14. </if>
  15. <if test="bookDesc != null">
  16. and BOOK_DESC = #{bookDesc}
  17. </if>
  18. <if test="bookPrice != null">
  19. and BOOK_PRICE = #{bookPrice}
  20. </if>
  21. <if test="bookName != null">
  22. and BOOK_NAME = #{bookName}
  23. </if>
  24. </select>
  25. <select id="count" resultType="int">
  26. select count(1) from BOOK WHERE 1=1
  27. <if test="bookId != null">
  28. and BOOK_ID = #{bookId}
  29. </if>
  30. <if test="pageNum != null">
  31. and PAGE_NUM = #{pageNum}
  32. </if>
  33. <if test="bookType != null">
  34. and BOOK_TYPE = #{bookType}
  35. </if>
  36. <if test="bookDesc != null">
  37. and BOOK_DESC = #{bookDesc}
  38. </if>
  39. <if test="bookPrice != null">
  40. and BOOK_PRICE = #{bookPrice}
  41. </if>
  42. <if test="bookName != null">
  43. and BOOK_NAME = #{bookName}
  44. </if>
  45. </select>
  46. <resultMap id="bookResultMap" type="Book">
  47. <id column="BOOK_ID" property="bookId"/>
  48. <result column="PAGE_NUM" property="pageNum"/>
  49. <result column="BOOK_NAME" property="bookName"/>
  50. <result column="BOOK_TYPE" property="bookType"/>
  51. <result column="BOOK_DESC" property="bookDesc"/>
  52. <result column="BOOK_PRICE" property="bookPrice"/>
  53. <result column="CREATE_TIME" property="createTime"/>
  54. <result column="MODIFY_TIME" property="modifyTime"/>
  55. </resultMap>
  56. </mapper>

BookRepository.java

  1. public interface BookRepository {
  2. //省略多余内容
  3. List<Book> getBooks(Book book);
  4. int count(Book book);
  5. }

6-2 定义service

  1. @Service
  2. @Log4j2
  3. public class BookService {
  4. @Autowired
  5. private BookRepository bookRepository;
  6. // 省略多余内容
  7. public ResponseEntity<PageInfo<Book>> getBooksByPageHelper(int pageId, int pageSize) {
  8. PageHelper.startPage(pageId, pageSize);
  9. List<Book> books = bookRepository.getBooks(Book.builder().build());
  10. int totalNum = bookRepository.count(Book.builder().build());
  11. PageInfo<Book> page = new PageInfo<>();
  12. page.setPageNum(pageId);
  13. page.setPageSize(pageSize);
  14. page.setSize(totalNum);
  15. page.setList(books);
  16. return ResponseEntity.ok(page);
  17. }
  18. }

此处使用PageHelper提供的PageInfo来承载分页信息,你也可以自定义分页模型来进行承载,但一般情况下使用给定的完全能满足要求

6-3 定义controller

  1. @RestController
  2. @RequestMapping("/book")
  3. @Api(description = "书籍接口")
  4. @Log4j2
  5. public class BookApi {
  6. @Autowired
  7. private BookService bookService;
  8. // 省略多余内容
  9. @RequestMapping(value = "/getBooksByPageHelper", method = RequestMethod.GET)
  10. @ApiOperation(value = "分页获取书籍", notes = "通过PageHelper分页获取书籍", httpMethod = "GET")
  11. public ResponseEntity<PageInfo<Book>> getBooksByPageHelper(final int pageId, final int pageNum){
  12. return bookService.getBooksByPageHelper(pageId, pageNum);
  13. }
  14. }

SpringBoot整合系列-PageHelper分页插件的更多相关文章

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

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

  2. spring-boot | 整合通用Mabatis 分页插件PageHelper

    Mybatis通用Mapper介绍 Mybatis 通用 Mapper 极其方便的使用 Mybatis 单表的增删改查,支持单表操作,不支持通用的多表联合查询 优点: 通用 Mapper 可以极大的方 ...

  3. Springboot整合Mybatis 之分页插件使用

    1: 引入jar包 <!-- 引入MyBatis分页插件--> <dependency> <groupId>com.github.pagehelper</gr ...

  4. SpringBoot整合MyBatis的分页插件PageHelper

    1.导入依赖(maven) <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...

  5. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  6. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  7. spring boot 整合pagehelper分页插件

    Spring Boot 整合pagehelper分页插件 测试环境: spring boot  版本 2.0.0.M7 mybatis starter 版本  1.3.1 jdk 1.8 ------ ...

  8. Spring Boot整合tk.mybatis及pageHelper分页插件及mybatis逆向工程

    Spring Boot整合druid数据源 1)引入依赖 <dependency> <groupId>com.alibaba</groupId> <artif ...

  9. SpringBoot 整合Mybatis + PageHelper 实现分页

    前言: 现在公司大多数都实现了前后端分离,前端使用Vue.React.AngularJS 等框架,不用完全依赖后端.但是如果对于比较小型的项目,没必要前后端分离,而SpringBoot也基本抛弃了Js ...

随机推荐

  1. http 缓存学习.

    mark 一下 HTTP 缓存机制一二三 http://web.jobbole.com/92773/ 彻底弄懂HTTP缓存机制及原理 https://www.cnblogs.com/chenqf/p/ ...

  2. date函数的属性

    date () a: "am"或是"pm" A: "AM"或是"PM" d: 几日,两位数字,若不足则补零:从" ...

  3. NOIP2017感悟

    Day1 第一次会做的题这么多却比以前靠的更差. 其实停课期间水平还是提升了很多,但是做题速度和心态问题一就是我最难克服的一个地方. 题目很简单,但是又很恶心,主要是我代码能力太差,第二题调不出来,第 ...

  4. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  5. java简单的双色球摇号程序

    import java.util.HashSet; import java.util.Random; import java.util.Set; /** * LotteryClient * @auth ...

  6. 关于js的页面高度和滚动条高度还有元素高度

    window.innerHeight    这是浏览器里面内容的高度,直接就是值,不需要其它操作; window.pageYOffset 这是滚动条到浏览器顶端的距离; $(元素).offset(). ...

  7. JavaWeb学习路线

    一.三大组件介绍 javaweb在开发中有三大组件分别提供不同的功能,这三大组件为servlet,filter,listener 1.servlet 简单来说就是客户端请求服务器和接受服务器的响应,狭 ...

  8. 微信小程序开发-tabbar组件

    "tabBar": { "backgroundColor": "#303133", "color": "#ff ...

  9. Three.js学习笔记01

    1.四大组件: 场景:场景是所有物体的容器 var scene = new THREE.Scene(); 相机: 正投影相机:远处的和近处的是一样大 THREE.OrthographicCamera ...

  10. [Swift]LeetCode398. 随机数索引 | Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...