mybatis源碼探究(-)MapperProxyFactory&MapperProxy

來源:互聯網
上載者:User

標籤:div   amp   checked   exception   bounds   cached   nal   uid   cat   

在MyBatis中MapperProxyFactory,MapperProxy,MapperMethod是三個很重要的類。

弄懂了這3個類你就大概清楚Mapper介面與SQL的映射,

為什麼是介面,沒有執行個體類也可以完成注入或者調用

其中MapperMethod可以參考:MapperMethod源碼分析傳送門

在調用MyBatis的addMapper的時候如果你跟蹤源碼就會最終跟到MapperRegistry的addMapper中有如下的語句:

knownMappers.put(type, new MapperProxyFactory<T>(type));

 

type就是Mapper介面。下面我們來看一下MapperProxyFactory的源碼。

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源碼分析傳送門

然後就是2個newInstance,看名字就知道是Factory 方法,一個是protected,一個是public,所以首先看public方法。發現有一個參數SqlSession,SqlSession處理的其實就是執行一次SQL的過程。其實public的newInstance就是new了一個MapperProxy,關於MapperProxy可以先看一下後面的MapperProxy。然後調用了protected的newInstance。

接著我們看protected的newInstance。protected簡單明了,就是使用Java Proxy的Factory 方法產生一個了Mapper介面的代理類。我想都關係MyBatis源碼了應該對Java的Proxy動態代理方式應該非常熟悉了。如果不熟悉可以參考一下我之前寫的一篇關於Java動態代理的Java動態代理細探

我們指定MapperProxy實現了InvocationHandler,所以調用Mapper介面中的方法走的是MapperProxy的invoke。而MapperProxy的invoke是把Method封裝成了MapperMethod,MapperMethod處理了Mapper介面方法與xml映射的關係。是不是串聯起來了。

MapperProxy
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動態代理。

既然實現了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相關的邏輯。

mybatis源碼探究(-)MapperProxyFactory&MapperProxy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.