Spring Boot 集成 PageHelper


配置一:在 【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);】。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM