MyBatis源码(3.2.8)之:MapperProxyFactory 与 MapperProxy

简介

在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的源码。源码分析

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

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动态代理。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相关的逻辑。

相关文章
相关标签/搜索