SqlSession可以說是整個MyBatis的重中之重,在SqlSession中涉及到前一篇四大對象:Executor、StatementHandler、ParameterHandler、ResultHandler,所以在此先只對SqlSession有一個大概的了解。
在代碼中我們可以看到當我們構造出一個SqlSession實例過后,可以通過SqlSession構造出Mappper映射器。UserMapper是一個接口,那么我們可以肯定的是,它一定是用了Java的動態代理生成了一個代理類。
SqlSession sqlSession = SessionFactory.getSqlSession(resource); UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
接着上一篇講到通過DefaultSqlSessionFactory可以得到一個SqlSession的實例DefaultSqlSession。
通過源代碼可以發現,SqlSessionManger又實現了SqlSession,在上一節中可知SqlSessionManager同樣也繼承了SqlSessionFactory接口。我們把它們結合起來看看。
看來這個SqlSessionManager有點神奇,同時繼承SqlSession和SqlSessionFactory。從名字上來看好像是管理SqlSession的,這里不討論,隨着源代碼的閱讀相信我們能逐步清晰,包括整個包的結構。
回到DefaultSqlSession中來。在SqlSession接口中提供了很多方法,用於我們的增刪改查,這在舊版的MyBatis或者iBatis中常常所使用的,我們現在大多直接使用xml配置文件以達到更加靈活的效果。所以我們將注意力放在getMapper方法上。
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
注意UserMapper僅僅是一個接口,這里會涉及到Java的動態代理,所以得要有一定的基礎才能讀懂。
通過打斷點調試我們可以發現確實產生了一個叫MapperProxy的代理類。
下面是DefaultSqlSession的getMapper方法:
//org.apache.ibatis.session.default.DefaultSqlSession
public <T> T getMapper(Class<T> type) { return configuration.<T>getMapper(type, this); }
看起來語法有點奇怪,這是一個泛型方法。看來是調用了Configuration的getMapper方法,還不是DefaultSqlSession實現了getMapper。接着再看Configuration的getMapper方法:
//org.apache.ibatis.session.Configuration
public <T> T getMapper(Class<T> type, SqlSession sqlSession) { return mapperRegistry.getMapper(type, sqlSession); }
Configuration.getMapper一共兩個參數,一個是Class類型,一個是SqlSession,在DefaultSqlSession.getMapper調用Configuration.getMapper時,將傳遞進來的Class類型參數和其本身傳遞給了Configuration.getMapper。此時還不是在Configuration中實現了getMapper,看來還是一個叫做mapperRegistry的變量。
//org.apache.ibatis.session.Configuration
protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
看着名字好像是注冊Mapper映射器的地方,想來也是,既然要得到Mapper的映射,那么所有的Mapper都要一個地方去注冊(在我們的mybytis-config.xml里),注冊好過后需要的時候再去查找是否已經注冊,那么就是MapperRegistry,所以取一個好的變量名是非常重要的。
1 //org.apache.ibatis.binding.MapperRegistry 2 public <T> T getMapper(Class<T> type, SqlSession sqlSession) { 3 final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type); 4 if (mapperProxyFactory == null) { 5 throw new BindingException("Type " + type + " is not known to the MapperRegistry."); 6 } 7 try { 8 return mapperProxyFactory.newInstance(sqlSession); 9 } catch (Exception e) { 10 throw new BindingException("Error getting mapper instance. Cause: " + e, e); 11 } 12 }
在第3行代碼中試圖從一個叫knownMappers的變量取出MapperProxyFactory。這個knownMapper的定義:
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
既然能用get方法取,那說明就有add方法咯?果不其然我們在MapperRegistry類中發現了public <T> void addMapper(Class<T> type)方法,那么是在哪里調用的這個方法呢?
我們來重新理一理。
使用MyBatis的第一步是配置mybatis-config.xml,配置好過后,mybatis-config跑起來的第一步也一定是首先解析xml配置文件,將解析好的配置文件各個配置參數放入Configuration對象中,包括Mapper的配置,所以應該是在解析xml文件的某個類中解析過來后調用Configuration的方法將mapper放置到MapperRegister中。事實也的確如此,有興趣可以跟蹤下代碼看看。回到MapperRegistry.getMapper的方法中。
當我們一切正確時,我們就能獲取到一個MapperProxyFactory實例。想必MapperProxy代理類的生成正是通過MapperProxyFactory工廠類構建的,即第8行代碼。進入MapperProxyFactory類。
//org.apache.ibatis.binding.MapperProxyFactory public T newInstance(SqlSession sqlSession) { final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache); return newInstance(mapperProxy); }
在這里終於看到了MapperProxy代理類,是通過sqlSession、mapperInterface、mechodCache三個參數構造的。
newInstance有一個重載方法:
//org.apache.ibatis.binding.MapperProxyFactory protected T newInstance(MapperProxy<T> mapperProxy) { return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); }
終於是走到頭了,這里就是返回的一個代理類實例。最后來看看MapperProxy。
MapperProxy是一個重要的類,所以我們將其代碼全部貼出:
1 //org.apache.ibatis.binding.MapperProxy 2 public class MapperProxy<T> implements InvocationHandler, Serializable { 3 4 private static final long serialVersionUID = -6424540398559729838L; 5 private final SqlSession sqlSession; 6 private final Class<T> mapperInterface; 7 private final Map<Method, MapperMethod> methodCache; 8 9 public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) { 10 this.sqlSession = sqlSession; 11 this.mapperInterface = mapperInterface; 12 this.methodCache = methodCache; 13 } 14 15 @Override 16 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 17 if (Object.class.equals(method.getDeclaringClass())) { 18 try { 19 return method.invoke(this, args); 20 } catch (Throwable t) { 21 throw ExceptionUtil.unwrapThrowable(t); 22 } 23 } 24 final MapperMethod mapperMethod = cachedMapperMethod(method); 25 return mapperMethod.execute(sqlSession, args); 26 } 27 28 private MapperMethod cachedMapperMethod(Method method) { 29 MapperMethod mapperMethod = methodCache.get(method); 30 if (mapperMethod == null) { 31 mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()); 32 methodCache.put(method, mapperMethod); 33 } 34 return mapperMethod; 35 } 36 37 }
要使用Java的動態代理就必須得實現InvocationHandler接口,在第17行代碼中首先判斷代理對象是一個接口還是一個類,顯然我們沒有對mapper接口進行任何實現,那么它將生成一個MapperMethod對象,接着調用其execute方法,把sqlSession和參數傳遞進去。