MybatisPlus關(guān)聯(lián)查詢的完美實(shí)現(xiàn)方案
Mybatis-Plus 簡介
什么是 MyBatis-Plus?Mybatis-Plus:為簡化開發(fā)而生
MyBatis-Plus(簡稱 MP)是一個(gè)基于 MyBatis 的增強(qiáng)工具,它對 Mybatis 的基礎(chǔ)功能進(jìn)行了增強(qiáng),但未做任何改變。使得我們可以可以在 Mybatis 開發(fā)的項(xiàng)目上直接進(jìn)行升級為 Mybatis-plus,正如它對自己的定位,它能夠幫助我們進(jìn)一步簡化開發(fā)過程,提高開發(fā)效率。
Mybatis-Plus 其實(shí)可以看作是對 Mybatis 的再一次封裝,升級之后,對于單表的 CRUD 操作,調(diào)用 Mybatis-Plus 所提供的 API 就能夠輕松實(shí)現(xiàn),此外還提供了各種查詢方式、分頁等行為。最最重要的,開發(fā)人員還不用去編寫 XML,這就大大降低了開發(fā)難度
官網(wǎng):https://mp.baomidou.com
連表?Left Join?Inner Join?
MybtaisPlus 極大簡便了單表的操作,但對應(yīng)連表查詢更多的還是在xml中實(shí)現(xiàn),本人是一個(gè)單表主義者(能單表,盡量不連表),阿里的相關(guān)規(guī)范中也提到:盡量使用單表,連表盡量不要超過3張,于是更多的時(shí)候,是把主表查詢處理,然后使用流處理把關(guān)聯(lián)的表Id集合起來,再通過listByIds轉(zhuǎn)toMap,得到Id對應(yīng)的對象,再進(jìn)行g(shù)et相對應(yīng)賦值,最初也沒啥,當(dāng)寫的越來越多了之后,問題就開始暴露了,沒錯(cuò),代碼實(shí)在是太長,太臭啦!??!
舊版代碼
Set<Long> systemProjectFileSecurityIdSet = records.stream().map(SystemProjectFileSecurityGroupSetting::getSystemProjectFileSecurityId).collect(Collectors.toSet()); Map<Long, SystemProjectFileSecurity> systemProjectFileSecurityMap = new HashMap<>(); if (!systemProjectFileSecurityIdSet.isEmpty()) { systemProjectFileSecurityMap.putAll(systemProjectFileSecurityService.listByIds(systemProjectFileSecurityIdSet) .stream() .collect(Collectors.toMap(SystemProjectFileSecurity::getSystemProjectFileSecurityId, systemProjectFileSecurity -> systemProjectFileSecurity))); }
舊版代碼太長了啦,每次關(guān)聯(lián),都需要5-7行,一張業(yè)務(wù)表少說也有3到5個(gè)表關(guān)聯(lián),這樣編寫的話,顯得代碼太過累贅,以及冗余,整體看的實(shí)在太難受啦
新版優(yōu)化
Map<Long, SystemProjectFileSecurity> systemProjectFileSecurityMap = linkTableUtils.linkSystemProjectFileSecurity(records, SystemProjectFileSecurityGroupSetting::getSystemProjectFileSecurityId);
我們對冗余的代碼進(jìn)行的抽取,目前1-2行就可以實(shí)現(xiàn)5-7行的功能,就算關(guān)聯(lián)查詢更多的表,代碼看起來也更整潔,嗯,真香
相關(guān)工具類
import com.baomidou.mybatisplus.extension.service.IService; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; /** * 鏈接其他表的工具類 * * @author zengqinghua */ @Component public class LinkTableUtils { /** * 鏈接 XXX表 */ public <T> Map<Long, SystemProjectFileSecurity> linkSystemProjectFileSecurity(List<T> records, Function<T, Long> getKey) { return this.linkTable( TableEnum.SystemProjectFileSecurity, records, getKey, SystemProjectFileSecurity::getSystemProjectFileSecurityId ); } /** * 鏈接 表(通用方法) * * @param tableEnum 表名枚舉 * @param records 數(shù)據(jù)源列表 * @param getKey 查詢的Id * @param getRKey map的key * @param <T> 數(shù)據(jù)源 * @param <R> 結(jié)果 * @return */ public <T, R> Map<Long, R> linkTable( TableEnum tableEnum, List<T> records, Function<T, Long> getKey, Function<R, Long> getRKey ) { if (records.isEmpty()) { return new HashMap<>(); } IService<R> iService = (IService) ApplicationContextUtil.getBean(tableEnum.tClass); // 得到對應(yīng)的id Set<Long> idSet = records.stream().map(getKey).collect(Collectors.toSet()); Map<Long, R> map = new HashMap<>(); if (!idSet.isEmpty()) { map.putAll(iService.listByIds(idSet) .stream() .collect(Collectors.toMap(getRKey, data -> data))); } return map; } public enum TableEnum { SystemProjectFileSecurity("xxxx", ISystemProjectFileSecurityService.class); private final String tableRemark; private final Class tClass; TableEnum(String tableRemark, Class tClass) { this.tableRemark = tableRemark; this.tClass = tClass; } } }
總結(jié)
到此這篇關(guān)于MybatisPlus關(guān)聯(lián)查詢的文章就介紹到這了,更多相關(guān)MybatisPlus關(guān)聯(lián)查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis-plus出現(xiàn)數(shù)據(jù)庫id很大或者為負(fù)數(shù)的解決
本文主要介紹了Mybatis-plus出現(xiàn)數(shù)據(jù)庫id很大或者為負(fù)數(shù)的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解
Spring?Cloud?Ribbon?是基于Netflix?Ribbon?實(shí)現(xiàn)的一套客戶端負(fù)載均衡工具,Ribbon客戶端組件提供了一系列的完善的配置,如超時(shí),重試等,這篇文章主要介紹了Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解,需要的朋友可以參考下2023-03-03SpringBoot?調(diào)用外部接口的三種實(shí)現(xiàn)方法
Spring Boot調(diào)用外部接口的方式有多種,常見的有以下三種方式:RestTemplate、Feign 和 WebClient,本文就詳細(xì)介紹一下,感興趣的可以了解一下2023-08-08IDEA導(dǎo)入eclipse項(xiàng)目并且部署到tomcat的步驟詳解
這篇文章主要給大家介紹了關(guān)于IDEA導(dǎo)入eclipse項(xiàng)目并且部署到tomcat的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02深入了解SpringAOP中的jdk動態(tài)代理與CGlib
這篇文章主要介紹了深入了解SpringAOP中的jdk動態(tài)代理與CGlib,一般我們編寫程序的思想是縱向的,也就是一個(gè)方法代碼從該方法第一行開始往下一步一步走,直到走完最后一行代碼,也就是說很多業(yè)務(wù)都需要的比如用戶鑒權(quán),資源釋放等,需要的朋友可以參考下2023-12-12spring?cloud?gateway中netty線程池小優(yōu)化
這篇文章主要介紹了spring?cloud?gateway中netty線程池小優(yōu)化技巧示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07java中isEmpty和isBlank的區(qū)別小結(jié)
Java中的isEmpty和isBlank都是用來判斷字符串是否為空的方法,但在不同的情況下有所區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09