欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-18 16:29:36
目录mybatis-plus 简介连表?left join?inner join?旧版代码新版优化相关工具类总结mybatis-plus 简介什么是 mybatis-plus?mybatis-plus...

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相对应赋值,最初也没啥,当写的越来越多了之后,问题就开始暴露了,没错,代码实在是太长,太臭啦!!!

旧版代码

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个表关联,这样编写的话,显得代码太过累赘,以及冗余,整体看的实在太难受啦

新版优化

map<long, systemprojectfilesecurity> systemprojectfilesecuritymap = 
    linktableutils.linksystemprojectfilesecurity(records, systemprojectfilesecuritygroupsetting::getsystemprojectfilesecurityid);

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

相关工具类

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关联查询的文章就介绍到这了,更多相关mybatisplus关联查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: mybatisplus 关联