SpringBoot整合PageHelper分頁無效的常見原因分析
SpringBoot整合PageHelper分頁無效的常見原因
1.maven依賴的問題
此類原因是與pom.xml文件中引入的分頁依賴有關
由于springboot本身集成pagerhelper的分頁插件
只需要引入如下依賴即可
<!-- spring-boot mybatis pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>如引入的為如下依賴
需要添加Bean注入(如何添加請自行百度)
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>2.執(zhí)行PageHelper.startPage(int pageNum, int pageSize)
后沒有緊跟分頁查詢,而是先執(zhí)行了其他查詢
如下初始化分頁器后,應該緊跟mybatis的分頁查詢語句,方法中如有其他查詢需求,需要在其他查詢完成后,再執(zhí)行PageHelper.startPage(int pageNum, int pageSize)方法
public PageInfo<R> page(Map<String, ? extends Object> map) {
//獲取第1頁,10條內容,默認查詢總數(shù)count
PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()), Integer.parseInt(map.get("pageSize").toString()));
String sql = String.format("%s%s",sqlMapping , map.get("mapping")==null?"getPageObjList" : map.get("mapping")) ;
List<R> l = sqlSessionTemplate.selectList(sql , map);
return new PageInfo<R>(l);
}3.沒有配置mybatis的分頁攔截器(也是我遇到的問題)
當攔截器沒有配置的時候,每次進行List查詢都會返回全部結果數(shù)據,此時需要在啟動類中注入攔截器類
@Bean
public Interceptor[] plugins() {
return new Interceptor[]{new PageInterceptor()};
}或者在MyBatis的配置文件mybatis-config.xml中添加如下代碼
<configuration> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"/> </plugins> </configuration>
總結
以上就是綜合網上大家遇到的springboot使用pagehelper進行分頁時,遇到查詢出全部數(shù)據而沒有進行分頁的常見問題及解決方案。
這些僅為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解析Java中的Timer和TimerTask在Android中的用法和實例
本篇文章主要介紹了解析Java中的Timer和TimerTask在Android中的用法,主要介紹了Timer和TimerTask的用法,有需要的可以了解一下。2016-11-11
IDEA無法創(chuàng)建JDK1.8版本的Springboot項目問題解決(2種方法)
本文主要介紹了IDEA無法創(chuàng)建JDK1.8版本的Springboot項目問題解決,包含兩種解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
spring+springmvc+mybatis 開發(fā)JAVA單體應用
這篇文章主要介紹了spring+springmvc+mybatis 開發(fā)JAVA單體應用的相關知識,本文通過圖文實例代碼的形式給大家介紹的非常詳細 ,需要的朋友可以參考下2018-11-11
mybatis createcriteria和or的區(qū)別說明
這篇文章主要介紹了mybatis createcriteria和or的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

