Maven 项目中使用 Mybatis 框架 ,如果运行项目时报如下错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
原因是构建Maven项目的时候,如果没有进行特殊的配置,Maven会按照标准的目录结构查找和处理各种类型文件。
Maven打包时,默认java目录下只将.java编译并将编译后的class文件打包,而mapper接口与mapper映射文件放在同一目录下时,mapper映射文件未被打包 。
以下几种方法都可以解决该问题。
1.build节点下添加 resources节点
<!--xml文件打包-->
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
2.如使用build-helper-maven-plugin插件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
3.使用maven-resources-plugin插件
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-xmls</id>
<phase>process-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
当使用Maven和Mybatis框架时,如果遇到Mapper.xml文件未被包含进打包的错误,原因可能是Maven默认配置不包含非.java文件。解决方法包括在build节点下添加resources配置,使用build-helper-maven-plugin插件,或者通过maven-resources-plugin插件确保mapper映射文件被正确打包。
1884

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



