IDEA org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxx
前言
在IDEA上运行Mybatis+Spring时,总是能碰到Eclipse上碰不到的问题,在Eclipse上没一点问题,下面将错误即解决办法记录下来,希望可以帮到更多的人。
具体情况
由于我这边的mapper.xml文件是放在dao包下面的,和dao层接口放到同一个目录,在IDEA上不会编译src/main/java
目录下的xml文件,因为IDEA默认是编译resources
目录下的mapper.xml文件即使你配置了也不会编译,看下图。
编译后的classes,在项目目录下的target目录下。
可以看到编译后的目录下没有生成我们想到的 IApiInfoDAO.xml文件,所以运行的时候会出现异常。
具体异常如下:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.javaex.uscat.dao.user_info.IApiInfoDAO.select
解决办法
1、可以将所有的mapper.xml放到resources目录下。
2、可以在pom.xml文件下添加下面配置即可。
<build>
<resources>
<!-- mapper.xml文件在java目录下 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
修改完成之后先clean一下,避免编译的时候出问题,修改完从生新 install 一下可以看到,mapper.xml文件在classes目录下生成了。
补充
这里补充几个Mybatis的知识。
1、Mapper.java 和 Mapper.xml命名需要一致
2、包扫描能不能扫到.xml文件
3、Mapper.xml的namespace配置是否准确,接口方法名是否准确
总结
1、以后碰到类似not found 的提示一定要记得检查编译到的文件有没有编译完成。