maven项目下报错mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement(not found
问题原因
今天在maven项目下遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)这个错,到target目录下看是否有xxmapper.xml文件生成(按照自己对xxmapper.xml文件的路径配置到target下classes目录下找),发现没有对应的xxmapper.xml文件。总结原因是受到以前整个springMvc整合mybatis的影响,将文件放在了src/main/java下,导致一直扫描不到xxxMapper.xml文件。而idea中maven打包的时候不会将xxxMapper.xml打包,target打包编译之后不会有xxxMapper.xml文件,所以一直扫描不到而报此错。
解决办法
有以下几种办法:
1、将xxxMapper.xml文件放在了src/main/resources下,然后在application.properties文件中添加配置
mybatis.mapper-locations=classpath:mappings/*.xml
2、在application.xml文件中加入配置:
mybatis:
mapper-locations: classpath:mappings/*.xml
3、到target目录下看是否有mapper.xml文件生成(按照自己对mapper.xml文件的路径配置到target下classes目录下找),如果没有可以在pom.xml文件的<build></build>
之间添加
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
或者也可以再在<resources></resources>
之间再添加一项
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
使mapper.xml文件不论是放在src/main/resources或src/main/java下都可以被打包到classes目录下。
4、使用@Select、@Insert、@Update、@Delete注解代替xxxMapper.xml里面的内容,这样就不存在扫描xxxMapper.xml文件的问题了。
其它原因
1、xxxMapper.xml文件中的namespace对应有误
2、xxxMapper.java文件中没有对应的xxxMapper.xml中的方法。
在此mark以下,防止以后再次踩坑。