mybatis源码探究(-)MapperProxyFactory&MapperProxy

春衣少年当酒歌,起舞四顾以笑和。这篇文章主要讲述mybatis源码探究(-)MapperProxyFactory& MapperProxy相关的知识,希望能为你提供帮助。
在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,看名字就知道是工厂方法,一个是protected,一个是public,所以首先看public方法。发现有一个参数SqlSession,SqlSession处理的其实就是执行一次SQL的过程。其实public的newInstance就是new了一个MapperProxy,关于MapperProxy可以先看一下后面的MapperProxy。然后调用了protected的newInstance。
接着我们看protected的newInstance。protected简单明了,就是使用java Proxy的工厂方法生成一个了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源码探究(-)MapperProxyFactory& MapperProxy】总结一下,我们公共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相关的逻辑。

    推荐阅读