mybatis源码学习之执行过程分析(3)——mapper接口的获取
2017-01-07 00:46
796 查看
在 mybatis源码学习之执行过程分析(1)——SqlSessionFactory及SqlSession的创建中,跟踪到
当我们通过
回过头来看我们的代码:
可以看到是调用了Configuration中的getMapper()方法。在这里是因为Mapper信息保存在MapperRegistry中的
可以看到就是对HashMap的get()操作,并转换为MapperProxyFactory。
至此我们就拿到了UserMapper接口。因为它被MapperProxyFactory中的mapperInterface所引用,获取过程如下图:
下面继续分析接口中方法的调用(即代理的调用)。
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.java public <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.java public <T> T getMapper(Class<T> type, SqlSession sqlSession) { return this.mapperRegistry.getMapper(type, sqlSession); }
MapperRegistry.java public <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代理类。下面继续分析接口中方法的调用(即代理的调用)。
相关文章推荐
- mybatis源码学习之执行过程分析(4)——映射文件中sql的获取和sql语句的执行
- mybatis源码学习之执行过程分析(2)——config.xml配置文件和mapper.xml映射文件解析过程
- Mybatis源码分析之Mapper执行SQL过程(三)
- mybatis 源码学习之getMapper过程分析
- mybatis源码学习之执行过程分析(1)——SqlSessionFactory及SqlSession的创建
- mybatis源码学习之执行过程分析(5)——sql执行后ResultSet的处理及结果返回
- mybatis源码学习之执行过程分析(0)——配置文件加载(io包)
- Mybatis执行dao接口方法的流程梳理及源码分析
- Mybatis源码解析-Mapper执行SQL过程
- Mybatis架构设计及源码分析-一条语句执行的过程
- Mybatis 源码解析二、Mapper接口的代理实现过程 MapperScannerConfigurer 解析
- Mybatis源码分析获取Mapper
- Mybatis 源码学习获取接口代理对象
- MyBatis源码学习笔记(十)SQL执行流程分析
- 【Mybatis源码剖析】Spring中获取 Mybatis Mapper接口(注解Autowired),并调用过程剖析
- SprignMVC+myBatis整合+mybatis源码分析+动态代理实现流程+如何根据mapper接口生成其实现类
- MyBatis Mapper 接口如何通过JDK动态代理来包装SqlSession 源码分析
- 【mybatis源码学习】mybatis和spring框架整合,我们依赖的mapper的接口真相
- Mybatis底层原理学习(二):从源码角度分析一次查询操作过程
- Mybatis3源码分析(19)-Mapper生成过程-示例