一文搞懂Mybatis-plus的分頁(yè)查詢操作
1. 簡(jiǎn)單說(shuō)明
嗨,大家好!今天給大家分享的是Mybatis-plus 插件的分頁(yè)機(jī)制,說(shuō)起分頁(yè)機(jī)制,相信我們程序員都不陌生,今天,我就給大家分享一下Mybatis-plus的分頁(yè)機(jī)制,供大家學(xué)習(xí)和Copy。
2. 介紹說(shuō)明
如果你想看代碼,可以直接跳到代碼區(qū)域,這里只是一些簡(jiǎn)單的說(shuō)明,如果你想學(xué)習(xí),建議可以看看這一塊的任容。
本章節(jié)將介紹 BaseMapper 中的分頁(yè)查詢,BaseMapper 接口提供了如下幾個(gè)分頁(yè)查詢接口:
- selectPage:根據(jù) entity 條件,查詢?nèi)坑涗?/li>
- selectMapsPage:根據(jù) Wrapper 條件,查詢?nèi)坑涗?/li>
在使用上面兩個(gè)方法進(jìn)行分頁(yè)查詢時(shí),我們需要配置分頁(yè)插件。這是只是在介紹SpringBoot的使用。
注意:由于我們使用的 Spring Boot 項(xiàng)目,因此需要通過(guò) @Configuration 和 @Bean 注解來(lái)添加配置
3. 完整配置類代碼
下邊就是完整的配置類,至于為什么比官網(wǎng)上的少一點(diǎn),因?yàn)槟莻€(gè)可以說(shuō)會(huì)報(bào)錯(cuò),而且也不需要使用到它,以下就是完整配置類:
package com.hxstrive.mybatis_plus; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusConfig { /** * 分頁(yè)插件。如果你不配置,分頁(yè)插件將不生效 */ @Bean public MybatisPlusInterceptor paginationInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 指定數(shù)據(jù)庫(kù)方言為 MYSQL interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
注意:如果你沒(méi)有配置分頁(yè)插件,則不會(huì)進(jìn)行分頁(yè)。所以這個(gè)一定要配置。
4. 示例代碼
1.使用 QueryWrapper 和 Page 作為參數(shù)進(jìn)行分頁(yè),例如:
package com.hxstrive.mybatis_plus.select; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.hxstrive.mybatis_plus.mapper.SimpleMapper; import com.hxstrive.mybatis_plus.model.UserBean; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest class Select6Test { @Autowired private SimpleMapper simpleMapper; @Test void contextLoads() { QueryWrapper<UserBean> wrapper = new QueryWrapper<>(); wrapper.isNotNull("user_id"); // 創(chuàng)建分頁(yè)對(duì)象(1表示第一頁(yè);4表示每頁(yè)大小為4) Page<UserBean> page = new Page<>(1, 4); Page<UserBean> result = simpleMapper.selectPage(page, wrapper); System.out.println("page == result: " + (page == result)); System.out.println("size: " + result.getSize()); System.out.println("total: " + result.getTotal()); for(UserBean userBean : result.getRecords()) { System.out.println(userBean); } } }
運(yùn)行上面代碼,你會(huì)發(fā)現(xiàn) page 和selectPage 返回的 result1 相等,說(shuō)明兩者是同一個(gè)對(duì)象。因此,可以忽略掉 selectPage 方法的返回結(jié)果,如下:
Page<UserBean> page = new Page<>(1, 4);
simpleMapper.selectPage(page, wrapper);
2.另外一個(gè)分頁(yè)方法,selectMapsPage 和上面的使用方法一樣,僅僅是返回類型不一樣。代碼如下:
package com.hxstrive.mybatis_plus.select; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.hxstrive.mybatis_plus.mapper.SimpleMapper; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest class Select7Test { @Autowired private SimpleMapper simpleMapper; @Test void contextLoads() { // 返回的結(jié)果類型為 Map<String,Object> Page<Map<String,Object>> page = new Page<>(1, 4); simpleMapper.selectMapsPage(page, null); System.out.println("size: " + page.getSize()); System.out.println("total: " + page.getTotal()); System.out.println("pages: " + page.getPages()); for(Map<String,Object> map : page.getRecords()) { System.out.println(map); } } }
注意:這里我們平常會(huì)使用以下代碼獲取page里邊的存放的代碼。
page.getRecords():這是用來(lái)獲取我們分頁(yè)查出來(lái)的數(shù)據(jù)
5. 最后總結(jié)
這一小結(jié),我們主要是對(duì)mybatis-pluts 插件的分頁(yè)功能的使用,做了簡(jiǎn)單介紹。下邊我們來(lái)梳理以下,使用插件步驟:
- 在我們項(xiàng)目的配置文件夾下,一定要添加MybatisPlusConfig
- 我們需要在這個(gè)配置類中添加paginationInterceptor()方法,進(jìn)行分頁(yè)功能的配置,其實(shí)就是配置分頁(yè)功能的攔截器
- 使用方法,進(jìn)來(lái)數(shù)據(jù)的分頁(yè)
- 使用方法,返回分頁(yè)的數(shù)據(jù)
以上就是一文搞懂Mybatis-plus的分頁(yè)查詢操作的詳細(xì)內(nèi)容,更多關(guān)于Mybatis-plus分頁(yè)查詢的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Mybatis-Plus 多表聯(lián)查分頁(yè)的實(shí)現(xiàn)代碼
- Mybatis-plus新版本分頁(yè)失效PaginationInterceptor過(guò)時(shí)的問(wèn)題
- MyBatis-Plus 分頁(yè)查詢以及自定義sql分頁(yè)的實(shí)現(xiàn)
- mybatis-plus分頁(yè)傳入?yún)?shù)后sql where條件沒(méi)有l(wèi)imit分頁(yè)信息操作
- mybatis-plus分頁(yè)查詢的實(shí)現(xiàn)示例
- MyBatis-Plus分頁(yè)插件不生效的解決方法
- MyBatis-Plus實(shí)現(xiàn)2種分頁(yè)方法(QueryWrapper查詢分頁(yè)和SQL查詢分頁(yè))
- MyBatis-Plus分頁(yè)時(shí)排序的實(shí)現(xiàn)方法
- 解決mybatis-plus3.4.1分頁(yè)插件PaginationInterceptor和防止全表更新與刪除插件SqlExplainInterceptor過(guò)時(shí)失效問(wèn)題
- Mybatis-Plus中分頁(yè)插件PaginationInterceptor的使用
- MyBatis-Plus 分頁(yè)插件配置的兩種方式實(shí)現(xiàn)
相關(guān)文章
如何解決java.lang.IllegalStateException: Target host&n
文章描述了通過(guò)MocoRunner模擬接口,并使用properties文件和ResourceBundle讀取配置文件進(jìn)行g(shù)et請(qǐng)求的過(guò)程,在執(zhí)行過(guò)程中遇到了目標(biāo)主機(jī)為空的錯(cuò)誤,通過(guò)檢查和修正url拼接問(wèn)題解決了該錯(cuò)誤2024-12-12Java LinkedList的實(shí)現(xiàn)原理圖文詳解
今天小編就為大家分享一篇關(guān)于Java LinkedList的實(shí)現(xiàn)原理圖文詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01Java實(shí)用工具庫(kù)commons-lang3的使用
Apache?Commons?Lang?3是一個(gè)流行的Java實(shí)用工具庫(kù),提供了對(duì)java.lang包的擴(kuò)展,包括字符串操作、正則表達(dá)式處理、數(shù)字操作、日期和時(shí)間操作、隨機(jī)字符串生成和對(duì)象操作等功能2025-03-03IDEA下SpringBoot指定環(huán)境、配置文件啟動(dòng)操作過(guò)程
這篇文章主要介紹了IDEA下SpringBoot指定環(huán)境、配置文件啟動(dòng)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08