mybatis-plus报错解决Invalid bound statement (not found)错误

摘要:
˃declaringClass=方法.getDeclaringClass();//问题是返回为空:原因是找不到接口类MappedStatementms=resolveMappedStatement;如果{If(method.getAnnotation(Flush.class)!˃declaringClass,Configurationconfiguration){//XXMapper.xxMethodStringstatementId=mapperInterface.getName()+“.”+methodName;//配置有一个大集合,如果{returnconfiguration.getMappedStatement;}否则如果{return null;}for(class˂?=null){retrnms;}}returnnull;}}问题出在这里。MappedStatements没有项目的映射器。原因是它没有被扫描,也就是说,它在扫描过程中定位到了配置问题!

mybatis-plus报错解决Invalid bound statement (not found)错误

异常信息

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

也就是在mybatis中dao层xxxMapper接口与xxxMapper.xml文件在做映射绑定的时候出现问题,也就是xxxMapper接口无法匹配到操作sql语句的方法id~

源码解析

首先断点打在调用mapper方法的地方

tagMapper.findTagList();

继续走,进入MapperMethod.java类:

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
      //methodName就是调用的方法名
      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;
  }
}

  

问题就在这里 ,mappedStatements没有工程的mapper,原因就是没有扫描到,即定位到扫描时配置问题!

解决方案

1. 检查xml映射文件中<mapper>标签绑定包名地址是否正确(即namespace的值)

2. 检查xxxMapper接口中的方法,对应xml映射文件中是否有

3. 检查<select>标签中的resultType是否与xxxMapper接口中的方法返回值类型一致,若一个是对象一个是集合,那也会报错~

4. 检查yml配置文件中的mybatis配置 

配置项 mybatis -> mybatis-plus 

mybatis-plus:
mapper-locations: classpath*:com/xxx/*Mapper.xml
typeAliasesPackage: com.xxx.entity

5. xml资源配置

maven:

<build>
          <resources>
              <resource>
                  <directory>src/main/java</directory>
                  <includes>
                      <include>**/*.properties</include>
                      <include>**/*.xml</include>
                      <include>**/*.tld</include>
                  </includes>
                  <filtering>true</filtering>
              </resource>
          </resources>
  </build>

gradle:

processResources {
    from('src/main/java') {
        include '**/xml/*.xml'
    }
}

  

问题解决!

免责声明:文章转载自《mybatis-plus报错解决Invalid bound statement (not found)错误》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇gitlab根据hook钩子自动化部署06_zookeeper原生Java API使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot分层概览

从下到上依次为:数据库、Model层、Mapper层(DAO层)、Service层、Controller层、View层、用户 下边对分层进行大致的解释: 数据库 Model层:存放了页面需要传递数据对应字段的实体类,它和数据库中对应表字段的属性值保持一致。也就是说成员变量+getter/setter方法把数据库对应表中的字段映射为对象的属性。 Mapper...

mybatis3 @SelectProvider

mybatis3中增加了使用注解来配置Mapper的新特性,本篇文章主要介绍其中几个@Provider的使用方式,他们是:@SelectProvider、@UpdateProvider、@InsertProvider和@DeleteProvider。MyBatis 3 User Guide中的最后一章描述了注解的简单用法,但是对于这几个Provider的具...

SpringBoot框架:使用mybatis连接mysql数据库完成数据访问(二)

一、导入依赖包 1、在创建项目时勾选: 勾选SQL中的JDBC API、MyBatis Framework、MySQL Driver,创建项目后就会自动配置和引入这些包。 2、在pom.xml文件中添加依赖: 在<dependencies></dependencies>中添加以下代码,引入jdbc、mybatis和mysql依赖...

IDEA中比较实用的几款插件

写在前面: 我是 扬帆向海,这个昵称来源于我的名字以及女朋友的名字。我热爱技术、热爱开源、热爱编程。技术是开源的、知识是共享的。 这博客是对自己学习的一点点总结及记录,如果您对 Java、算法 感兴趣,可以关注我的动态,我们一起学习。 用知识改变命运,让我们的家人过上更好的生活。 学习使用一些插件,可以提高平常工作中的开发效率。对于我们开发人员很有帮助...

MyBatis(四)映射文件 之 参数获取详解#{} 与 ${}

一、#{} 与${} 的取值 相同点: #{}:可以获取map中的值或者pojo对象属性的值; ${}:可以获取map中的值或者pojo对象属性的值; 区别: #{}:是以预编译的形式,将参数设置到sql语句中;PreparedStatement;防止sql注入; ${}:取出的值直接拼装在sql语句中;会有安全问题; 大多情况下,我们去参数的值都应该去使...

MyBatis基础总结

1.1什么是MyBatis MyBatis(前身是iBatis)是一个支持普通SQL查询、存储过程以及高级映射的持久层框架, 它消除了几乎所有的JDBC代码和参数的手动设置以及对结果集的检索,并使用简单的XML或注解进行配置和原始映射, 用以将接口和Java的POJO(Plain Old Java Object,普通Java对象)映射成数据库中的记录,使得...