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


    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'
        }
    }
    

      

    问题解决!

    作者:森林木马

    -------------------------------------------

    特此声明:所有评论和私信都会在第一时间回复。也欢迎朋友们指正错误,共同进步!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    个性签名:好记性不如勤随笔,好随笔还请多关注!

  • 相关阅读:
    Unix/Linux环境C编程入门教程(23) 字符数字那些事儿
    Unix/Linux环境C编程入门教程(22) C/C++如何获取程序的运行时间
    如何定义函数模板
    Unix/Linux环境C编程入门教程(21) 各个系统HelloWorld跑起来效果如何?
    为什么使用模板
    CC++初学者编程教程(16) 搭建Xcode cocos2dx开发环境
    delete noprompt archivelog 报错ORA-00245,RMAN-08132
    RMAN-03002、RMAN-06059
    RAC RMAN 备份 RMAN-03009 ORA-19504 ORA-27040 RMAN-06012 channel c3 not allocated 错误分析
    RMAN备份到NFS,报错 ORA-27054
  • 原文地址:https://www.cnblogs.com/owenma/p/14570765.html
Copyright © 2020-2023  润新知