MyBatis Plus 實(shí)現(xiàn)多表分頁查詢功能的示例代碼
在Mybatis Plus 中,雖然IService 接口幫我們定義了很多常用的方法,但這些都是 T 對象有用,如果涉及到 多表的查詢,還是需要自定義Vo 對象和自己編寫sql 語句,Mybatis Plus提供了一個Page 對象,查詢是需要設(shè)置其中的 size 字段 和 current 字段的值
一、分頁配置
可以直接使用selectPage這樣的分頁,但返回的數(shù)據(jù)確實(shí)是分頁后的數(shù)據(jù),但在控制臺打印的SQL語句其實(shí)并沒有真正的物理分頁,而是通過緩存來獲得全部數(shù)據(jù)中再進(jìn)行的分頁,這樣對于大數(shù)據(jù)量操作時是不可取的,那么接下來就敘述一下,真正實(shí)現(xiàn)物理分頁的方法。
官方在分頁插件上如是描述:自定義查詢語句分頁(自己寫sql/mapper),也就是針對自己在Mapper中寫的方法,但經(jīng)過測試,如果不配置分頁插件,其默認(rèn)采用的分頁為RowBounds的分頁即邏輯分頁,也就是先把數(shù)據(jù)記錄全部查詢出來,然在再根據(jù)offset和limit截斷記錄返回(數(shù)據(jù)量大的時候會造成內(nèi)存溢出),故而不可取,而通過分頁插件的配置即可達(dá)到物理分頁效果。
新建一個MybatisPlusConfig配置類文件,代碼如下所示:
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisPlusConfig {
/**
* mybatis-plus分頁插件<br>
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
二、使用分頁進(jìn)行單表的查詢
對于單表的分頁查詢,ServiceImpl 類已經(jīng)為我們提供了對應(yīng)的方法 selectPage(),并將結(jié)果封裝到Page 對象中:

在項(xiàng)目開發(fā)當(dāng)中,都會將分頁的一些參數(shù)封裝成一個類 PageReq(不要在意這個Req 為什么不是全大寫)->import java.io.Serializable;
public class PageReq implements Serializable {
/**
* 每頁顯示大小
*/
private long size;
/**
* 當(dāng)前頁碼
*/
private long current;
/**
* 最大頁數(shù)
*/
private long maxCurrent;
/**
* 數(shù)據(jù)總條數(shù)
*/
private long total;
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public long getCurrent() {
return current;
}
public void setCurrent(long current) {
this.current = current;
}
public long getMaxCurrent() {
return maxCurrent;
}
public void setMaxCurrent(long maxCurrent) {
this.maxCurrent = maxCurrent;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
if(size != 0){
if(total % size != 0){
maxCurrent = total / size + 1;
}else {
maxCurrent = total / size;
}
}
}
public PageReq() {
}
public PageReq(long size, long current, long total) {
this.size = size;
this.current = current;
this.total = total;
setTotal(total);
}
}
功能編寫:


執(zhí)行完之后,會將查詢的接口封裝到我們 Page的 對象中:

三、多表關(guān)聯(lián)分頁查詢
對于多表關(guān)聯(lián)的查詢時,還是需要編寫 VO 類和 手動的在Mapper.xml 中編寫sql,雖然是可以不用創(chuàng)建VO,用Map 的方式接受返回的結(jié)果,但這樣只會更麻煩,甚至VO 是很有可能在其他地方使用的
先準(zhǔn)備個VO類:

編寫Mapper接口,添加一個分頁查詢的方法
package com.eiot.e_view.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.eiot.e_view.model.req.RoomPageReq;
import com.eiot.e_view.model.vo.RoomVO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface RoomMapper extends BaseMapper<Room> {
List<RoomVO> getRoomPageList(Page page, @Param("roomPageReq")RoomPageReq roomPageReq);
}
編寫sql,和我們使用Mybatis 沒有區(qū)別:


編寫Server :

執(zhí)行結(jié)果:

總結(jié)
到此這篇關(guān)于MyBatis Plus 實(shí)現(xiàn)多表分頁查詢功能的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis Plus 多表分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于Mybatis Plus實(shí)現(xiàn)多表分頁查詢的示例代碼
- Mybatis-Plus或PageHelper多表分頁查詢總條數(shù)不對問題的解決方法
- springboot整合mybatis-plus實(shí)現(xiàn)多表分頁查詢的示例代碼
- MyBatis-Plus多表聯(lián)合查詢并且分頁(3表聯(lián)合)
- 最簡單的MyBatis Plus的多表聯(lián)接、分頁查詢實(shí)現(xiàn)方法
- mybatis-plus實(shí)現(xiàn)自定義SQL、多表查詢與多表分頁查詢語句實(shí)例
- mybatis-plus多表分頁查詢最佳實(shí)現(xiàn)方法(非常簡單)
相關(guān)文章
解決Mybatis?plus實(shí)體類屬性與表字段不一致的問題
這篇文章主要介紹了Mybatis?plus實(shí)體類屬性與表字段不一致解決方法,文末給大家提到了Mybatis-plus中數(shù)據(jù)庫表名和表字段名的相關(guān)知識,需要的朋友可以參考下2022-07-07
SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁示例
本文介紹了SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
詳解MybatisPlus中@TableLogic注解的使用
@TableLogic一般用于實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)邏輯刪除,本文我們將介紹 @TableLogic 注解的用法,以及每個屬性的實(shí)際意義和用法,感興趣的可以了解一下2022-06-06

