LoginSignup
1
3

More than 5 years have passed since last update.

mybatis-spring-boot-starter 1.2の変更点

Last updated at Posted at 2017-01-14

2017年1月2日のリリースされた1.2の変更点をまとめました。

なお、

  • mybatis-spring-boot-starterの使い方については、こちら(バージョン1.2対応済み)
  • mybatis-spring-boot-starter 1.0から1.1の変更点については、こちら

をご覧下さい。

Note:

2017/4/10: 追記
バージョン1.2.1の変更点を追加しました。

2017/8/21: 追記
バージョン1.2.2の変更点を追加しました。

依存ライブラリの必須バージョン

バージョン1.2を使う場合は、以下のバージョンが必須になります。

  • MyBatis 3.4+
  • Mybatis-Spring 1.3+
  • Spring 4.3+

Spring 4.3+(Spring Boot 1.4+)が必須

バージョン1.2では、Spring 4.3から追加されたObjectProviderを利用して自動コンフィギュレーションを行うようになっているため、Spring 4.3ベースのSpring Boot 1.4+が必須になります。
なお、Mapperファイルの読み込み時にワイルドカード(例:mybatis.mapper-locations=classpath*:**/mapper/*.xml)を使用している場合は、Spring Boot 1.4.3+(Spring 4.3.5+)を使わないとMapperファイルが重複して読み込む不具合に遭遇する可能性があります(gh-108)。

Note:
Spring 4.2(Spring Boot 1.3)上で使う場合は、バージョン1.1.1を利用してください。

ライブラリバージョン

バージョン1.2では、以下のライブラリのバージョンが更新されています。

ライブラリ名 1.1.1のバージョン 1.2.0のバージョン 1.2.1のバージョン 1.2.2のバージョン
MyBatis 3.4.0 3.4.2 3.4.4 3.4.5
MyBatis Spring 1.3.0 1.3.1 1.3.1 1.3.1
Spring Boot 1.3.3.RELEASE 1.4.3.RELEASE 1.4.5.RELEASE 1.4.7.RELEASE

アーティファクトの依存構造

バージョン1.2では、mybatis-spring-boot-starter及びmybatis-spring-boot-autoconfigureの依存構造の見直しが行われています。これは、Spring Bootのコアコミッターの一人であるStéphane Nicoll氏からの指摘を受けての対応になります。

具体的には・・・バージョン1.1では、

[INFO] +- org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:1.1.1:compile
[INFO] |  \- org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:jar:1.1.1:compile
[INFO] |     +- org.mybatis:mybatis:jar:3.4.0:compile
[INFO] |     +- org.mybatis:mybatis-spring:jar:1.3.0:compile
[INFO] |     +- org.springframework.boot:spring-boot:jar:1.4.3.RELEASE:compile
[INFO] |     |  \- org.springframework:spring-context:jar:4.3.5.RELEASE:compile
[INFO] |     |     +- org.springframework:spring-aop:jar:4.3.5.RELEASE:compile
[INFO] |     |     +- org.springframework:spring-beans:jar:4.3.5.RELEASE:compile
[INFO] |     |     \- org.springframework:spring-expression:jar:4.3.5.RELEASE:compile
[INFO] |     +- org.springframework.boot:spring-boot-autoconfigure:jar:1.4.3.RELEASE:compile
[INFO] |     \- org.springframework.boot:spring-boot-starter-jdbc:jar:1.4.3.RELEASE:compile
[INFO] |        +- org.springframework.boot:spring-boot-starter:jar:1.4.3.RELEASE:compile
[INFO] |        |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.3.RELEASE:compile
[INFO] |        |  |  +- ch.qos.logback:logback-classic:jar:1.1.8:compile
[INFO] |        |  |  |  \- ch.qos.logback:logback-core:jar:1.1.8:compile
[INFO] |        |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.22:compile
[INFO] |        |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.22:compile
[INFO] |        |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.22:compile
[INFO] |        |  \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] |        +- org.apache.tomcat:tomcat-jdbc:jar:8.5.6:compile
[INFO] |        |  \- org.apache.tomcat:tomcat-juli:jar:8.5.6:compile
[INFO] |        \- org.springframework:spring-jdbc:jar:4.3.5.RELEASE:compile
[INFO] |           \- org.springframework:spring-tx:jar:4.3.5.RELEASE:compile

となっていたところが、バージョン1.2では、

[INFO] +- org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:1.2.0:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.4.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.4.3.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-context:jar:4.3.5.RELEASE:compile
[INFO] |  |  |     +- org.springframework:spring-aop:jar:4.3.5.RELEASE:compile
[INFO] |  |  |     \- org.springframework:spring-expression:jar:4.3.5.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.4.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.3.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.8:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.8:compile
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.22:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.22:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.22:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.4.3.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat:tomcat-jdbc:jar:8.5.6:compile
[INFO] |  |  |  \- org.apache.tomcat:tomcat-juli:jar:8.5.6:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:4.3.5.RELEASE:compile
[INFO] |  |     +- org.springframework:spring-beans:jar:4.3.5.RELEASE:compile
[INFO] |  |     \- org.springframework:spring-tx:jar:4.3.5.RELEASE:compile
[INFO] |  +- org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:jar:1.2.0:compile
[INFO] |  +- org.mybatis:mybatis:jar:3.4.2:compile
[INFO] |  \- org.mybatis:mybatis-spring:jar:1.3.1:compile

になっています。
何が変わったかというと・・・、バージョン1.1では、mybatis-spring-boot-startermybatis-spring-boot-autoconfigureだけに依存し、mybatis-spring-boot-autoconfigureがMyBatis, MyBatis-Spring, Spring Bootとの依存関係をもっていました。一見すると問題ないように見えますが、実質的にmybatis-spring-boot-autoconfigureがStarterの役割もになっており、この部分がSpring Bootが提供するStarterとAutoConfigureの考え方と異なるよ!!ってことでした。

これは・・・AutoConfigureのアーティファクトだけを追加した場合に、Spring Bootアプリケーションに副作用が出ないようにする(自動コンフィギュレーションが働かない状態にする)べきで、自動コンフィギュレーションを有効にするために必要となる依存ライブラリ群を一発で解決する役割がStarterだよね!!ってことみたいです。

Warning:
依存アーティファクトとしてmybatis-spring-boot-starterを指定している場合はこの変更の影響をうけることはありませんが、もしmybatis-spring-boot-autoconfigureの方を指定している場合は、バージョン1.2を使う際にmybatis-spring-boot-starterを指定するように変更してください。

mybatis.configuration-propertiesの追加

バージョン1.2より、プレースホルダ(${キー名})に置き換える値を指定するための新しいプロパティ(mybatis.configuration-properties)が追加されました。プレースホルダは、MyBatis設定ファイル、Mapper XMLファイル、SQL定義の置換変数、一部アノテーションの値の中で使うことができます。

プロパティで指定可能な形式は、

mybatis.configuration-properties.キー名=キー値

で・・・例えば、

src/main/resources/application.properties
mybatis.configuration-properties.db.schema=prod

と定義することで${db.schema}といった感じで参照することができます。

これは・・・MyBatis設定ファイルで表現すると、

mybatis-config.xml
<properties>
    <property name="db.schema" value="prod" />
</properties>

MyBatis Springが提供しているorg.mybatis.spring.SqlSessionFactoryBeanを使って表現すると、

@Bean
SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    Properties props = new Properties();
    props.setProperty("db.schema", "prod");
    factoryBean.setConfigurationProperties(props);
    return factoryBean;
}

or

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configurationProperties">
        <props>
            <prop key="db.schema">prod</prop> 
        </props>
    </property>
</bean>

になります。

Note:
実は・・・バージョン1.1.1でも、以下のように定義することで同じことが実現できます。この方法はバージョン1.2でも使うことができ、同じキー名がある場合はバージョン1.2で追加されたmybatis.configuration-propertiesで指定した値が優先されます。

src/main/resources/application.properties
mybatis.configuration.variables.db.schema=prod

たとえば・・・mybatis.configuration.variablesには環境依存しない値(or デフォルト値)を定義しておき、プロファイル毎(環境毎)のプロパティファイルでmybatis.configuration-propertiesを使用して環境依存値に上書きするといった使い方もできます。

mybatis.configuration配下のプロパティキーとIDEの連携

バージョン1.1からorg.apache.ibatis.session.Configurationをコンフィギュレーションするためのプロパティ(mybatis.configuration.*)がサポートされましたが、STSやIntelliJ IDEAなどのIDE上でプロパティキーが補完対象になっていませんでした。この不具合がバージョン1.2で解消されます。

Note:
ただし・・・今度はプロパティキーが重複するという別の問題(gh-127)が・・・
これは、ビルド時に使用しているmaven-bundle-pluginの不具合のようで、バージョン1.2.1で解決される予定です。

2017/4/10: 追記
プロパティキーが重複する問題はバージョン1.2.1で解決されました!!

ConfigurationCustomizerインタフェースの追加

バージョン1.3の変更点を参照してください。

@MybatisTest(mybatis-spring-boot-starter-test)の追加

バージョン1.3の変更点を参照してください。

1
3
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
1
3