SpringBoot整合系列-PageHelper分页插件
2018-11-16 19:12
459 查看
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html
SpringBoot整合MyBatis分页插件PageHelper
步骤
第一步:首先整合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); } }
相关文章推荐
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
- spring-boot | 整合通用Mabatis 分页插件PageHelper
- Mybatis分页插件 - PageHelper与springBoot的整合
- Spring Boot整合Mybatis-Plus和PageHelper分页插件,附项目源码
- springboot+Mybatis整合pageHelper分页插件
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- springboot 集成,pagehelper分页插件不起作用java.lang.ClassCastException: java.util.ArrayList cannot be cast to
- Springboot +pagehelper 分页插件
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- SpringBoot Mybatis PageHelper分页插件的两种用法(二)
- springboot中分页插件pageHelper的使用
- SpringBoot整合PageHelper插件
- SpringBoot之分页插件PageHelper的使用
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- Spring,SpringBoot 集成 MyBatis 的分页插件 PageHelper
- springboot如何集成mybatis的pagehelper分页插件
- springboot 集成myBatis PageHelper 分页插件
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- spring-boot 集成mybatis的分页插件PageHelper版本问题
- SpringBoot集成分页插件PageHelper