原创作品,可以转载,但是请标注出处地址: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);
} }

最新文章

  1. Ubuntu回收站
  2. POJ 1155 (树形DP+背包+优化)
  3. Sqoop导数据出现的问题
  4. java多态例子
  5. 转:VS2010与SVN
  6. ZeroClipboard插件:兼容各浏览器网页复制功能
  7. Nova创建虚拟机的底层代码分析
  8. ContentResolver + SqliteOpenHelper + ContentProvider 理解
  9. 576. Out of Boundary Paths
  10. HDU 1724 Ellipse [辛普森积分]
  11. poj 2689 (素数二次筛选)
  12. Java 中Log4j的使用详情
  13. Jmeter5.1.1的汉化
  14. Python基础理论 - 常用模块
  15. 解决Spark filter过滤条件中使用&gt;=或&lt;=时不识别的问题
  16. Python知识梳理
  17. 基于Apache Spark机器学习的客户流失预测
  18. ElasticSearch 2 (3) - Breaking Changes
  19. pycharm使用docker镜像的python解释器,pycahrm可视化操作和管理dcoker
  20. CALayer &amp; bitmap Content

热门文章

  1. 洛谷P3802:小魔女帕琪
  2. C#中IPAddress转换成整型int
  3. 获取标准shell 命令的输出内容
  4. 常用的Tensor操作
  5. C#等同于正则表达式的写法
  6. Mesos源码分析(5): Mesos Master的启动之四
  7. 【安富莱】【RL-TCPnet网络教程】第10章 RL-TCPnet网络协议栈移植(FreeRTOS)
  8. [Swift]LeetCode44. 通配符匹配 | Wildcard Matching
  9. Java第三次上机随笔
  10. 【Spark篇】---Spark初始