Maven使用Mybatis出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误

当使用Maven和Mybatis遇到'org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)'错误时,可能是因为Mapper XML文件未被正确编译打包。解决方案包括在pom.xml的<build><resources>部分添加资源目录,或在父工程配置中指定资源路径,或者在resource目录下创建mapper文件夹并批量加载mapper配置文件。
摘要由CSDN通过智能技术生成

在确保其他配置都正确的情况下,检查打包时有没有将Mapper.xml一起打包进来。如果你是把Mapper.xml文件和Mapper类放在一起,或者是通过Mybatis逆向工程生成而直接拷贝的文件,很容易出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常,原因是Maven编译时没有把**Mapper.xml文件编译进来,在pom.xml中或者在父工程的pox.xml中添加:

<build>
     <resources>
         <!--编译之后包含xml-->
         <resource>
             <directory>src/main/java</directory>
             <includes>
                 <include>**/*.xml</include>
             </includes>
             <filtering>true</filtering>
         </resource>
     </resources>
 </build>

或者:

<build>
     <!--
       此plugin可以用
       利用此plugin,把源代码中的xml文件,
       打包到相应位置,这里主要是为了打包Mybatis的mapper.xml文件
       -->
       <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>
   </plugins>
 </build>

更或者是在resource资源目录下建立一个mapper文件夹用来存放mapper.xml配置文件,然后在sqlMapConfig.xml配置文件中批量加载mapper配置文件

<mappers>
	<mapper resource="mybatis/mapper/*.xml"/>
</mappers>

或是spring配置文件中批量加载mapper


<!--自动加载所有的mapper.xml文件,不再需要单独配置  -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:spring/sqlMapConfig.xml"/>
    <property name="mapperLocations">
    	<!-- 批量扫描maven resource资源目录下的mapper配置文件-->
        <value>classpath:mybatis/mapper/*.xml</value>
    </property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值