nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

107 篇文章 17 订阅
4 篇文章 0 订阅

项目中使用的mybatis执行SQL后报错:

nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

本人项目中使用到的mybatis的逆向生成工具类在单独的一个项目中(因为项目生产环境中非常不建议将mybatis-generator集成到业务项目中),这个生成工具在这里:https://download.csdn.net/download/fanrenxiang/10427434

spring中使用mybatis作为持久层,在调用时经常会发生nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误,这里可根据以下步骤排查:

1、mapper.xml文件中的namespace指向正确的Mapper接口全限定名

<mapper namespace="com.simons.cn.springbootdemo.dao.system.MovieMapper" >

2、Mapper接口中方法名和×××Mapper.xml中sql的id对应

 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
        select
        <include refid="Base_Column_List" />
        from movie
        where id = #{id,jdbcType=BIGINT}
 </select>
public interface MovieMapper {
    Movie selectByPrimaryKey(Long id);
}

不止是方法名和SQL的id名要一致,方法的入参类型,结果集类型都要匹配上。

3、包扫描路径要正确

比如在springboot中的扫描mapper接口组件位置注解

@MapperScan("com.simons.cn.springbootdemo.dao.*")

4、sql语句的返回值和resultType、resultMap对应上,例如sql的返回值是List类型,就需要用resultMap接收

5、如果以上都没错误,则打开项目的target目录下中,观察里面是否有对应的××Mapper.xml文件,若没有,则在pom.xml文件中加入如下配置

<build>
    <resources>
      <resource>
       <directory>src/main/java</directory>
       <includes>
         <include>**/*.xml</include>
       </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>  
      </resource> 
    </resources>
</build>

刷新并且重新打包一下就好了。若没有加以上配置,maven打包默认会遗漏mapper.xml文件,而你运行的又是war包,所以会一直找不到对应的sql statement。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值