报错问题:
mapper绑定异常 nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxx.xxxx.mapper.UserMapper.getAllUser] with root cause
错误日志:
2022-03-23 19:26:05.834 ERROR 26256 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxx.xxxx.mapper.UserMapper.getAllUser] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxx.xxxx.mapper.UserMapper.getAllUser
背景:
mapper接口和**mapper.xml映射文件**在同一个包下。
排查过程:
在看到 **Invalid bound statement (not found)**后,提示未找到mapper映射文件,然后打开编译后的target包或生成的jar包后,找到mapper映射文件存放位置,发现编译后的jar包内并没有此映射文件,进而将问题指向到Maven编译的过程中没有将普通配置文件编译进jar包内。
报错原因:
由于我把mapper映射文件放在mapper接口的同一包里,并没有放在resources目录下,导致通过Maven编译后没有把除Java以外的映射文件、配置文件编译到target和jar包内,在运行时,调用mapper映射文件时,发现在同一个目录下没有找到mapper映射文件而报错。
我们知道Maven是根据pom.xml执行任务,其中build标签描述了如何来编译及打包项目,而具体的编译和打包工作是通过build中配置的 plugin 来完成。而对于resources目录下资源,往往不是代码(.properties或XML配置文件),无需编译,所以在构建过程中往往会将资源文件从源路径直接复制到指定的目标路径。
src/main/java和src/test/java这两个目录中的所有*.java文件会分别在comile和test-comiple阶段被编译,编译结果分别放到了target/classes和targe/test-classes目录中,但是这两个目录中的其他文件都会被忽略掉。
解决办法:
需要在你的项目的pom文件中添加一下配置
<build>
<resources>
<!-- 解决mapper绑定异常 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!-- 解决未找到数据源等配置异常 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
</build>
博客内容讲述了在使用MyBatis时遇到的mapper绑定异常,原因是mapper映射文件没有被Maven正确编译进jar包。作者分析了问题背景,指出由于mapper.xml文件放在了非resources目录下,导致Maven在打包时不包含这些文件。为了解决这个问题,需要在pom.xml中添加特定资源配置,确保mapper.xml和相关资源文件能被正确编译并打包。
6336

被折叠的 条评论
为什么被折叠?



