mybatis/mybatis plus报错:Invalid bound statement (not found) 解决方法汇总

2年前 (2022) 程序员胖胖胖虎阿
339 0 0

一、问题描述

mybatis/mybatis plus报:Invalid bound statement (not found) 错误,基本上都是mapper文件引起的,我将它总结三类:

1.mapper.xml文件不存在
2.mapper.xml文件里内容有误
3.mapper.xml文件路径配置有误

二、解决方法

以下是自己遇到的和参考了网上的一些解决方法,可以对着过一遍:

1.检查xml的namespace是否和xml文件的package名称一一对应
2.检查xml中是否使用了type别名,如果用了别名,检查下别名包名是否配置正确,如果不确定,可以将实体类全包名加上去,还有就是看下实体类里面是否使用了typeHandler类型处理器,如果使用了,记得将完整包名加上去。

# MyBatis配置
mybatis:
    # 搜索指定包别名
    typeAliasesPackage: com.xxx.**.domain
    <resultMap id="xxxxResultMap" type="xxx.xxx.SiteEntity">
        <result column="xxx" property="xx" javaType="java.util.List" typeHandler="xxx.mybatis.plus.core.type.JsonLongListTypeHandler"/>
	</resultMap>

3.Mapper.java的方法在Mapper.xml中没有,然后执行Mapper的方法会报错
4.xxxMapper.java的方法返回值是List,而select元素没有正确配置ResultMap,或者只配置ResultType
5.Mapper.xml不存在,一般mapper.xml会放在源码目录下,或resources目录下,检查mapper.xml打包后,在target/classes目录下是否存在,使用idea打包时,会过滤一些文件,导致没有打xml文件打包到target/classes目录下,在pom.xml文件里添加如下配置:

    <build>
        <!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

6.注意mybatis/mybatis plus配置与@MapperScan 注解

@MapperScan 注解和 mybatis.mapper-locations 配置两者缺一不可
@MapperScan(basePackages=“xxx.xxx.xxx”) 这个注解是用户扫描 mapper 接口的,也就是dao类;
mybatis.mapper-locations 配置是用于扫描 mapper.xml 的,两者用途不同,故缺一不可。

mybatis-plus:
	mapper-locations: classpath*:mapper/*.xml //classpath后添加你xml文件的目录
方法一:只有一个路径

mybatis:
 mapper-locations: classpath:mapper/*.xml
 

方法二:有多个路径

mybatis:
 mapper-locations: classpath:mapper/*.xml,classpath:mapper/user*.xml
 
方法三:通配符 ** 表示任意级的目录

mybatis:
 mapper-locations: classpath:**/*.xml

7.还有个问题就是,如果是多模块的情况下,我这里出现了如下情况,比如A模块是启动模块,B模块是业务模块,A模板引入了B模块,两个jar包中的mapper.xml都放在resources/mapper/目录下,打包启动之后,调用B模块mapper.xml中的方法也会报Invalid bound statement错误,最后发现是两个模块mapper重名引起的,导致B模块中的mapper.xml加载不出来,虽然存在,但就是加载不出来,通过DEBUG的方法发现的,然后将B模块resources/mapper/改成resources/xxx-mapper/然后就可以了。

8.后面发现还有种情况也会报错,在IDEA下,resource下新建目录层级有问题

这是正确的目录层级:
mybatis/mybatis plus报错:Invalid bound statement (not found) 解决方法汇总
这个是错误的目录层级:
mybatis/mybatis plus报错:Invalid bound statement (not found) 解决方法汇总
在IDEA下两者显示的是一模一样,不在文件下查看,根本发现不了。。

三、提高扩展

如果上面你都检查过了,还没有解决,直接看源码吧,首先断点打在调用mapper方法的地方

List<SiteEntity> siteList= siteMapper.selectSiteList();

往下走,进入MybatisMapperMethod.java类

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
    //methodName就是调用的方法名 getCapitalTypeListAll
    final String methodName = method.getName();
    //declaringClass就是 Mapper接口类
    final Class<?> declaringClass = method.getDeclaringClass();
    //问题出在这里 返回为空:原因是没有找到该接口类
    MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
        configuration);
    if (ms == null) {
        if (method.getAnnotation(Flush.class) != null) {
            name = null;
            type = SqlCommandType.FLUSH;
        } else {
            throw new BindingException("Invalid bound statement (not found): "
                + mapperInterface.getName() + "." + methodName);
        }
    } else {
        name = ms.getId();
        type = ms.getSqlCommandType();
        if (type == SqlCommandType.UNKNOWN) {
            throw new BindingException("Unknown execution method for: " + name);
        }
    }
}
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
                                                   Class<?> declaringClass, Configuration configuration) {
        //XXMapper.xxMethod
        String statementId = mapperInterface.getName() + "." + methodName;
        //configuration有一个大集合,缓存了所有的Mapper及所有的方法
        if (configuration.hasStatement(statementId)) {
            return configuration.getMappedStatement(statementId);
        } else if (mapperInterface.equals(declaringClass)) {
            return null;
        }
        for (Class<?> superInterface : mapperInterface.getInterfaces()) {
            if (declaringClass.isAssignableFrom(superInterface)) {
                MappedStatement ms = resolveMappedStatement(superInterface, methodName,
                    declaringClass, configuration);
                if (ms != null) {
                    return ms;
                }
            }
        }
        return null;
    }
}

报错的位置一般出在:

        if (configuration.hasStatement(statementId)) {
            return configuration.getMappedStatement(statementId);
        } else if (mapperInterface.equals(declaringClass)) {
            return null;
        }

相关文章

暂无评论

暂无评论...