一.问题描述
今天在在项目时使用mybatis逆向工程生成的dao层代码时,在访问数据库时出现下面的异常:
Invalid bound statement (not found): cn.e3mall.mapper.TbItemMapper.selectByExample] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
调用数据库出错。
二.问题排查
出现此问题的原因是mybatis生成的接口和映射文件在编译过后不在同一目录下,导致接口定义找不到映射文件出错。虽然在IDE中,mapper接口文件和映射文件是放在同一个目录下,但是编译后的却不在一起
IDE中的文件目录结构:
编译后的class文件:
三.解决问题
只要在此工程的pom.xml加入以下内容即可,表示在编译时把配置文件也编译到目标目录
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>