自定义数据源后,springboot报错org.apache.ibatis.binding.BindingException: Invalid bound statement,具体如下

报错如下:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.hqtc.live.admin.common.dao.UserDao.list
常规检查方式(网上其他博客所写):
1:Mapper.xml中的namespace不对应和mapper接口不对应
   2:Mapper.xml中的方法(即id)和mapper接口中的方法名字不同或对应的方法不存在
  3:返回类型不匹配(即没有正确配置ResultMap或者ResultType)
  4:可能xml文件有缓存或者修改后没保存
  5:可能没有配置MapperScan导致dao方法没有被扫描注入
  6:配置文件中mybatis.mapper-locations或mybatis.typeAliasesPackage配置不正确

根据以上方式检查代码后发现都没有问题!!!!
通过打断点的方式,发现后台源代码

 public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
       final String methodName = method.getName();
      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);
         }
       }
    }

通过以上源代码发现进入了ms==null的逻辑,通过调试发现springboot配置文件中,mapper-locations属性没有被解析到。回想到自定义数据源的时候,自定义过SqlSessionFactory的实现和注入,又可能是那个地方的值不对

  @Bean(name = "SqlSessionFactory")
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dynamicDataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*.mapping/*.xml"));
        return bean.getObject();
    }

检查发现上面的重定义的resource路径不对
PathMatchingResourcePatternResolver().getResources(“classpath*.mapping/.xml"));,中间的符号出现问题,正确方式为PathMatchingResourcePatternResolver().getResources("classpath:mapping/*.xml”));,一个符号的问题找了两个小时,不过通过照bug的方式也学习了源码!!!
这个地方特别需要注意的是,自定义过SqlSessionFactory的实现和注入之后,springboot的配置文件中mapper-locations属性失效,要想mapper配置文件生效需要重新定义路径(如上)
Springboot自定义数据源方式,参考

springboot-mybatis多数据源的两种整合方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值