栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一、问题描述

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
    
        
	

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文件里添加如下配置:

    
        
        
            
                src/main/java
                
                    ***.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/然后就可以了。

三、提高扩展

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

List 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;
        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732235.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号