什么是 mybatis-plus?mybatis-plus:为简化开发而生
mybatis-plus(简称 mp)是一个基于 mybatis 的增强工具,它对 mybatis 的基础功能进行了增强,但未做任何改变。使得我们可以可以在 mybatis 开发的项目上直接进行升级为 mybatis-plus,正如它对自己的定位,它能够帮助我们进一步简化开发过程,提高开发效率。
mybatis-plus 其实可以看作是对 mybatis 的再一次封装,升级之后,对于单表的 crud 操作,调用 mybatis-plus 所提供的 api 就能够轻松实现,此外还提供了各种查询方式、分页等行为。最最重要的,开发人员还不用去编写 xml,这就大大降低了开发难度
官网:https://mp.baomidou.com
mybtaisplus 极大简便了单表的操作,但对应连表查询更多的还是在xml中实现,本人是一个单表主义者(能单表,尽量不连表),阿里的相关规范中也提经典搞笑短信到:尽量使用单表,连表尽量不要超过3张,于是更多的时候,是把主表查询处理李明浩的女友,然后使用流处理把关联的表id集合起来,再通过listbyids转tomap,得到id对应的对象,再进行get相对应赋值,最初也没啥,当写的越来越多了之后,问题就开始暴露了,没错,代码实在是太长,太臭啦!!!
t<long> systemprojectfilecurityidt = records.stream().map(systemprojectfilecuritygrouptting::getsystemprojectfilecurityid).collect(collectors.tot());map<long, systemprojectfilecurity> systemprojectfilecuritymap = new hashmap<>();if (!systemprojectfilecurityidt.impty()) { systemprojectfilecuritymap.putall(systemprojectfilecurityrvice.listbyid人生如戏英文s(systemprojectfilecurityidt) .stream() .collect(collectors.tomap(systemprojectfilecurity::getsystemprojectfilecurityid, systemprojectfilecurity -> systemprojectfilecurity)));}
旧版代码太长了啦,每次关联,都需要5-7行,一张业务表少说也有3到5个表关联,这样编写的话,显得代码太过累赘,以及冗余,整体看的实在太难受啦
map<long, systemprojectfilecurity> systemprojectfilecuritymap = linktableutils.linksystemprojectfilecurity(records, systemprojectfilecuritygrouptting::getsystemprojectfilecurityid);
我们对冗余的代码进行的抽取,目前1-2行就可以实现5-7行的功能,就算关联查询更多的表,代码看起来也更整洁,嗯,真香
import com.baomidou.mybatisplus.extension.rvice.irvice;import org.sp越俎代庖造句ringframework.stereotype.component; import java.util.hashmap;import java.util.list;import java.util.map;import java.util.t;import java.util.function.function;import java.util.stream.collectors; /** * 链接其他表的工具类 * * @author zengqinghua */@componentpublic class linktableutils { /** * 链接 xxx表 */ public <t> map<long, systemprojectfilecurity> linksystemprojectfilecurity(list<t> records, function<t, long> getkey) { return this.linktable( tableenum.systemprojectfilecurity, records, getkey, systemprojectfilecurity::getsystemprojectfilecurityid ); } /** * 链接 表(通用方法) * * @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.impty()) { return new hashmap<>(); } irvice<r> irvice = (irvice) applicationcontextutil.getbean(tableenum.tclass); // 得到对应的id t<long> idt = records.stream().map(getkey).collect(collectors.tot()); map<long, r> map = new hashmap<>(); if (!idt.impty()) { map.putall(irvice.listbyids(idt) .stream() .collect(collectors.tomap(getrkey, data -> data))); } return map; } public enum tableenum { systemprojectfilecurity("xxxx", isystemprojectfilecurityrvice.class); private final string tableremark; private final class tclass; tableenum(string tableremark, class tclass) { this.tableremark = tableremark; this.tclass = tclass; } }}
到此这篇关于mybatisplus关联查询的文章就介绍到这了,更多相自强的事例关mybatisplus关联查询内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 03:09:11,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/29fe6772d26e28ab45df0995cd79100d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:MybatisPlus关联查询的完美实现方案.doc
本文 PDF 下载地址:MybatisPlus关联查询的完美实现方案.pdf
留言与评论(共有 0 条评论) |