文章目錄
Mapper接口的註冊過程
CachedAuthorMapper cachedAuthorMapper = sqlSession.getMapper(CachedAuthorMapper.class);
使用Mybatis框架的時候,我們調用sqlSession的getMapper方法可以獲取到一個Mapper對象,但是我們定義的Mapper是接口,我們要調用方法必須是個實例對象,那麼getMapper方法返回的到底是個什麼呢?其實,getMapper方法返回的是一個我們定義的Mapper接口的動態代理對象。
Mapper接口的註冊時序
SqlSession的getMapper方法
實際上SqlSession將功能委託給了Configuration的getMapper對象。Configuration對象又將方法委託給了MapperRegistry的getMapper對象。
@Override
public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
MapperRegistry的getMapper方法
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
MapperRegistry的getMapper方法首先從knownMappers這個Map對象中獲取指定Mapper類型的MapperProxyFactory對象。那麼這個knownMappers中的元素是什麼時候註冊上去的呢?其實在構造Configuration對象,解析Mybatis主配置文件的時候,解析Mappers節點時會調用MapperRegistry的addMapper方法,這個時候會註冊Mapper類型和其對應的MapperProxyFactory對象。由MapperProxyFactory生成Mapper的動態代理對象。
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}
MapperProxyFactory
MapperProxyFactory是MapperProxy對象的工廠類,其封裝了MapperProxy的創建過程。在newInstance方法中,MapperProxyFactory使用jdk動態代理生成Mapper對象的動態代理對象。
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<>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
MapperProxy
MapperProxy對象實現了InvocationHandler接口,根據動態代理的特性,在調用目標對象的方法時,方法執行會被攔截,轉而執行代理對象的invoke方法,在此方法中,調用SqlSession對象來執行實際的數據庫交互。
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
// 從Object類繼承的方法不做處理
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else if (method.isDefault()) {
if (privateLookupInMethod == null) {
return invokeDefaultMethodJava8(proxy, method, args);
} else {
return invokeDefaultMethodJava9(proxy, method, args);
}
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
// 對Mapper接口中定義的方法進行封裝,生成MapperMethod對象
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}