org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 引发的血案

本文主要是介绍org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 引发的血案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

故事背景

今天在跑项目(SpringBoot+mybatis)时,当通过service调用DAO时,系统抛出了异常:

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

很奇怪的异常,异常堆栈也没有关键的信息。从这句话大概的分析可以知道原因是:mybatis的Mapper接口不能够和XML映射文件绑定起来,所以导致的接口调用失败。

解决历程:

首先当然是Google,这个问题原来很常见,网上给出的解决方案终结一下大概有下面几点:

  1. 检查xml文件所在的package名称是否和interface对应的package名称一一对应(这个不是必须)

  2. 检查xml文件的namespace是否和mapper接口的package名称一一对应

  3. 检查函数名称能否对应上

  4. 去掉xml文件中的中文注释

  5. 随意在xml文件中加一个空格或者空行然后保存

以上几点我都做了确保,依旧没有解决问题。

此外还有一个很奇怪的现象,我跑单元测试时,无论是我调用Service还是直接调用DAO,都能够很好的执行,并获得结果。然后就自习对比了一下, 发现在我项目的Application.java和ApplicationTest.java中有一点区别,就是:Application.java类上我加了@MapperScan注解,也就是指定了Mapper接口的扫描路径,这是唯一的区别了,于是从这里出发。

1. 先看@MapperScan注解的作用

这个注解可以加载Java config类上面,用于扫描mybatis的Mapper接口。里面有两个很重要的属性:

  • basePackages :这个值注解扫描的base包。

  • annotationClass:准确的指定这个扫描器扫描的注解,也就是被某个注解标记的接口才会被扫描成Mapper。

但是我在自己的应用中,@MapperScan值配置了basePackages属性,没有配置annotationClass属性,所以那个model包下面的DAO接口,也就扫描成了Mapper,但是这个DAO接口是没有对应的XML映射文件的,所以报错了。

2. 解决方式:

后来我加上了annotationClass=Mapper.class这个属性之后,就可以正常的访问了。问题成功解决。

3. 深究背后的原理(source is truth)

我们应该仔细看一下@MapperScan 源码:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {String[] value() default {};String[] basePackages() default {};Class<?>[] basePackageClasses() default {};Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;Class<? extends Annotation> annotationClass() default Annotation.class;Class<?> markerInterface() default Class.class;String sqlSessionTemplateRef() default "";String sqlSessionFactoryRef() default "";Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;
}

上面的源码我们应该关注的是:@Import(MapperScannerRegistrar.class)

也就是MapperScan 导入了MapperScannerRegistrar并作为一个bean。也就是需要分析MapperScannerRegistrar.java源码

这里写图片描述

从类继承图我们知道关键点在于ImportBeanDefinitionRegistrar.class 接口。实现这个接口我们能够具备注册除了Spring标准的bean以外的其余bean到Spring容器。

完整的源码我就不给了,我就给出
registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) 里面的核心部分。

Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");if (!Annotation.class.equals(annotationClass)) {scanner.setAnnotationClass(annotationClass);
}

这段代码显示,当我们没有配置@MapperScan注解的annotationClass 属性的时候,那么scanner就会不会设置scanner的annotationClass域,当我们配置了这个属性时,就会把这个条件注入scanner里面。

scanner.registerFilters(); 里面有一行很重要的代码:

// if specified, use the given annotation and / or marker interface
if (this.annotationClass != null) {addIncludeFilter(new AnnotationTypeFilter(this.annotationClass));acceptAllInterfaces = false;
}
if (this.markerInterface != null) {addIncludeFilter(new AssignableTypeFilter(this.markerInterface) {@Overrideprotected boolean matchClassName(String className) {return false;}});acceptAllInterfaces = false;
}

也就是如果scanner的annotationClass为空,那么mybatis的scanner注册器就不会添加AnnotationTypeFilter,也就是不会添加annotation作为过滤条件。但是我也没有配置markerInterface属性,也就是接口过滤,这时候,@MapperScanner会扫描basePackages下面的所有接口都作为Mapper,也就是把DAO也作为Mapper扫描了,也就会抛出碰到的异常。

至此,原理分析完毕。

其实这里面涉及到两个Spring特别重要的类:
ImportBeanDefinitionRegistrar
ClassPathBeanDefinitionScanner
理解这两个类会大大加深对Spring的理解

参考:http://blog.csdn.net/softwarehe/article/details/8889206

这篇关于org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 引发的血案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996332

相关文章

持久层 技术选型如何决策?JPA,Hibernate,ibatis(mybatis)

转自:http://t.51jdy.cn/thread-259-1-1.html 持久层 是一个项目 后台 最重要的部分。他直接 决定了 数据读写的性能,业务编写的复杂度,数据结构(对象结构)等问题。 因此 架构师在考虑 使用那个持久层框架的时候 要考虑清楚。 选择的 标准: 1,项目的场景。 2,团队的技能掌握情况。 3,开发周期(开发效率)。 传统的 业务系统,通常业

yum install 失败报错`XZ_5.1.2alpha' not found (required by /lib64/librpmio.so.3)

/export/env/py3.6/lib/liblzma.so.5: version `XZ_5.1.2alpha' not found (required by /lib64/librpmio.so.3)   到/export/env/py3.6/lib cp /lib64/liblzma.so.5.2.2 . sudo ln -s -f liblzma.so.5.2.2 liblzm

BD错误集锦1——[Hive]ERROR StatusLogger No log4j2 configuration file found. Using default configuration:

错误描述:在使用IDEA进行jdbc方式连接到hive数据仓库时,出现以下错误:                ERROR StatusLogger No log4j2 configuration file found. 问题原因:缺少log4j2.xml文件   <?xml version="1.0" encoding="UTF-8"?><Configuration><Appender

# bash: chkconfig: command not found 解决方法

bash: chkconfig: command not found 解决方法 一、chkconfig 错误描述: 这个错误表明在 Bash 环境下,尝试执行 chkconfig 命令,但是系统找不到这个命令。chkconfig 命令是一个用于管理 Linux 系统中服务的启动和停止的工具,通常它是 initscripts 包的一部分,但在最新的 Linux 发行版中可能已经被 syste

crontab: command not found

[root@localhost syweb-sdkapi-new_manager]# crontab -e -bash: crontab: command not found 解决方法: yum install vixie-cron yum install crontabs service crond start           /etc/init.d/crond

no thread-bound request found:are you referring to request

问题描述: 通过webservice接口调用程序时,发现在执行查询的时候一直报一个错误,错误信息如下: java.lang.IllegalStateExceptino:No thread-bound request found:are you referring to request attributes outside of an actual web request,or processi

修改wamp的apache默认端口80以及www目录

转自:http://blog.csdn.net/daydreamingboy/article/details/6247592 修改wamp的apache默认端口80以及www目录 以修改为8088端口和D:/workphp目录为例。 1. 修改为8088端口 左键托盘图标,在“Apache”里可以直接打开httpd.conf,查找到“Listen 80”,可以改成其他端口,我选用808

vue dist文件打开index.html报Failed to load resource: net::ERR_FILE_NOT_FOUND

本地正常。打包好的dist文件打开index.html报Failed to load resource: net::ERR_FILE_NOT_FOUND 解决办法: 在webpack.prod.conf.js 中output添加参数publicPath:’./’ 在webpack.base.conf.js里 publicPath: process.env.NODE_ENV === ‘pro

如何解决matplotlib运行出现的Invalid DISPLAY variable

最近在服务器上运行matplotlib相关的脚本时遇到了"Invalid DISPLAY variable"报错,从报错中就可以知道这是因为没有显示设备导致的报错。 解决方案: 方案一: ~/.config/matplotlib/matplotlibr,在里面添加backend : Agg 这个方案不一定有用,如果失效考虑下面两种 方案二: 更换后端 可以先设置后端,然后导入pyp

「Debug R」如何处理Error in readLines(f) :(converted from warning) incomplete final line found on xxx...

用devtools::install_github从GitHub上安装一个R包的时候出现了报错, 报错截图如下所示: 报错 从报错内容基本上可以确定是换行符惹的祸,我将该文件传送到Linux下,用cat -A检查,发现最后一行后面没有换行符。 ^M是Windows的换行符 解决方案: 手动增加最后一行。 手动加换行 到此当前的