MyBatisを利用していて、XML内の記述にstartDateTime
とendDateTime
の変数を指定しているところがある
... where INSERT_TIME between #{startDateTime} and #{endDateTime} ...
Mapperのインターフェースは以下のように定義した
@Mapper @Component interface MyQueryMapper { List<ResultDto> aggregate( OffsetDateTime startDateTime, OffsetDateTime endDateTime); }
これを実行したところ以下のエラーが発生した
Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'startDateTime' not found. Available parameters are [arg1, arg0, param1, param2]
調べたところ、MyBatisではMapper メソッドが複数の引数を取る場合、各引数は位置を表す数字で #{param1}, #{param2} のように参照する
そのため、わかりやすい名前で引数を参照したい場合は、@Param
アノテーションを使って @Param("paramName")
のように指定する必要があった)引数が複数存在する場合のみ)
MyBatis – MyBatis 3 | Java API
ということで、以下のように実装を修正したらエラーが消えた
@Mapper @Component interface MyQueryMapper { List<ResultDto> aggregate( @Param("startDateTime") OffsetDateTime startDateTime, @Param("endDateTime") OffsetDateTime endDateTime); }