MyBatis-Plus中如何使用ResultMap

MyBatis-Plus (简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。

MyBatis-PlusMyBatis基本零侵入,完全可以与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 {

<span class="hljs-meta">@TableId("id")</span>
<span class="hljs-keyword">private</span> Long id;

<span class="hljs-meta">@TableField(value = "name", keepGlobalFormat = true)</span>
<span class="hljs-keyword">private</span> String name;

<span class="hljs-meta">@TableField(value = "age", keepGlobalFormat = true)</span>
<span class="hljs-keyword">private</span> Integer age;

<span class="hljs-meta">@TableField(value = "skill", keepGlobalFormat = true)</span>
<span class="hljs-keyword">private</span> String skill;

<span class="hljs-meta">@TableField(value = "bid", keepGlobalFormat = true)</span>
<span class="hljs-keyword">private</span> Long bookId;

<span class="hljs-comment">// *********************************</span>
<span class="hljs-comment">// 数据库表中不存在以下字段(表join时会用到)</span>
<span class="hljs-comment">// *********************************</span>

<span class="hljs-meta">@TableField(value = "book_name", exist = false)</span>
<span class="hljs-keyword">private</span> String bookName;

<span class="hljs-meta">@TableField(value = "author", exist = false)</span>
<span class="hljs-keyword">private</span> 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_herotb_book这两张表,如下:

@Mapper
@Repository
public interface HeroMapper extends BaseMapper<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<Hero> pageQueryHero(@Param(Constants.WRAPPER) Wrapper<Hero> queryWrapper,
                              Page<Hero> 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-PlusBaseMapper中内置的方法生成 SQL 时,会把SELECT子句中bid做别名bookId,而自己写的查询MyBatis-Plus并不会帮你修改SELECT子句,也就导致bookId属性为null

解决方法#

  • 方案一:表中的字段与实体类的属性严格保持一致 (字段有下划线则属性用驼峰表示)

在这里就是tb_hero表中的bid字段映射成实体类Hero中的bid属性。这样当然可以解决问题,但不是本篇讲的重点。

  • 方案二:把自己写的SQLbid做别名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<Hero> {
@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<Hero> pageQueryHero(@Param(Constants.WRAPPER) Wrapper<Hero> queryWrapper,
Page<Hero> 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<ResultMapping> 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 ---");

       <span class="hljs-type">String</span> <span class="hljs-variable">namespace</span> <span class="hljs-operator">=</span> <span class="hljs-string">"auto"</span>;
      
       <span class="hljs-type">String</span> <span class="hljs-variable">packageName</span> <span class="hljs-operator">=</span> <span class="hljs-string">"com.bytesfly.mybatis.model.db.resultmap"</span>;
       Set&lt;Class&lt;?&gt;&gt; classes = ClassUtil.scanPackageByAnnotation(packageName, AutoResultMap.class);
      
       org.apache.ibatis.session.<span class="hljs-type">Configuration</span> <span class="hljs-variable">configuration</span> <span class="hljs-operator">=</span> sqlSessionTemplate.getConfiguration();
      
       <span class="hljs-keyword">for</span> (Class clazz : classes) {
           <span class="hljs-type">MapperBuilderAssistant</span> <span class="hljs-variable">assistant</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">MapperBuilderAssistant</span>(configuration, <span class="hljs-string">""</span>);
           assistant.setCurrentNamespace(namespace);
           <span class="hljs-type">TableInfo</span> <span class="hljs-variable">tableInfo</span> <span class="hljs-operator">=</span> TableInfoHelper.initTableInfo(assistant, clazz);
      
           <span class="hljs-keyword">if</span> (!tableInfo.isAutoInitResultMap()) {
               <span class="hljs-comment">// 设置 tableInfo的autoInitResultMap属性 为 true</span>
               ReflectUtil.setFieldValue(tableInfo, <span class="hljs-string">"autoInitResultMap"</span>, <span class="hljs-literal">true</span>);
               <span class="hljs-comment">// 调用 tableInfo#initResultMapIfNeed() 方法,自动构建 resultMap 并注入</span>
               ReflectUtil.invoke(tableInfo, <span class="hljs-string">"initResultMapIfNeed"</span>);
           }
       }
      
       log.info(<span class="hljs-string">"--- finish register @AutoResultMap ---"</span>);
      

      } catch (Throwable e) {
      log.error("initAutoResultMap error", e);
      System.exit(1);
      }
      }
      }

关键代码其实没有几行,耐心看下应该不难懂。

  • 使用 @AutoResultMap 注解

还是用例子来说明更直观。

下面是一个聚合查询:

@Mapper
@Repository
public interface BookMapper extends BaseMapper<Book> {
<span class="hljs-meta">@ResultMap("auto.mybatis-plus_BookAgg")</span>
<span class="hljs-meta">@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"})</span>
List&lt;BookAgg&gt; <span class="hljs-title function_">agg</span><span class="hljs-params">()</span>;

}

其中BookAgg的定义如下,在实体类上使用了@AutoResultMap注解:

@Getter
@Setter
@NoArgsConstructor
@AutoResultMap
public class BookAgg {
<span class="hljs-meta">@TableId("id")</span>
<span class="hljs-keyword">private</span> Long bookId;

<span class="hljs-meta">@TableField("name")</span>
<span class="hljs-keyword">private</span> String bookName;

<span class="hljs-meta">@TableField("hero_ids")</span>
<span class="hljs-keyword">private</span> Object heroIds;

}

完整代码见: https://github.com/bytesfly/springboot-demo/tree/master/springboot-mybatis-plus