mybatic MapperScannerConfigurer的原理
原文地址:http://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigurer-analysis.html
前言
本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理。本文分析的mybatis版本3.2.7,mybatis-spring版本1.2.2。
MapperScannerConfigurer介绍
MapperScannerConfigurer是spring和mybatis整合的mybatis-spring jar包中提供的一个类。
想要了解该类的作用,就得先了解MapperFactoryBean。
MapperFactoryBean的出现为了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate编写数据访问对象(DAO)的代码,使用动态代理实现。
比如下面这个官方文档中的配置:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
org.mybatis.spring.sample.mapper.UserMapper是一个接口,我们创建一个MapperFactoryBean实例,然后注入这个接口和sqlSessionFactory(mybatis中提供的SqlSessionFactory接口,MapperFactoryBean会使用SqlSessionFactory创建SqlSession)这两个属性。
之后想使用这个UserMapper接口的话,直接通过spring注入这个bean,然后就可以直接使用了,spring内部会创建一个这个接口的动态代理。
当发现要使用多个MapperFactoryBean的时候,一个一个定义肯定非常麻烦,于是mybatis-spring提供了MapperScannerConfigurer这个类,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>
这段配置会扫描org.mybatis.spring.sample.mapper下的所有接口,然后创建各自接口的动态代理类。
注意:这里会扫描下面所有的接口,但是经过我的实验,只会对在mapper.xml中的namespace中配置了的接口才会生成动态代理类。不会影响到basePackage下面的其他接口。
MapperScannerConfigurer底层代码分析
以以下代码为示例进行讲解(部分代码,其他代码及配置省略):
package org.format.dynamicproxy.mybatis.dao;
public
interface
UserDao {
public User getById(int id);
public
int
add(User user);
public
int
update(User user);
public
int
delete(User user);
public List<User> getAll();
}
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.format.dynamicproxy.mybatis.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
我们先通过测试用例debug查看userDao的实现类到底是什么。
我们可以看到,userDao是1个MapperProxy类的实例。
看下MapperProxy的源码,没错,实现了InvocationHandler,说明使用了jdk自带的动态代理。
public
class
MapperProxy<T> implements
InvocationHandler, Serializable {
private
static
final
long serialVersionUID = -6424540398559729838L;
private
final SqlSession sqlSession;
private
final Class<T> mapperInterface;
private
final Map<Method, MapperMethod> methodCache;
public
MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
try {
return method.invoke(this, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
private MapperMethod cachedMapperMethod(Method method) {
MapperMethod mapperMethod = methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method, mapperMethod);
}
return mapperMethod;
}
}
注:了解概念看到这里就可以了,如果需要详细研究可以继续看下面的内容。
下面开始分析MapperScannerConfigurer的源码
MapperScannerConfigurer实现了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口是一个可以修改spring工长中已定义的bean的接口,该接口有个postProcessBeanDefinitionRegistry方法。
然后我们看下ClassPathMapperScanner中的关键是如何扫描对应package下的接口的。
其实MapperScannerConfigurer的作用也就是将对应的接口的类型改造为MapperFactoryBean,而这个MapperFactoryBean的属性mapperInterface是原类型。MapperFactoryBean本文开头已分析过。
所以最终我们还是要分析MapperFactoryBean的实现原理!
MapperFactoryBean继承了SqlSessionDaoSupport类,SqlSessionDaoSupport类继承DaoSupport抽象类,DaoSupport抽象类实现了InitializingBean接口,因此实例个MapperFactoryBean的时候,都会调用InitializingBean接口的afterPropertiesSet方法。
DaoSupport的afterPropertiesSet方法:
MapperFactoryBean重写了checkDaoConfig方法:
然后通过spring工厂拿对应的bean的时候:
这里的SqlSession是SqlSessionTemplate,SqlSessionTemplate的getMapper方法:
Configuration的getMapper方法,会使用MapperRegistry的getMapper方法:
MapperRegistry的getMapper方法:
MapperProxyFactory构造MapperProxy:
没错! MapperProxyFactory就是使用了jdk组带的Proxy完成动态代理。
MapperProxy本来一开始已经提到。MapperProxy内部使用了MapperMethod类完成方法的调用:
下面,我们以UserDao的getById方法来debug看看MapperMethod的execute方法是如何走的。
@Test
public
void
testGet() {
int id = 1;
System.out.println(userDao.getById(id));
}
<select id="getById" parameterType="int" resultType="org.format.dynamicproxy.mybatis.bean.User">
SELECT * FROM users WHERE id = #{id}
</select>


mybatic MapperScannerConfigurer的原理的更多相关文章
- MapperScannerConfigurer的原理
原文地址:http://www.mybatis.org/spring/zh/mappers.html#MapperScannerConfigurer 为了代替手工使用 SqlSessionDaoSup ...
- Mybatis的体系结构(转载)
原文:http://blog.csdn.net/hupanfeng/article/details/9068003/ MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这 ...
- java动态代理浅析
最近在公司看到了mybatis与spring整合中MapperScannerConfigurer的使用,该类通过反向代理自动生成基于接口的动态代理类. 于是想起了java的动态代理,然后就有了这篇文章 ...
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...
- 转:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
原文地址:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析 前言 本文将分析mybatis与spring整合的MapperScannerConfigur ...
- MapperScannerConfigurer
MapperScannerConfigurer转自:http://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigur ...
- 170111、MapperScannerConfigurer处理过程源码分析
前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...
- ssm框架搭建流程及原理分析
这几天自己想搭建个ssm框架玩一下,有些东西长时间不玩都给忘了,所以自己把整个流程整理了一下,只要跟着步骤,就能顺利完成ssm框架的搭建. 一.搭建步骤: 1.整理jar包 2.对于一个web ...
- 2017.2.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解
深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二) ------配置文件详解 8.2 MyBatis-Spring应用 8.2.1 概述 本文主要讲述通过注解配置MyBa ...
随机推荐
- 2019牛客暑期多校训练营(第六场)-D Move
题目链接:https://ac.nowcoder.com/acm/contest/886/D 题意:给n个物品,每个物品有一个体积值,K个箱子,问箱子的最小体积为多少可将物品全部装下. 思路:比赛时一 ...
- rtsp学习参考资料1
转载于:http://itindex.net/detail/51966-%E6%B5%B7%E5%BA%B7-rtsp-%E5%AE%A2%E6%88%B7%E7%AB%AF 海康相机RTSP连接代码 ...
- oracle——学习之路(SQL基础)
使用create语句创建表 create table 表名 ( 列名 类型 [null | not null], 列名 类型 [null | not null] ) 在 ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- python基础学习日记(一)注释(二)算术运算符(三)变量的基本使用
一.python程序的注释 注释部份程序运行时不起作用.用于说明代码的用途 1.单行注释 # 开始的一行文字,为单行注释 # 单行注释 print("hello python") ...
- 并不对劲的复健训练-CF1187D
题目大意 有两个长度为\(n\)的序列\(a_1,...,a_n\),\(b_1,...,b_n\)(\(a,b\leq n\leq 3\times 10^5\) ).一次操作是选取 \([l,r]\ ...
- centos7 源码安装 MongoDb
1.下载源码包 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.12.tgz 2.解压 放到 /usr/local/ ...
- docker推送镜像到docker本地仓库报错:http: server gave HTTP response to HTTPS client
因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误. 解决办法: vim / ...
- hdu 5651 重复全排列+逆元
知识点: n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列. 若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为 ,即其重复度为ni! ...
- 学生管理系统利用arrayList第二次优化
package StuManage; public class Student { private String name;//姓名 private String stuNum;//学号 privat ...