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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot+自定義 AOP 實現全局校驗的實例代碼
最近公司重構項目,重構為最熱的微服務框架 spring boot, 重構的時候遇到幾個可以統(tǒng)一處理的問題。這篇文章主要介紹了spring boot+自定義 AOP 實現全局校驗 ,需要的朋友可以參考下2019-04-04
使用@RequiredArgsConstructor注解來取代繁瑣的@Autowrired
有了@RequiredArgsConstructor注解,我們就可以減少@Autowired的書寫,本文主要介紹了使用@RequiredArgsConstructor注解來取代繁瑣的@Autowrired,感興趣的可以了解一下2022-04-04
IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測)
這篇文章主要介紹了IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
spring-boot中使用spring-boot-devtools的實現代碼
這篇文章主要介紹了spring-boot中使用spring-boot-devtools的實現代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
springboot?pom文件加入監(jiān)控依賴后沒有起作用的解決
這篇文章主要介紹了springboot?pom文件加入監(jiān)控依賴后沒有起作用的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

