MyBatisPlus中如何使用ResultMap
作者:字节飞扬
原文链接:https://www.cnblogs.com/bytesfly/p/resultmap-in-mybatis-plus.html
MyBatis-Plus (简称 MP )是一个MyBatis 的增强工具,在MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MyBatis-Plus 对MyBatis 基本零侵入,完全可以与MyBatis 混合使用,这点很赞。
在涉及到关系型数据库增删查改的业务时,我比较喜欢用 MyBatis-Plus ,开发效率极高。具体的使用可以参考官网,或者自己上手摸索感受一下。
下面简单总结一下在 MyBatis-Plus 中如何使用ResultMap 。问题说明#
先看个例子:
有如下两张表: create table tb_book ( id bigint primary key, name varchar(32), author varchar(20) ); create table tb_hero ( id bigint primary key, name varchar(32), age int, skill varchar(32), bid bigint );
其中, tb_hero 中的bid 关联tb_book 表的id 。
下面先看 Hero 实体类的代码,如下:import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @Getter @Setter @NoArgsConstructor @TableName("tb_hero") @JsonInclude(JsonInclude.Include.NON_NULL) public class Hero { @TableId("id") private Long id; @TableField(value = "name", keepGlobalFormat = true) private String name; @TableField(value = "age", keepGlobalFormat = true) private Integer age; @TableField(value = "skill", keepGlobalFormat = true) private String skill; @TableField(value = "bid", keepGlobalFormat = true) private Long bookId; // ********************************* // 数据库表中不存在以下字段(表join时会用到) // ********************************* @TableField(value = "book_name", exist = false) private String bookName; @TableField(value = "author", exist = false) private String author; }
注意了,我特地把 tb_hero 表中的bid 字段映射成实体类Hero 中的bookId 属性。测试 BaseMapper 中内置的insert() 方法或者IService 中的save() 方法
MyBatis-Plus 打印出的SQL 为:==> Preparing: INSERT INTO tb_hero ( id, "name", "age", "skill", "bid" ) VALUES ( ?, ?, ?, ?, ? ) ==> Parameters: 1589788935356416(Long), 阿飞(String), 18(Integer), 天下第一快剑(String), 1(Long)
没毛病, MyBatis-Plus 会根据@TableField 指定的映射关系,生成对应的SQL 。测试 BaseMapper 中内置的selectById() 方法或者IService 中的getById() 方法
MyBatis-Plus 打印出的SQL 为:==> Preparing: SELECT id,"name","age","skill","bid" AS bookId FROM tb_hero WHERE id=? ==> Parameters: 1(Long)
也没毛病,可以看到生成的 SELECT 中把bid 做了别名bookId 。测试自己写的SQL
比如现在我想连接 tb_hero 与tb_book 这两张表,如下:@Mapper @Repository public interface HeroMapper extends BaseMapper { @Select({"SELECT tb_hero.*, tb_book.name as book_name, tb_book.author" + " FROM tb_hero" + " LEFT JOIN tb_book" + " ON tb_hero.bid = tb_book.id" + " ${ew.customSqlSegment}"}) IPage pageQueryHero(@Param(Constants.WRAPPER) Wrapper queryWrapper, Page page); }
查询 MyBatis-Plus 打印出的SQL 为:==> Preparing: SELECT tb_hero.*, tb_book.name AS book_name, tb_book.author FROM tb_hero LEFT JOIN tb_book ON tb_hero.bid = tb_book.id WHERE ("bid" = ?) ORDER BY id ASC LIMIT ? OFFSET ? ==> Parameters: 2(Long), 1(Long), 1(Long)
SQL没啥问题,过滤与分页也都正常,但是此时你会发现 bookId 属性为null ,如下:
为什么呢?
调用 BaseMapper 中内置的selectById() 方法并没有出现这种情况啊???
回过头来再对比一下在 HeroMapper 中自己定义的查询与MyBatis-Plus 自带的selectById() 有啥不同,还记得上面的刚刚的测试吗,生成的SQL有啥不同?
原来, MyBatis-Plus 为BaseMapper 中内置的方法生成SQL时,会把SELECT 子句中bid 做别名bookId ,而自己写的查询MyBatis-Plus 并不会帮你修改SELECT 子句,也就导致bookId 属性为null 。解决方法#方案一:表中的字段与实体类的属性严格保持一致(字段有下划线则属性用驼峰表示)
在这里就是 tb_hero 表中的bid 字段映射成实体类Hero 中的bid 属性。这样当然可以解决问题,但不是本篇讲的重点。方案二:把自己写的 SQL 中bid 做别名bookId 方案三:使用 @ResultMap ,这是此篇的重点
在 @TableName 设置autoResultMap = true @TableName(value = "tb_hero", autoResultMap = true) public class Hero { }
然后在自定义查询中添加 @ResultMap 注解,如下:import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.ResultMap; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Mapper @Repository public interface HeroMapper extends BaseMapper { @ResultMap("mybatis-plus_Hero") @Select({"SELECT tb_hero.*, tb_book.name as book_name, tb_book.author" + " FROM tb_hero" + " LEFT JOIN tb_book" + " ON tb_hero.bid = tb_book.id" + " ${ew.customSqlSegment}"}) IPage pageQueryHero(@Param(Constants.WRAPPER) Wrapper queryWrapper, Page page); }
这样,也能解决问题。
下面简单看下源码, @ResultMap("mybatis-plus_实体类名") 怎么来的。
详情见: com.baomidou.mybatisplus.core.metadata.TableInfo#initResultMapIfNeed() /** * 自动构建 resultMap 并注入(如果条件符合的话) */ void initResultMapIfNeed() { if (autoInitResultMap && null == resultMap) { String id = currentNamespace + DOT + MYBATIS_PLUS + UNDERSCORE + entityType.getSimpleName(); List resultMappings = new ArrayList<>(); if (havePK()) { ResultMapping idMapping = new ResultMapping.Builder(configuration, keyProperty, StringUtils.getTargetColumn(keyColumn), keyType) .flags(Collections.singletonList(ResultFlag.ID)).build(); resultMappings.add(idMapping); } if (CollectionUtils.isNotEmpty(fieldList)) { fieldList.forEach(i -> resultMappings.add(i.getResultMapping(configuration))); } ResultMap resultMap = new ResultMap.Builder(configuration, id, entityType, resultMappings).build(); configuration.addResultMap(resultMap); this.resultMap = id; } }
注意看上面的字符串 id 的构成,你应该可以明白。
思考: 这种方式的 ResultMap 默认是强绑在一个@TableName 上的,如果是某个聚合查询或者查询的结果并非对应一个真实的表怎么办呢?有没有更优雅的方式?自定义@AutoResultMap注解#
基于上面的思考,我做了下面简单的实现: 自定义@AutoResultMap注解 import java.lang.annotation.*; /** * 使用@AutoResultMap注解的实体类 * 自动生成{auto.mybatis-plus_类名}为id的resultMap * {@link MybatisPlusConfig#initAutoResultMap()} */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface AutoResultMap { } 启动时扫描@AutoResultMap注解的实体类 package com.bytesfly.mybatis.config; import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.ReflectUtil; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils; import com.bytesfly.mybatis.annotation.AutoResultMap; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.builder.MapperBuilderAssistant; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.annotation.PostConstruct; import java.util.Set; /** * 可添加一些插件 */ @Configuration @EnableTransactionManagement(proxyTargetClass = true) @MapperScan(basePackages = "com.bytesfly.mybatis.mapper") @Slf4j public class MybatisPlusConfig { @Autowired private SqlSessionTemplate sqlSessionTemplate; /** * 分页插件(根据jdbcUrl识别出数据库类型, 自动选择适合该方言的分页插件) * 相关使用说明: https://baomidou.com/guide/page.html */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(DataSourceProperties dataSourceProperties) { String jdbcUrl = dataSourceProperties.getUrl(); DbType dbType = JdbcUtils.getDbType(jdbcUrl); MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbType)); return interceptor; } /** * @AutoResultMap注解的实体类自动构建resultMap并注入 */ @PostConstruct public void initAutoResultMap() { try { log.info("--- start register @AutoResultMap ---"); String namespace = "auto"; String packageName = "com.bytesfly.mybatis.model.db.resultmap"; Set> classes = ClassUtil.scanPackageByAnnotation(packageName, AutoResultMap.class); org.apache.ibatis.session.Configuration configuration = sqlSessionTemplate.getConfiguration(); for (Class clazz : classes) { MapperBuilderAssistant assistant = new MapperBuilderAssistant(configuration, ""); assistant.setCurrentNamespace(namespace); TableInfo tableInfo = TableInfoHelper.initTableInfo(assistant, clazz); if (!tableInfo.isAutoInitResultMap()) { // 设置 tableInfo的autoInitResultMap属性 为 true ReflectUtil.setFieldValue(tableInfo, "autoInitResultMap", true); // 调用 tableInfo#initResultMapIfNeed() 方法,自动构建 resultMap 并注入 ReflectUtil.invoke(tableInfo, "initResultMapIfNeed"); } } log.info("--- finish register @AutoResultMap ---"); } catch (Throwable e) { log.error("initAutoResultMap error", e); System.exit(1); } } }
关键代码其实没有几行,耐心看下应该不难懂。 使用@AutoResultMap注解
还是用例子来说明更直观。
下面是一个聚合查询: @Mapper @Repository public interface BookMapper extends BaseMapper { @ResultMap("auto.mybatis-plus_BookAgg") @Select({"SELECT tb_book.id, max(tb_book.name) as name, array_agg(distinct tb_hero.id order by tb_hero.id asc) as hero_ids" + " FROM tb_hero" + " INNER JOIN tb_book" + " ON tb_hero.bid = tb_book.id" + " GROUP BY tb_book.id"}) List agg(); }
其中 BookAgg 的定义如下,在实体类上使用了@AutoResultMap 注解:@Getter @Setter @NoArgsConstructor @AutoResultMap public class BookAgg { @TableId("id") private Long bookId; @TableField("name") private String bookName; @TableField("hero_ids") private Object heroIds; }
你的iPhone上安装了哪些优秀的App?这11个iPhone上超级出色的良心APP,好几个iPhone自带的优秀软件,原来iPhone自带的软件竟然可以这么好用!以前真的是没发掘到!用完直呼相见恨晚!1微手帐(iOSAn
c入门教程(十三)委托委托是一个方法的签名,它规定了方法的返回类型,参数的个数和类型。委托的作用是可以把方法当成参数一样进行传递。定义publicdelegatevoidToDo()委托由访问级别关键词
电商之利弊电商优化了生产要素,提高了生产效率,相当于提高了生产力,比如原来全国的人民一年需1亿台电视机,从材料,到组装到分销渠道共需一百万工人,电视机一台一万。现在电商从分销渠道上少用五十万
电脑一直嘟嘟嘟响是什么问题相信也有不少用户遇到过笔记本出现嘟嘟的声音的情况,出现这样的问题会对我们使用电脑造成一定的影响和困扰。那么我们应该如何解决出现嘟嘟声的问题呢?接下来就为大家来介绍一个小妙招,教教大
英特尔订购全球最先进芯片制造机,为追赶台积电财经网科技1月19日讯,据新浪科技报道,为了从台积电手中夺回全球最先进芯片制造商的地位,英特尔今日向荷兰光刻机制造商阿斯麦(ASML)订购了一款最先进的芯片制造机(光刻机)。这款光
邓丽君现场清唱甜蜜蜜!微软奈飞等巨头纷纷入局,又一风口来了?(央视财经经济信息联播)近来,元宇宙概念可谓是火爆网络。虚拟的世界个性化的人物和沉浸式的场景,都吸引了不少的关注。在元宇宙里可以有怎样的体验?生活方式会有哪些改变呢?记者走入元宇宙
微软收购暴雪索尼股价暴跌市值一日蒸发200亿美元!丨一只热股微软宣布以687亿美元的价格收购游戏开发商动视暴雪后,市场出现了剧烈震动。微软在游戏界的主要竞争对手索尼尤其受挫。1月19日,索尼股价暴跌13,市值一天之内减少约200亿美元。微软
Oracle不同数据库之间同步处理方案背景项目中遇到的问题,需要二区两台数据库之间同步一些表,以及导出sql文件同步至三区数据库。1SQL文件目录新建SQL文件生成的目录DmptmsudataDNLTBDIR。二区数据
想换电动车,新日和雅迪哪个更好?选新日是必须的,好品质选新日,新日是电动车制造的黄浦军校,试问爱玛,雅迪等前十位电车制造商,哪家没有从新日高薪挖宝,亚运会,奥运会,世博会,指定用车是新日,还是上海主板上市公司,我
全球最大比特币基金较比特币折价近30创出纪录Grayscale比特币信托基金成为了加密货币遭遇抛售的最大受害者之一。这个规模达到270亿美元的基金(代码GBTC)2022年迄今下跌了近27,大于比特币近9的跌幅。根据彭博的数
中信证券新能源汽车换电行业销量五年有望提升十倍以上3060大背景下,换电成为推动新能源汽车全场景普及的必然形式,是充电的有效补充。我们预计,在政策资本自身产品力提升下,2022年有望成为换电行业高速发展的元年,全年有望新增换电站超