在MyBatis中Mapper文件中的方法和xml配置文件中的SQL映射最重要的3個類就是MapperProxyFactory,MapperProxy,MapperMethod了。java
弄懂了這3個類你就Mapper接口與SQL的映射,爲何是接口,沒有實例類也能夠完成注入或者調用。sql
其中MapperMethod能夠參考:MapperMethod源碼分析傳送門緩存
在調用MyBatis的addMapper的時候若是你跟蹤源碼就會最終跟到MapperRegistry的addMapper中有以下的語句:app
knownMappers.put(type, new MapperProxyFactory<T>(type));
type就是Mapper接口。下面咱們來看一下MapperProxyFactory的源碼。源碼分析
public class MapperProxyFactory<T> { private final Class<T> mapperInterface; private Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>(); public MapperProxyFactory(Class<T> mapperInterface) { this.mapperInterface = mapperInterface; } public Class<T> getMapperInterface() { return mapperInterface; } public Map<Method, MapperMethod> getMethodCache() { return methodCache; } @SuppressWarnings("unchecked") 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); } }
MapperProxyFactory一看名字咱們就知道確定是一個工廠類,就是爲了生成MapperProxy。其實MapperProxyFactory也很是簡單。首先看2個成員mapperInterface就是Mapper接口,methodCache就是對Mapper接口中的方法和方法的封裝類(MapperMethod)的映射。MapperMethod處理的事情主要就是:處理Mapper接口中方法的註解,參數,和返回值。若是想了解更多的細節能夠參考MapperMethod源碼分析傳送門this
而後就是2個newInstance,看名字就知道是工廠方法,一個是protected,一個是public,因此首先看public方法。發現有一個參數SqlSession,SqlSession處理的其實就是執行一次SQL的過程。其實public的newInstance就是new了一個MapperProxy,關於MapperProxy能夠先看一下後面的MapperProxy。而後調用了protected的newInstance。.net
接着咱們看protected的newInstance。protected簡單明瞭,就是使用Java Proxy的工廠方法生成一個了Mapper接口的代理類。我想都關係MyBatis源碼了應該對Java的Proxy動態代理方式應該很是熟悉了。若是不熟悉能夠參考一下我以前寫的一篇關於Java動態代理的Java動態代理細探 代理
咱們指定MapperProxy實現了InvocationHandler,因此調用Mapper接口中的方法走的是MapperProxy的invoke。而MapperProxy的invoke是把Method包裝成了MapperMethod,MapperMethod處理了Mapper接口方法與xml映射的關係。是否是串聯起來了。code
public class MapperProxy<T> implements InvocationHandler, Serializable { private static final long serialVersionUID = -6424540398559729838L; private final SqlSession sqlSession; private final Class<T> mapperInterface; private final Map<Method, MapperMethod> methodCache; public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) { this.sqlSession = sqlSession; this.mapperInterface = mapperInterface; this.methodCache = methodCache; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (Object.class.equals(method.getDeclaringClass())) { try { return method.invoke(this, args); } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } } final MapperMethod mapperMethod = cachedMapperMethod(method); return mapperMethod.execute(sqlSession, args); } private MapperMethod cachedMapperMethod(Method method) { MapperMethod mapperMethod = methodCache.get(method); if (mapperMethod == null) { mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()); methodCache.put(method, mapperMethod); } return mapperMethod; } }
咱們看MapperProxy實現了InvocationHandler接口,不用仔細想,基本上是由於Java動態代理。xml
既然實現了InvocationHandler接口那麼固然要先看一下invoke方法了。首先檢查了若是是Object中方法就直接調用方法自己。
若是不是就把方法Method包裝成MapperMethod,咱們前面已經提到了MapperMethod主要就是處理方法的註解,參數,返回值,參數與SQL語句中的參數的對應關係。再次打一波廣告,若是像瞭解更多MapperMethod的細節能夠參考MapperMethod源碼分析傳送門
由於把Method處理爲MapperMethod仍是一個比較重的操做,因此這裏作了緩存處理。
總結一下,咱們公共MyBatis添加的Mapper的操做實際上添加的是MapperProxyFactory,這個是MapperProxy的工廠類,可是MapperProxyFactory生產的也不是MapperProxy,而是Mapper的Proxy代理。使用的InvocationHandler是MapperProxy,MapperProxy的invoke方法其實是把Mapper接口方法包裝爲了MapperMethod,並執行的MapperMethod的execute方法。MapperMethod處理的邏輯是Mapper方法與xml中的SQL的一些映射關係。例如@MapKey等註解的處理,一些如RowBounds特殊參數的處理以及一些其餘關於Mapper方法與SQL映射的處理。執行SQL的邏輯實際上是委託給了SqlSession相關的邏輯。