6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java】【MyBatis】MapperXMLファイルのresultmapにて、別ファイルで定義したresultmapを参照する

6
Posted at

想定ケース

以下のように共通カラムを抽象クラス化※1し、そのクラスを継承しているエンティティクラス※2をMybatisにてマッピングする際などに、MapperXMLファイルで定義したresultMapを共通化して複数のMappaerXMLファイルで参照したい場合。

言語について本記事ではJavaを使用。
本題から逸れるのを避けるため、mybatisの導入設定やMapperインターフェースの内容といったプロジェクト詳細については省略します。

方法について

参照を行うMapperXMLファイルのresultMapタグのextends属性に対し、参照したいMapperXMLファイルの[mapperタグのnamespace属性の値].[resultMapタグのid属性の値]を指定します。

※1抽象クラス

BaseSample.java
package com.sample.mybatis.model.entity.common

public abstract class BaseSample {
    String foo;
    String bar;
}

※2継承クラス

Hoge.java
package com.sample.mybatis.model.entity

public class Hoge extends BaseSample {
    Integer hogeId;
    String hogeName;

    // ...
}
Fuga.java
package com.sample.mybatis.model.entity

public class Fuga extends BaseSample {
    Integer fugaId;
    String fugaName;

    // ...
}

MapperXMLファイルの記載方法

共通化するMapperXMLファイル

BaseColumnForUpdInfoMapper.xml
<!--
    ...
    ...
-->   
<mapper namespace="com.sample.mybatis.model.dao.BaseSampleMapper">
     <resultMap id="BaseSampleMap" type="com.sample.mybatis.model.entity.common.BaseSample">
        <result column="foo" property="foo" jdbcType="VARCHAR" />
        <result column="bar" property="bar" jdbcType="VARCHAR" />    
    </resultMap>
    <!--
        ...
        ...
    -->
</mapper>
    

参照を行うMapperXMLファイル

Hoge.xml
<!--
    ...
    ...
-->
    <resultMap id="HogeMap" type="com.sample.mybatis.model.entity.common.Hoge" extends="com.sample.mybatis.model.dao.common.BaseSampleMapper.BaseSampleMap">
        <id column="hoge_id" property="hogeId" jdbcType="INTEGER" />
        <result column="hoge_name" property="hogeName" jdbcType="VARCHAR" />
        <!--
            ...
            ...
        -->
        <!--★extends属性によりBaseSampleMapを継承するため、BaseSampleMapにて定義した以下の記述を省略してマッピングできます。
        <result column="foo" property="foo" jdbcType="VARCHAR" />
        <result column="bar" property="bar" jdbcType="VARCHAR" />    
        -->
    </resultMap>
<!--
    ...
    ...
-->
 
Fuga.xml
<!--
    ...
    ...
-->
    <resultMap id="FugaMap" type="com.sample.mybatis.model.entity.common.Fuga" extends="com.sample.mybatis.model.dao.common.BaseSampleMapper.BaseSampleMap">
        <id column="fuga_id" property="fugaId" jdbcType="INTEGER" />
        <result column="fuga_name" property="fugaName" jdbcType="VARCHAR" />
        <!--
            ...
            ...
        -->
        <!--★extends属性によりBaseSampleMapを継承するため、BaseSampleMapにて定義した以下の記述を省略してマッピングできます。
        <result column="foo" property="foo" jdbcType="VARCHAR" />
        <result column="bar" property="bar" jdbcType="VARCHAR" />    
        -->
    </resultMap>
<!--
    ...
    ...
-->

参考

https://mybatis.org/mybatis-3/ja/sqlmap-xml.html
https://www.casleydi.com/blog/engineer/231/

著者: H.M (株式会社ウィズツーワン)

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?