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

SpringBoot整合MyBatis分页插件PageHelper

步骤

第一步:首先整合MyBatis

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

第二步:添加必要的依赖

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>

第三步:添加必要的配置

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

@Configuration
public class PageHelperConfig {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
pageHelper.setProperties(properties);
return pageHelper;
}
}

第五步:使用插件

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

BookRepository.xml

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootdemo.mapper.BookRepository"> <!--省略多余内容--> <select id="getBooks" resultMap="bookResultMap">
select * from BOOK WHERE 1=1
<if test="bookId != null">
and BOOK_ID = #{bookId}
</if>
<if test="pageNum != null">
and PAGE_NUM = #{pageNum}
</if>
<if test="bookType != null">
and BOOK_TYPE = #{bookType}
</if>
<if test="bookDesc != null">
and BOOK_DESC = #{bookDesc}
</if>
<if test="bookPrice != null">
and BOOK_PRICE = #{bookPrice}
</if>
<if test="bookName != null">
and BOOK_NAME = #{bookName}
</if>
</select>
<select id="count" resultType="int">
select count(1) from BOOK WHERE 1=1
<if test="bookId != null">
and BOOK_ID = #{bookId}
</if>
<if test="pageNum != null">
and PAGE_NUM = #{pageNum}
</if>
<if test="bookType != null">
and BOOK_TYPE = #{bookType}
</if>
<if test="bookDesc != null">
and BOOK_DESC = #{bookDesc}
</if>
<if test="bookPrice != null">
and BOOK_PRICE = #{bookPrice}
</if>
<if test="bookName != null">
and BOOK_NAME = #{bookName}
</if>
</select>
<resultMap id="bookResultMap" type="Book">
<id column="BOOK_ID" property="bookId"/>
<result column="PAGE_NUM" property="pageNum"/>
<result column="BOOK_NAME" property="bookName"/>
<result column="BOOK_TYPE" property="bookType"/>
<result column="BOOK_DESC" property="bookDesc"/>
<result column="BOOK_PRICE" property="bookPrice"/>
<result column="CREATE_TIME" property="createTime"/>
<result column="MODIFY_TIME" property="modifyTime"/>
</resultMap>
</mapper>

BookRepository.java

public interface BookRepository {

    //省略多余内容

    List<Book> getBooks(Book book);
int count(Book book);
}

6-2 定义service

@Service
@Log4j2
public class BookService { @Autowired
private BookRepository bookRepository; // 省略多余内容 public ResponseEntity<PageInfo<Book>> getBooksByPageHelper(int pageId, int pageSize) {
PageHelper.startPage(pageId, pageSize);
List<Book> books = bookRepository.getBooks(Book.builder().build());
int totalNum = bookRepository.count(Book.builder().build());
PageInfo<Book> page = new PageInfo<>();
page.setPageNum(pageId);
page.setPageSize(pageSize);
page.setSize(totalNum);
page.setList(books);
return ResponseEntity.ok(page);
}
}

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

6-3 定义controller

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

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. H5本地存储详细使用教程(localStorage + JSON数据存储应用框架)

    一.Web Storage教程 1.概述: 对于Web Storage来说,实际上是Cookies存储的进化版.如果了解Cookie的人几乎一看Web Storage就会用,如果你从来没用过没了解过C ...

  2. 开发中少不了的Fun -- 微信开发IOS端alert/confirm提示信息,去除网址(URL)的方法

    在微信公众号开发的时候在使用[alert/confirm]弹出提示或者警告信息的时候,[alert/confirm]会将该公众号的网址显示出来,这样很不美观.所以很多时候我们会选择去除那个网址提示内容 ...

  3. Angular 2项目的环境配置和项目搭建

    AngularJS2 发布于2016年9月份,它是基于ES6来开发的. AngularJS2 是一款开源JavaScript库,由Google维护,用来协助单一页面应用程序运行.AngularJS2 ...

  4. 20175324 2018-2019-2 《Java程序设计》第8周学习总结

    本周学习<Java程序设计>第十五章: 泛型: 泛型(Generics)的主要目的是可以建立具有类型安全的集合框架,如链表.散列映射等数据结构. 泛型类声明:class 名称<泛型列 ...

  5. Nginx的虚拟主机配置

    虚拟主机技术能够让同一台服务器.同一组Nginx进程上运行多个网站,降低了资金和服务器资源的损耗.Nginx可以配置三种类型的虚拟主机,本文就是主要介绍这三种虚拟主机配置方式. 配置基于IP的虚拟主机 ...

  6. vue组件里定时器销毁问题

    我在a页面写一个定时,让他每秒钟打印一个1,然后跳转到b页面,此时可以看到,定时器依然在执行.这样是非常消耗性能的.如下图所示: 解决方法1: 首先我在data函数里面进行定义定时器名称: data( ...

  7. Flutter 编写内联文本

    使用Text.rich或者RichText ListView( children: <Widget>[ Text.rich( TextSpan( text: 'Text: ', child ...

  8. mysql 水平分表

    新建10张表,user_0,user_1,...user_9,方法不可串用,采用hash或取余法,获取要操作的表名,取值用对应存值的方法 1.hash取余法 public function part_ ...

  9. Swift 编程杂谈

    1.Swift 3.0 使用Cocopods 导入第三方报错 之前一直用Object-C 编写代码  用Cocopods导入第三方没出过什么问题(PS:2017最新cocoaPods安装教程) 今天用 ...

  10. VIM批量缩进

    方法一 1.按 ctrl + shif + ;  进入底行模式 2.将所要批量缩进的行号写上,按照格式:“行号1,行号2>”输入命令,如要将4至11行批量缩进一个tab值,则命令为“4,11&g ...