欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis-Plus中如何使用ResultMap的方法示例

 更新時(shí)間:2021年11月23日 08:27:50   作者:字節(jié)飛揚(yáng)  
本文主要介紹了MyBatis-Plus中如何使用ResultMap,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

MyBatis-Plus (簡(jiǎn)稱(chēng)MP)是一個(gè)MyBatis的增強(qiáng)工具,在MyBatis的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生。

MyBatis-Plus對(duì)MyBatis基本零侵入,完全可以與MyBatis混合使用,這點(diǎn)很贊。

在涉及到關(guān)系型數(shù)據(jù)庫(kù)增刪查改的業(yè)務(wù)時(shí),我比較喜歡用MyBatis-Plus,開(kāi)發(fā)效率極高。具體的使用可以參考官網(wǎng),或者自己上手摸索感受一下。

下面簡(jiǎn)單總結(jié)一下在MyBatis-Plus中如何使用ResultMap。

問(wèn)題說(shuō)明

先看個(gè)例子:

有如下兩張表:

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關(guān)聯(lián)tb_book表的id。

下面先看Hero實(shí)體類(lèi)的代碼,如下:

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;

    // *********************************
    // 數(shù)據(jù)庫(kù)表中不存在以下字段(表join時(shí)會(huì)用到)
    // *********************************

    @TableField(value = "book_name", exist = false)
    private String bookName;

    @TableField(value = "author", exist = false)
    private String author;
}

注意了,我特地把tb_hero表中的bid字段映射成實(shí)體類(lèi)Hero中的bookId屬性。

測(cè)試BaseMapper中內(nèi)置的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)

沒(méi)毛病, MyBatis-Plus會(huì)根據(jù)@TableField指定的映射關(guān)系,生成對(duì)應(yīng)的SQL。

測(cè)試BaseMapper中內(nèi)置的selectById()方法或者IService中的getById()方法

MyBatis-Plus打印出的SQL為:

==> Preparing: SELECT id,"name","age","skill","bid" AS bookId FROM tb_hero WHERE id=?

==> Parameters: 1(Long)

也沒(méi)毛病,可以看到生成的SELECT中把bid做了別名bookId。

測(cè)試自己寫(xiě)的SQL

比如現(xiàn)在我想連接tb_hero與tb_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);

}

查詢(xún)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沒(méi)啥問(wèn)題,過(guò)濾與分頁(yè)也都正常,但是此時(shí)你會(huì)發(fā)現(xiàn)bookId屬性為null,如下:

為什么呢?

調(diào)用BaseMapper中內(nèi)置的selectById()方法并沒(méi)有出現(xiàn)這種情況?????

回過(guò)頭來(lái)再對(duì)比一下在HeroMapper中自己定義的查詢(xún)與MyBatis-Plus自帶的selectById()有啥不同,還記得上面的剛剛的測(cè)試嗎,生成的SQL有啥不同?

原來(lái),MyBatis-Plus為BaseMapper中內(nèi)置的方法生成SQL時(shí),會(huì)把SELECT子句中bid做別名bookId,而自己寫(xiě)的查詢(xún)MyBatis-Plus并不會(huì)幫你修改SELECT子句,也就導(dǎo)致bookId屬性為null。

解決方法

方案一:表中的字段與實(shí)體類(lèi)的屬性嚴(yán)格保持一致(字段有下劃線則屬性用駝峰表示)

在這里就是tb_hero表中的bid字段映射成實(shí)體類(lèi)Hero中的bid屬性。這樣當(dāng)然可以解決問(wèn)題,但不是本篇講的重點(diǎn)。

方案二:把自己寫(xiě)的SQL中bid做別名bookId

方案三:使用@ResultMap,這是此篇的重點(diǎn)

在@TableName設(shè)置autoResultMap = true

@TableName(value = "tb_hero", autoResultMap = true)
public class Hero {
}

然后在自定義查詢(xún)中添加@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);

}

這樣,也能解決問(wèn)題。

下面簡(jiǎn)單看下源碼,@ResultMap("mybatis-plus_實(shí)體類(lèi)名")怎么來(lái)的。

詳情見(jiàn): com.baomidou.mybatisplus.core.metadata.TableInfo#initResultMapIfNeed()

/**
 * 自動(dòng)構(gòu)建 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的構(gòu)成,你應(yīng)該可以明白。

思考: 這種方式的ResultMap默認(rèn)是強(qiáng)綁在一個(gè)@TableName上的,如果是某個(gè)聚合查詢(xún)或者查詢(xún)的結(jié)果并非對(duì)應(yīng)一個(gè)真實(shí)的表怎么辦呢?有沒(méi)有更優(yōu)雅的方式?

自定義@AutoResultMap注解

基于上面的思考,我做了下面簡(jiǎn)單的實(shí)現(xiàn):

自定義@AutoResultMap注解

import java.lang.annotation.*;

/**
 * 使用@AutoResultMap注解的實(shí)體類(lèi)
 * 自動(dòng)生成{auto.mybatis-plus_類(lèi)名}為id的resultMap
 * {@link MybatisPlusConfig#initAutoResultMap()}
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AutoResultMap {

}

動(dòng)時(shí)掃描@AutoResultMap注解的實(shí)體類(lèi)

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;

    /**
     * 分頁(yè)插件(根據(jù)jdbcUrl識(shí)別出數(shù)據(jù)庫(kù)類(lèi)型, 自動(dòng)選擇適合該方言的分頁(yè)插件)
     * 相關(guān)使用說(shuō)明: 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注解的實(shí)體類(lèi)自動(dòng)構(gòu)建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()) {
                    // 設(shè)置 tableInfo的autoInitResultMap屬性 為 true
                    ReflectUtil.setFieldValue(tableInfo, "autoInitResultMap", true);
                    // 調(diào)用 tableInfo#initResultMapIfNeed() 方法,自動(dòng)構(gòu)建 resultMap 并注入
                    ReflectUtil.invoke(tableInfo, "initResultMapIfNeed");
                }
            }

            log.info("--- finish register @AutoResultMap ---");
        } catch (Throwable e) {
            log.error("initAutoResultMap error", e);
            System.exit(1);
        }
    }
}

關(guān)鍵代碼其實(shí)沒(méi)有幾行,耐心看下應(yīng)該不難懂。

使用@AutoResultMap注解

還是用例子來(lái)說(shuō)明更直觀。

下面是一個(gè)聚合查詢(xún):

@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的定義如下,在實(shí)體類(lèi)上使用了@AutoResultMap注解:

@Getter
@Setter
@NoArgsConstructor
@AutoResultMap
public class BookAgg {
    @TableId("id")
    private Long bookId;
    @TableField("name")
    private String bookName;
    @TableField("hero_ids")
    private Object heroIds;
}

完整代碼見(jiàn): https://github.com/bytesfly/springboot-demo/tree/master/springboot-mybatis-plus

到此這篇關(guān)于MyBatis-Plus中如何使用ResultMap的方法示例的文章就介紹到這了,更多相關(guān)MyBatis-Plus使用ResultMap內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法

    Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法

    eclipse工程前面出現(xiàn)紅色嘆號(hào)都是由于eclipse項(xiàng)目、eclipse工程中,缺少了一些jar包等文件引起的,這篇文章主要給大家介紹了關(guān)于Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法,需要的朋友可以參考下
    2023-11-11
  • 關(guān)于@JsonProperty,@NotNull,@JsonIgnore的具體使用

    關(guān)于@JsonProperty,@NotNull,@JsonIgnore的具體使用

    這篇文章主要介紹了關(guān)于@JsonProperty,@NotNull,@JsonIgnore的具體使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 搭建簡(jiǎn)單的Spring-Data JPA項(xiàng)目

    搭建簡(jiǎn)單的Spring-Data JPA項(xiàng)目

    本文主要介紹了搭建簡(jiǎn)單的Spring-Data JPA項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • java IO流 之 輸出流 OutputString()的使用

    java IO流 之 輸出流 OutputString()的使用

    這篇文章主要介紹了java IO流 之 輸出流 OutputString()的使用的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • Java設(shè)計(jì)模式之模板方法詳解

    Java設(shè)計(jì)模式之模板方法詳解

    模板方法的概念:定義了一個(gè)操作中的算法的骨架,而將部分步驟的實(shí)現(xiàn)在子類(lèi)中完成。模板方法模式使得子類(lèi)可以不改變一個(gè)算法的結(jié)構(gòu)即可重定義該算法的某些特定步驟
    2022-07-07
  • mybatis-plus中的常用注解

    mybatis-plus中的常用注解

    這篇文章主要介紹了mybatis-plus中的常用注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Spring Boot 注解方式自定義Endpoint詳解

    Spring Boot 注解方式自定義Endpoint詳解

    這篇文章主要介紹了Spring Boot注解方式自定義Endpoint詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java抽象類(lèi)的構(gòu)造模板模式用法示例

    Java抽象類(lèi)的構(gòu)造模板模式用法示例

    這篇文章主要介紹了Java抽象類(lèi)的構(gòu)造模板模式用法,結(jié)合實(shí)例形式分析了java使用抽象類(lèi)構(gòu)造模板模式相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • 如何在java中正確使用注釋

    如何在java中正確使用注釋

    在編寫(xiě)程序時(shí),經(jīng)常需要添加一些注釋,用以描述某段代碼的作用。 一般來(lái)說(shuō),對(duì)于一份規(guī)范的程序源代碼而言,注釋?xiě)?yīng)該占到源代碼的 1/3 以上。下面我們來(lái)詳細(xì)了解一下吧
    2019-06-06
  • 解決使用httpclient傳遞json數(shù)據(jù)亂碼的問(wèn)題

    解決使用httpclient傳遞json數(shù)據(jù)亂碼的問(wèn)題

    這篇文章主要介紹了解決使用httpclient傳遞json數(shù)據(jù)亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01

最新評(píng)論