LoginSignup
26
17

More than 5 years have passed since last update.

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
17
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
17