今天写spring boot 整合mybatis的时候遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)这个错,找了很久,总结原因是因为扫描不到xxxMapper.xml。
受到以前整个springMvc整合mybatis的影响,将文件放在了src/main/java下,导致一直扫描不到xxxMapper.xml文件。原因是:idea中maven打包的时候不会将xxxMapper.xml,所以target打包之后不会有xxxMapper.xml文件,所以一直扫描不到。
目录结构:
解决办法:将xxxMapper.xml文件放在了src/main/resources下,然后在application.properties文件中添加配置
mybatis.mapper-locations=classpath:mappings/*.xml
或者在application.yml文件中加入配置:
mybatis:
mapper-locations: classpath:mappings/*.xml
还有一种办法是:
使用@Select、@Insert、@Update、@Delete注解代替xxxMapper.xml里面的内容,这样就不存在扫描xxxMapper.xml文件的问题了。
遇到org.apache.ibatis.binding.BindingException: Invalid bound statement(not found)这个问题可能还有其他原因,不过这些原因很容易排查。
- xxxMapper.xml文件中的namespace对应有误
- xxxMapper.java文件中没有对应的xxxMapper.xml中的方法。
记录一下,防止以后自己再采坑。:)