找不到mapper.xml:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
先说该异常常见解决方案:
一般是xml映射文件有错误。但是这个提示不一定准确,以致网上有些资料说这个问题很奇怪。出现这个错误一般的解决方式在下面有说明:
一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比。
按以下步骤一一执行:
1:检查xml文件所在的package名称是否和interface对应的package名称一一对应
2:检查xml文件的namespace是否和xml文件的package名称一一对应
3:检查函数名称能否对应上
4:去掉xml文件中的中文注释
5:随意在xml文件中加一个空格或者空行然后保存(触发了ide的自动编译功能。由于xml文件在编译的时候,不一定总能立即从源目录复制到class文件的编译目录(MyEclipse经常出这个问题),有时候你源目录中的xml文件已经修改好了,而class所在的目录里面还是旧的。因此真正确定有效的方式是将正确的xml文件复制到class输出目录。)
(博主使用IDEA工具)
但是博主在尝试以上方案之后仍然报错,在我查看了发布之后的源代码之后,发现*Mapper.xml文件并没有发布到目标文件中。
如图所示:
Mapper目录:
发布之后target目录中:
居然没有xml文件,突然意识到了异常的原因:
idea中我将mapper类型是source,而xml文件在发布时不会编译,同时也不会发布到target中,导致项目运行时找不到mapper.xml文件。
解决方案:将其以相同的路径写入resources中
在target中编译结果
总结:
(1)eclipse、myeclipse、idea针对项目的文件处理方式不一样。
eclipse和myeclipse中mapper.java和mapper.xml在同一目录下,直接配置扫描不会出现上述问题。
(2)注意xml文件的准确性。