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 {

    @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_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 ---");

            String namespace = "auto";

            String packageName = "com.bytesfly.mybatis.model.db.resultmap";
            Set<Class<?>> 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<Book> {

    @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<BookAgg> 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;
}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章