在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添加的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相关的逻辑。

最新文章

  1. MySQL基础学习(二) 常用SQL命令
  2. EasyUI之Form load函数IE8下设置Radio或Checkbox的BUG
  3. DP-母函数
  4. RPC进阶篇
  5. Ajax、Comet与Websocket
  6. MySQL 子查询 EXISTS 和 NOT EXISTS(转)
  7. HttpClient(4.3.5) - HTTP Protocol Interceptors
  8. yii2 gii页面404和debug调试栏无法显示解决方法
  9. 利用servicestack连接redis
  10. 2.安装Nginx
  11. cogs 2235 烤鸡翅
  12. P3366 【模板】最小生成树
  13. LwIP Application Developers Manual3---链路层和网络层协议之IPV6,ICMP,IGMP
  14. 3H - 进制转换
  15. js中的同步与异步的问题
  16. React Native 入门笔记一 -- Windows下基本环境配置
  17. HTML:链接标签的使用
  18. 【Html 学习笔记】第八节——表单实践
  19. ffmpeg新老接口对比
  20. c++重在运算符

热门文章

  1. 公司-ofo:ofo
  2. 基于MFC的Media Player播放器的制作(3---功能实现)
  3. 19-vim-分屏命令-01-末行命令扩展
  4. 将 XML 架构(XSD)附加到Word文档
  5. iOS音频Error
  6. 2018-2-13-win10-uwp-魔力鬼畜
  7. elasticsearch启动常见问题
  8. Postfix+Dovecot+MySQL搭建邮件服务器
  9. Python操作MySQL实战案例讲解
  10. HDU-3001 TSP+三进制DP