mybatismybatis 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


    <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下新建目录层级有问题

这是正确的目录层级:
在这里插入图片描述
这个是错误的目录层级:
在这里插入图片描述
在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;
        }
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在你的问题中,你提到了一个报错信息"Invalid bound statement (not found)"。根据引用的内容,这个报错信息通常出现在使用MyBatis框架时,表示在mapper接口中绑定的SQL语句无法找到。 这个报错信息的原因可能有以下几种可能性: 1. mapper接口中的SQL语句没有正确命名或者没有在对应的mapper.xml文件中定义。你需要仔细检查mapper接口和对应的mapper.xml文件,确保SQL语句的命名和定义是正确的。 2. mapper.xml文件没有正确配置或者没有被正确加载。你需要检查mybatis-config.xml文件中是否正确配置了mapper.xml文件的路径,并且确保mapper.xml文件在该路径下存在。 3. mapper接口没有在mybatis-config.xml文件中正确注册。你需要检查mybatis-config.xml文件中是否正确配置了mapper接口的路径,并且确保mapper接口已经在该路径下存在。 综上所述,当遇到"Invalid bound statement (not found)"的报错信息时,你可以先检查mapper接口和mapper.xml文件的配置、命名和路径是否正确,并确保它们能够正确加载和注册。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决动态SQL报错Invalid bound statement (not found): ……](https://blog.csdn.net/qq_49742153/article/details/129348842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [service层报错Invalid bound statement (not found),但调用mapper层不报错](https://blog.csdn.net/weixin_54348877/article/details/129742486)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值