MyBatis-Plus?分頁查詢的實現示例
更新時間:2022年03月06日 09:55:03 作者:不是七七子
本文主要介紹了MyBatis-Plus?分頁查詢的實現示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
方法:
使用selectPage()方法,
第一個參數是傳入分頁方法(傳入當前頁和當前顯示多少條數據),
第二個參數是傳入查詢條件(如果查詢全部的話,可以傳null)。

前提:
表中的數據為:

第一種方式:
//分頁查詢
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("數據為:"+employees.getRecords());
System.out.println("總數為:"+employees.getTotal()+",總頁數為:"+employees.getPages());
System.out.println("當前頁為:"+employees.getCurrent()+",每頁限制:"+employees.getSize());
結果為:

展示了所有的數據,也沒有總數,并沒有分頁的效果。
第二種方式:
//分頁查詢
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
Integer count = employeeMapper.selectCount(null);
employees.setTotal(count);
System.out.println("數據為:"+employees.getRecords());
System.out.println("總數為:"+employees.getTotal()+",總頁數為:"+employees.getPages());
System.out.println("當前頁為:"+employees.getCurrent()+",每頁限制:"+employees.getSize());
結果為:

雖然有了總數和總頁數,但依然沒有分頁的效果。
第三種方式:
//分頁查詢
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("數據為:"+employees.getRecords());
System.out.println("總數為:"+employees.getTotal()+",總頁數為:"+employees.getPages());
System.out.println("當前頁為:"+employees.getCurrent()+",每頁限制:"+employees.getSize());
增加Mybatis-Plus插件,
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
return page;
}
}
結果:

到此這篇關于MyBatis-Plus 分頁查詢的實現示例的文章就介紹到這了,更多相關MyBatis-Plus 分頁查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
mybatis多對多關聯(lián)實戰(zhàn)教程(推薦)
下面小編就為大家?guī)硪黄猰ybatis多對多關聯(lián)實戰(zhàn)教程(推薦)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
如何使用Sentry 監(jiān)控你的Spring Boot應用
這篇文章主要介紹了如何使用Sentry 監(jiān)控你的Spring Boot應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

