zoukankan      html  css  js  c++  java
  • 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'
        }
    }
    

      

    问题解决!

    作者:森林木马

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

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

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

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

  • 相关阅读:
    Kettle Spoon 数据源连接中报ORA12505, TNS:listener does not currently know of SID given in connect descriptor的错误
    oracle创建用户和删除用户sql
    Kettle Spoon表输入使用变量
    Kettle Spoon 资源库连接后新建转换打不开,报Invalid byte 1 of 1byte UTF8 sequence异常
    oracle 误删除表/表中数据,恢复方法
    让ie兼容css3新属性backroundsize
    网页设计中为啥少用奇数字体?
    浏览器差异总结,可以用此判断浏览器版本(转)
    ie浏览器f12下调“浏览器模式”和“文档模式”的区别
    jquery的淡入淡出和隐藏,展示函数在ie中应用的bug
  • 原文地址:https://www.cnblogs.com/owenma/p/14570765.html
Copyright © 2011-2022 走看看