通过跟踪源码分析BindingException: Invalid bound statement (not found)产生的原因

背景

新项目,框架springboot + mybatis + mybatis plus,想要增加一个CommonMapper以及对应的mapper xml文件专门存放通用的sql或者一些跨多张表的sql。结果写好了启用测试的时候报错了, 如下所示:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) 
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedMapperMethod$0(MybatisMapperProxy.java:101)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedMapperMethod(MybatisMapperProxy.java:100)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:95)
	at com.sun.proxy.$Proxy157.testList(Unknown Source)
	at com.mogulinker.gqgj.integrate.core.application.controller.DemoController.test(DemoController.java:189)

排查过程

在仔细分析后百思不得其解,在网上搜下可以看到很多说法,但是我不想盲目尝试了, 想要找到本质原因,所以有了以下排查过程:
首先定位到MapperMethod.java:235 行找到这个错误从哪里跑出来的,如下所示

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
      final String methodName = method.getName();
      final Class<?> declaringClass = method.getDeclaringClass();
    // == 3 继续找,说明 这个方法 resolveMappedStatement 方法返回为空,继续跟进这个方法
      MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
          configuration);
      if (ms == null) {
          //== 2 往上找,说明ms为空
        if (method.getAnnotation(Flush.class) != null) {
          name = null;
          type = SqlCommandType.FLUSH;
        } else {
            //  == 1 =======没错,错误就是从这里跑出来的========
          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) {
      String statementId = mapperInterface.getName() + "." + methodName;
   
      if (configuration.hasStatement(statementId)) {
          // === 4 说明configuration.hasStatement(statementId)  这个方法返回了false, 继续看这个方法
        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;
    }
    ```
```java
public boolean hasStatement(String statementName) {
    return hasStatement(statementName, true);
  }

  public boolean hasStatement(String statementName, boolean validateIncompleteStatements) {
    if (validateIncompleteStatements) {
      buildAllStatements();
    }
      // == 5 说明这个方法返回了false, 继续找
    return mappedStatements.containsKey(statementName);
  }

//== 6 发现mappedStatements是一个map集合, 说明这个集合中不存在,接下来就要找这个map什么时候初始化,添加元素的地方在哪里
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection")
      .conflictMessageProducer((savedValue, targetValue) ->
          ". please check " + savedValue.getResource() + " and " + targetValue.getResource());
//== 7 搜索一个,发现有方法会向这个集合中添加元素, 所有一下这个方法被调用的地方,发现有很多,为了简单。直接在这个方法打一个断点,启动debug模式
public void addMappedStatement(MappedStatement ms) {
    mappedStatements.put(ms.getId(), ms);
  }

== 8 从调用链可以清晰的看到这个方法是从哪里调用过来的,一层层找过去,
在这里插入图片描述

可以看到上图中最下面的这个就是MyBatisPlusAutoConfiguration这个类,如下所示的方法中,返回SqlSessionFactory的方法:
在这里插入图片描述

定位到如下这一行:
在这里插入图片描述

看到这里问题也就基本确定问题了, 这里只找到了一个demoMapperXml文件, 在我的项目中应该有3个,一个是代码生成工具自动生成的一个demoMapper, 另一个是根据一个表生成的ModuleAMapper, 还有一个是我手动新增的CommonMapper对应的xml。
那说明了mapper根据mapper location扫描路径没扫描出来,那是不是就可以确定配置路径的问题, 继续往下验证下,
在这里插入图片描述

如上图所示,这里就是根据配置文件中mapper-locations 路径获取所有的xml文件, mapper-locations配置如下所示:

mybatis-plus:
  mapper-locations: classpath*:com/aa/bb/cc/data/**/xml/*Mapper.xml

到这里就可以确定是通配符配置文件,对比下三个xml文件的路径就能确定了:
com/aa/bb/cc/data/xml/DempMapper.xml
com/aa/bb/cc/data/xml/module1/Module1Mapper.xml
com/aa/bb/cc/data/xml/common/CommonMapper.xml
只扫描到了DempMapper,把通配符改成com/aa/bb/cc/data/**/*Mapper.xml这样就都能扫出来。

结论:

万万没想到的是因为mapper-locations配置错误。
之所以没想到,是因为项目是用公司的代码生成工具直接生成的, 路径什么的全都是根据自动生成的, 并且自带的DemoMapper可以正常工作,以及用myBatis plus生成的Module1Mapper也能半正常工作,为什么说一半正常, 是因为在使用mybatis plus自动的基础的增删改查方法都能使用, 就算是新增的方法如果是使用@Select注解方法写得sql语句 也能正常使用, 只有当方法对应的sql写到xml文件的时候才会异常,也是报上面说的错误。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值