Mybatis-Plus-Join表連接插件集成過程
概要
Mybatis-Plus-Join 是基于 Mybatis-Plus 的擴(kuò)展插件,提供了簡便的表連接查詢功能,支持左連接、右連接和內(nèi)連接等操作,同時(shí)保持 Mybatis-Plus 的使用風(fēng)格。
安裝配置
1.添加依賴
在 pom.xml 中添加以下依賴:
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>1.4.5</version>
</dependency>
2.配置繼承結(jié)構(gòu)
Mapper 層繼承
import icu.mhb.mybatisplus.plugln.base.mapper.JoinBaseMapper;
public interface UserMapper extends JoinBaseMapper<User> {
}
Service 接口繼承
import icu.mhb.mybatisplus.plugln.base.service.JoinIService;
public interface UserService extends JoinIService<User> {
}
Service 實(shí)現(xiàn)層繼承
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends JoinServiceImpl<UserMapper, User> implements UserService {
}
基本使用方法
1. 創(chuàng)建 JoinLambdaWrapper
在 Service 中可直接使用 joinLambdaWrapper() 方法:
// 方式一:通過 Service 直接獲取 JoinLambdaWrapper<User> wrapper = userService.joinLambdaWrapper(); // 方式二:手動(dòng)創(chuàng)建 JoinLambdaWrapper<User> wrapper = new JoinLambdaWrapper<>(User.class);
2. 添加表連接
支持三種連接方式:
// 左連接 wrapper.leftJoin(Department.class, Department::getId, User::getDeptId); // 右連接 wrapper.rightJoin(Department.class, Department::getId, User::getDeptId); // 內(nèi)連接 wrapper.innerJoin(Department.class, Department::getId, User::getDeptId);
3. 添加查詢條件
wrapper.eq(Department::getName, "技術(shù)部")
.like(User::getName, "張")
.gt(User::getAge, 25);
4. 結(jié)束連接并執(zhí)行查詢
// 結(jié)束當(dāng)前連接(重要?。? wrapper.end(); // 執(zhí)行查詢 List<UserVO> result = userService.joinList(wrapper, UserVO.class);
多表連接示例
public List<UserVO> findUsersWithMultiJoin() {
JoinLambdaWrapper<User> wrapper = userService.joinLambdaWrapper();
// 第一重連接:部門表
wrapper.leftJoin(Department.class, Department::getId, User::getDeptId)
.selectAs(Department::getName, UserVO::getDeptName)
.end();
// 第二重連接:職位表
wrapper.leftJoin(Position.class, Position::getUserId, User::getId)
.eq(Position::getLevel, "P7")
.selectAs(Position::getName, UserVO::getPositionName)
.end();
return userService.joinList(wrapper, UserVO.class);
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Cloud Toolkit在IDEA中極速創(chuàng)建dubbo工程
這篇文章主要介紹了使用Cloud Toolkit在IDEA中極速創(chuàng)建dubbo工程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
win10下定時(shí)運(yùn)行與開機(jī)自啟動(dòng)jar包的方法記錄
這篇文章主要給大家介紹了關(guān)于win10下定時(shí)運(yùn)行與開機(jī)自啟動(dòng)jar包的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
springboot項(xiàng)目Redis統(tǒng)計(jì)在線用戶的實(shí)現(xiàn)示例
最近做個(gè)項(xiàng)目需要統(tǒng)計(jì)在線用戶,本文主要介紹了springboot項(xiàng)目Redis統(tǒng)計(jì)在線用戶的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn)
本文主要介紹了Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
SpringBoot實(shí)現(xiàn)無限級評論回復(fù)的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot實(shí)現(xiàn)無限級評論回復(fù)的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Java中的for循環(huán)結(jié)構(gòu)及實(shí)例
這篇文章主要介紹了Java中的for循環(huán)結(jié)構(gòu)及實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

