In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to implement Mybatis Plus related query". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction to Mybatis-Plus
What is MyBatis-Plus? Mybatis-Plus: created to simplify development
MyBatis-Plus (MP for short) is a MyBatis-based enhancement tool that enhances the basic functionality of Mybatis, but does not make any changes. So that we can directly upgrade to Mybatis-plus on the project developed by Mybatis, just like its own positioning, it can help us to further simplify the development process and improve development efficiency.
In fact, Mybatis-Plus can be seen as another encapsulation of Mybatis. After upgrading, for the CRUD operation of a single table, it can be easily realized by calling the API provided by Mybatis-Plus. In addition, it also provides a variety of query methods, paging and other behaviors. Most importantly, developers do not have to write XML, which greatly reduces the difficulty of development.
Official website: https://mp.baomidou.com
Even a watch? Left Join? Inner Join?
MybtaisPlus greatly simplifies the operation of a single table, but more queries corresponding to connected tables are still implemented in xml. I am a single-tabularist (can single tables, try not to connect tables as much as possible). Ali's relevant specifications also mentioned: try to use single tables, and try not to have more than 3 tables, so more often, it is to process the main table query, and then use flow processing to aggregate the associated table Id, and then transfer it to toMap through listByIds. Get the object corresponding to Id, and then assign the corresponding value to get. At first, it doesn't matter. When you write more and more, the problem begins to be exposed. Yes, the code is too long, too smelly!
The old code Set systemProjectFileSecurityIdSet = records.stream (). Map (SystemProjectFileSecurityGroupSetting::getSystemProjectFileSecurityId) .codes (Collectors.toSet ()); Map systemProjectFileSecurityMap = new HashMap (); if (! systemProjectFileSecurityIdSet.isEmpty ()) {systemProjectFileSecurityMap.putAll (systemProjectFileSecurityService.listByIds (systemProjectFileSecurityIdSet) .stream () .codes (Collectors.toMap (SystemProjectFileSecurity::getSystemProjectFileSecurityId, systemProjectFileSecurity-> systemProjectFileSecurity));}
The old code is too long, each association requires 5-7 lines, and a business table has at least 3-5 table associations. If written in this way, the code is too cumbersome and redundant, and it is too uncomfortable to look at it as a whole.
New optimized Map systemProjectFileSecurityMap = linkTableUtils.linkSystemProjectFileSecurity (records, SystemProjectFileSecurityGroupSetting::getSystemProjectFileSecurityId)
Our extraction of redundant code, currently 1-2 lines can achieve the function of 5-7 lines, even if the association query more tables, the code also looks cleaner, well, really fragrant
Related tool class 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 / * * tool classes that link other tables * * @ author zengqinghua * / @ Componentpublic class LinkTableUtils {/ * Link XXX table * / public Map linkSystemProjectFileSecurity (List records, Function getKey) {return this.linkTable (TableEnum.SystemProjectFileSecurity, records, getKey, SystemProjectFileSecurity::getSystemProjectFileSecurityId) } / * linked table (common method) * * @ param tableEnum table name enumeration * @ param records data source list * @ param getKey query Id * @ param getRKey map key * @ param data source * @ param result * @ return * / public Map linkTable (TableEnum tableEnum List records, Function getKey, Function getRKey) {if (records.isEmpty ()) {return new HashMap () } IService iService = (IService) ApplicationContextUtil.getBean (tableEnum.tClass); / / get the corresponding idSet idSet = records.stream (). Map (getKey) .yield (Collectors.toSet ()); Map map = new HashMap () If (! idSet.isEmpty ()) {map.putAll (iService.listByIds (idSet) .stream () .upload (Collectors.toMap (getRKey, data-> data));} return map;} public enum TableEnum {SystemProjectFileSecurity ("xxxx", ISystemProjectFileSecurityService.class); private final String tableRemark; private final Class tClass This is the end of TableEnum (String tableRemark, Class tClass) {this.tableRemark = tableRemark; this.tClass = tClass;}} "how to implement Mybatis Plus Association query". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.