mybatis源码学习之执行过程分析(3)——mapper接口的获取
来源:互联网 发布:公章可以在淘宝上刻吗 编辑:程序博客网 时间:2024/06/10 19:14
在 mybatis源码学习之执行过程分析(1)——SqlSessionFactory及SqlSession的创建中,跟踪到openSession()
方法返回了DefaultSqlSession,并持有Configuration的引用(Configuration的实例化见mybatis源码学习——Configuration类及其初始化过程、TypeHandler、TypeAlias)。
当我们通过session.getMapper(UserMapper.class)
获取Mapper接口时,其实调用了Configuration中的getMapper()
方法。
回过头来看我们的代码:
SqlSession session = sqlSessionFactory.openSession();UserMapper userMapper = session.getMapper(UserMapper.class);
DefaultSqlSession.javapublic <T> T getMapper(Class<T> type) { return this.configuration.getMapper(type, this);}
可以看到是调用了Configuration中的getMapper()方法。在这里是因为Mapper信息保存在MapperRegistry中的final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap()
中(详细过程见Configuration的实例化和xml的解析的分析)。
Configuration.javapublic <T> T getMapper(Class<T> type, SqlSession sqlSession) { return this.mapperRegistry.getMapper(type, sqlSession);}
MapperRegistry.javapublic <T> T getMapper(Class<T> type, SqlSession sqlSession) { //在这里拿到了Mapper接口所在代理工厂 MapperProxyFactory mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type); if(mapperProxyFactory == null) { throw new BindingException("Type " + type + " is not known to the MapperRegistry."); } else { try { return mapperProxyFactory.newInstance(sqlSession); } catch (Exception var5) { throw new BindingException("Error getting mapper instance. Cause: " + var5, var5); } }} protected T newInstance(MapperProxy<T> mapperProxy) { return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); } public T newInstance(SqlSession sqlSession) { final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache); return newInstance(mapperProxy); }
可以看到就是对HashMap的get()操作,并转换为MapperProxyFactory。
至此我们就拿到了UserMapper接口。因为它被MapperProxyFactory中的mapperInterface所引用,获取过程如下图:
public class MapperProxyFactory<T> { private final Class<T> mapperInterface; private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>(); ...}
总结
至此mapper接口的获取就结束了,可以看到实质上拿到的是一个MapperProxy代理类。
下面继续分析接口中方法的调用(即代理的调用)。
0 0
- mybatis源码学习之执行过程分析(3)——mapper接口的获取
- mybatis源码学习之执行过程分析(2)——config.xml配置文件和mapper.xml映射文件解析过程
- mybatis源码学习之执行过程分析(4)——映射文件中sql的获取和sql语句的执行
- mybatis源码学习之执行过程分析(1)——SqlSessionFactory及SqlSession的创建
- mybatis源码学习之执行过程分析(5)——sql执行后ResultSet的处理及结果返回
- mybatis源码学习之执行过程分析(0)——配置文件加载(io包)
- Mybatis源码分析获取Mapper
- 【Mybatis源码剖析】Spring中获取 Mybatis Mapper接口(注解Autowired),并调用过程剖析
- mybatis 源码学习之getMapper过程分析
- Mybatis 源码解析二、Mapper接口的代理实现过程 MapperScannerConfigurer 解析
- Hadoop中Mapper过程的源码分析
- [Mybatis] Mapper接口原理分析
- mybatis源码分析之Mapper代理实现分析
- Mybatis的使用(mapper接口方式)
- Mybatis源码分析之执行完整分析
- mybatis源码分析——SqlSessionFactory实例的产生过程
- MyBatis源码分析——SqlSessionFactory实例的产生过程
- Mybatis源码分析(一)--Mapper的动态代理
- 利用反射实现类的复制
- 用C语言输出由*组成的菱形
- obj.currentStyle.property、window.getComputedStyle(obj,null).property、obj.style.property 获取与元素属性的总结
- SAP接口编程之 NCo3.0系列(07) : 异常处理
- ruby函数调用
- mybatis源码学习之执行过程分析(3)——mapper接口的获取
- powershell读CSV写入sql server数据库
- [iOS]过渡动画之高级模仿 airbnb
- SAP接口编程 之 JCo3.0系列(01):JCoDestination
- 图像处理中的特征系列——HOG特征
- iOS黑魔法-Method Swizzling
- Linux下安装jdk8步骤详述
- 不同平台windows、linux、mac 上换行符的问题
- Visual Studio 开发web应用程序时如何在项目中引用自定义类