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

发布时间 2023-04-17 16:44:20作者: 与f

1.问题

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

Springboot项目中,在mybatis中mapper数据库操作接口(有的称DAO,有的直接说mapper,都只同一文件)与mapper配置文件在做映射绑定的时候出现问题,简单说,就是接口与xml要么是找不到,要么是找到了却匹配不到。

2.原因导致 Invalid bound statement (not found)的可能原因有:

1)xml文件所在package名称和mapper interface所在的package name不一致,mapper 的namespace写的不对,需要修改。

mapper映射的xml文件示例:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.dao.IDemoMapper">
 
</mapper>

2)  mapper 接口文件在映射的xml文件中没有,执行对应方法后报 Invalid bound statement (not found)。

xml文件标签的id名与mapper接口中的方法名保持一致.

3)mapper接口文件中的返回值为定义的POJO时,select元素中没有正确配置ResultMap,或者只配置了ResultType

4)mapper的xml文件配置路径不正确。SpringBoot要扫描到mapper静态资源文件即xml文件,需要添加配置类 (加@Mapper注解,这个不用配置了)

@Configuration
@MapperScan("com.example.demo.mapper")
public class MyBatisConfiguration {
}

5.要把source/mapper这些文件编译到class类下,需要在pom文件中<build></build>添加如下.来保证文件都能正常被扫描到并且加载成功.

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

 

 

 

转: https://blog.csdn.net/qq_43780761/article/details/126494026