26
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

アルス・ウェアーAdvent Calendar 2018

Day 10

Spring Boot + Mybatisのmapper-locations

Last updated at Posted at 2018-12-09

環境

Spring Boot:2.0.4.RELEASE
Mybatis:3.4.6
Mybatis-Spring:1.3.2

Spring Boot + Mybatisのmapper-locations

Spring Boot + MybatisでMapper XMLを利用してデータアクセスを行う場合、以下のようにapplication.yml(properties)にXMLファイルのロケーションの指定ができます。

application.yml
mybatis:
  mapper-locations: classpath*:/jp/co/arsware/example/mapper/*.xml

ただし、Spring Boot + Mybatisでは、Mapper XMLとMapperクラスのパスが同じ場合、上記の指定をしなくてもMybatisがMapper XMLを読み込んでくれます。「Mapper XMLとMapperクラスのパスが同じ」のイメージは以下のような形です。

・src/main/java/jp/co/arsware/example/mapper/CityMapper.java
・src/main/resources/jp/co/arsware/example/mapper/CityMapper.xml
※co以下が同じパスであれば読み込んでくれる

上記が同じでない場合でかつapplication.yml(properties)にmapper-locationsの指定がない場合、実行時に以下のようなエラーが発生することになります。

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

そのため、例えばXMLの配置先は変更したい、等があれば明示的にapplication.yml(properties)にmapper-locationsの指定をする必要があります。

以上です。

26
18
2

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
26
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?