项目中使用的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。