配置一:在 【pom.xml】 文件中引入依赖
<!-- mybatis的分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency>
配置二:在 【application.properties】 文件中配置 pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
使用示例:
package com.huang.pims.family.controller; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.huang.pims.family.model.FamilyMember; import com.huang.pims.family.service.FamilyMemberService; import com.huang.pims.family.vo.FamilyMemberVO; import org.apache.ibatis.session.RowBounds; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; /** * (FamilyMember)表控制层 * * @author huangj * @since 2019-06-09 17:28:50 */ @RestController @RequestMapping("/familyMember") public class FamilyMemberController { private static final Logger LOGGER = LoggerFactory.getLogger(FamilyMemberController.class); /** * 服务对象 */ @Autowired private FamilyMemberService familyMemberService;
// offset代表页码,limit代表每页记录数 @RequestMapping(value = "/queryListForPageHelper", method = RequestMethod.POST) public ResponseEntity queryListForPageHelper(@RequestParam int offset, @RequestParam int limit) { LOGGER.info("rowBounds.offset={}, rowBounds.limit={}", offset, limit); PageHelper.startPage(offset, limit); List<FamilyMemberVO> familyMemberVOList = familyMemberService.queryListForPage(null); return new ResponseEntity(new PageInfo<>(familyMemberVOList), HttpStatus.OK); } }
测试
后端控制台输出
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c28a370] was not registered for synchronization because synchronization is not active JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4ea4e9c2] will not be managed by Spring ==> Preparing: SELECT count(0) FROM base_family_member WHERE status = 1 ==> Parameters: <== Columns: count(0) <== Row: 3 <== Total: 1 ==> Preparing: select id, member_name, nick_name, status, created_by, created_at, modified_by, modified_at from base_family_member where status = 1 LIMIT ? ==> Parameters: 2(Integer) <== Columns: id, member_name, nick_name, status, created_by, created_at, modified_by, modified_at <== Row: 1, 黄一号, 老爸, 1, null, 2019-05-03 18:55:36, null, 2019-05-03 18:55:36 <== Row: 2, 黄二号, 老妈, 1, null, 2019-05-03 00:23:05, null, 2019-05-03 00:23:05 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c28a370]
从上述输出内容可以看出,在执行查询sql之前,会先查询一次记录总数。然后通过记录总数,查询记录的页码,每页的最大记录数,为查询sql添加limit限制,从而达到分页的效果。上述的请求是每页展示2条记录,其中第一页的所有记录。如果请求的页数操过了总页数,则查询的结果始终是分页后的最后一页的记录。
PageHelper插件会将【PageHelper.startPage(offset, limit);】之后跟随的一次查询进行分页查询,后续如果还有查询,则不会再分页辅助,除非在查询之前再添加【PageHelper.startPage(offset, limit);】。