springboot整合分頁插件PageHelper
1.導入page-helper的依賴
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
我這里導入的是1.3.0版本,如果想要更換其他版本在maven倉庫中查找
pagehelper的maven倉庫網址:https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter
2.在application.yml中配置page-helper
pagehelper:
#啟用合理化,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最后一頁
reasonable: true
#標識是哪一種數據庫
helper-dialect: postgresql
#支持通過 Mapper 接口參數來傳遞分頁參數,默認值false,分頁插件會從查詢方法的參數值中,自動根據上面 params 配置的字段中取值,查找到合適的值時就會自動分頁
support-methods-arguments: true
#如果 pageSize=0 就會查詢出全部的結果(相當於沒有執行分頁查詢)
page-size-zero: true
3.在控制層中添加page-helper插件
@GetMapping("/selectBook")
public PageInfo<Book> selectBook(){
// pageNum:當前頁,pageSize:每頁的顯示的數據數目
PageHelper.startPage(1,5);
List<Book> list = handleBookService.selectBook();
// 上面兩行代碼必須在一起,設置.startPage()方法后立即查詢數據
PageInfo<Book> pageInfo = new PageInfo<>(list);
// 獲得分頁后的數據信息
return pageInfo;
}