Mybatis:Mapper接口编程原理分析(三)
2018-03-30 本文已影响0人
牧呈
在上一篇文章Mybatis:Mapper接口编程原理分析(二)中,已经知道 mapper 接口是怎么注册的了,那么现在就是需要获取 mapper 接口的代理了。
在使用 Mybatis 时,我们都是通过如下代码去获取 mapper 接口的代理的:
sqlSession.getMapper(UserMapper.class)
代码只有一行,就获取了 mapper 接口的代理对象了,那么在内部是怎么一回事呢?
- DefaultSqlSession
public <T> T getMapper(Class<T> type) {
// 从 Configuration 获取 mapper 接口代理
return this.configuration.getMapper(type, this);
}
- Configuration
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
// 从 MapperRegistry 获取 mapper 接口代理
return this.mapperRegistry.getMapper(type, sqlSession);
}
- MapperRegistry
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
// 获取 mapper 接口的代理工厂
MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
} else {
try {
// 生成 mapper 代理
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception var5) {
throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
}
}
}
- MapperProxyFactory
public T newInstance(SqlSession sqlSession) {
// MapperProxy 实现了 InvocationHandler
MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
// 获取 mapper 代理
return this.newInstance(mapperProxy);
}
// 生成 mapper 代理对象
protected T newInstance(MapperProxy<T> mapperProxy) {
return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
}
序列图如下:
获取 mapper 代理