SSM框架中Mybatis的分頁插件PageHelper分頁失效的原因


引用博客:個人博客地址:https://alexaccele.github.io/

PageHelper是Mybatis的一個很好的分頁插件,但要使用它的分頁功能需要注意一下幾點

1.導入相關包,例如maven導入依賴

1 <dependency>
2     <groupId>com.github.pagehelper</groupId>
3     <artifactId>pagehelper</artifactId>
4     <version>5.1.4</version>
5 </dependency>

2.在mybatis-config.xml中添加插件

引入 pageHelper插件

注意這里要寫成PageInterceptor, 5.0之前的版本都是寫PageHelper, 5.0之后要換成PageInterceptor

reasonable:分頁合理化參數,默認值為false。
  當該參數設置為 true 時,pageNum<=0 時會查詢第一頁,
  pageNum>pages(超過總數時),會查詢最后一頁。
  默認false 時,直接根據參數進行查詢。

1 <plugins>
2     <plugin interceptor="com.github.pagehelper.PageInterceptor">
3         <!--分頁參數合理化  -->
4         <property name="reasonable" value="true"/>
5     </plugin>
6 </plugins>

3.在Controller的方法中

PageHelper.startPage(1,5);//從第一頁開始,每頁5條記錄

以上代碼后面需緊跟查詢語句

1 List<Test> tests = testService.getAllTestsByTypeId(testTypeid);
2 PageInfo pageInfo = new PageInfo(tests,5);

當一個方法中有多個查詢語句時,只有緊跟在PageHelper.starPage()方法后的查詢結果才會分頁。

缺少以上三步都會導致分頁失效

 

參考:https://www.cnblogs.com/smfx1314/archive/2018/04/23/8920278.html


免責聲明!

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



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