阅读 158

MybatisPlus关联查询的完美实现方案

我们在项目开发的时候,难免会遇到连表查询的操作,所以下面这篇文章主要给大家介绍了关于MybatisPlus关联查询的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

目录
  • Mybatis-Plus 简介

  • 连表?Left Join?Inner Join?

  • 旧版代码

  • 新版优化

  • 相关工具类

  • 总结

Mybatis-Plus 简介

什么是 MyBatis-Plus?Mybatis-Plus:为简化开发而生

MyBatis-Plus(简称 MP)是一个基于 MyBatis 的增强工具,它对 Mybatis 的基础功能进行了增强,但未做任何改变。使得我们可以可以在 Mybatis 开发的项目上直接进行升级为 Mybatis-plus,正如它对自己的定位,它能够帮助我们进一步简化开发过程,提高开发效率。

Mybatis-Plus 其实可以看作是对 Mybatis 的再一次封装,升级之后,对于单表的 CRUD 操作,调用 Mybatis-Plus 所提供的 API 就能够轻松实现,此外还提供了各种查询方式、分页等行为。最最重要的,开发人员还不用去编写 XML,这就大大降低了开发难度

官网:https://mp.baomidou.com

连表?Left Join?Inner Join?

MybtaisPlus 极大简便了单表的操作,但对应连表查询更多的还是在xml中实现,本人是一个单表主义者(能单表,尽量不连表),阿里的相关规范中也提到:尽量使用单表,连表尽量不要超过3张,于是更多的时候,是把主表查询处理,然后使用流处理把关联的表Id集合起来,再通过listByIds转toMap,得到Id对应的对象,再进行get相对应赋值,最初也没啥,当写的越来越多了之后,问题就开始暴露了,没错,代码实在是太长,太臭啦!!!

旧版代码

1
2
3
4
5
6
7
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)));
}


旧版代码太长了啦,每次关联,都需要5-7行,一张业务表少说也有3到5个表关联,这样编写的话,显得代码太过累赘,以及冗余,整体看的实在太难受啦

新版优化

1
2
Map<Long, SystemProjectFileSecurity> systemProjectFileSecurityMap =
    linkTableUtils.linkSystemProjectFileSecurity(records, SystemProjectFileSecurityGroupSetting::getSystemProjectFileSecurityId);

我们对冗余的代码进行的抽取,目前1-2行就可以实现5-7行的功能,就算关联查询更多的表,代码看起来也更整洁,嗯,真香

相关工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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   数据源列表
     * @param getKey    查询的Id
     * @param getRKey   map的key
     * @param <T>       数据源
     * @param <R>       结果
     * @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);
  
        // 得到对应的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;
        }
    }
}

总结

到此这篇关于MybatisPlus关联查询的文章就介绍到这了

原文链接:https://blog.csdn.net/itjavaee/article/details/121931218


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐