美文网首页
mapper接口代理对象、MapperProxy

mapper接口代理对象、MapperProxy

作者: BenjaminCool | 来源:发表于2019-12-22 17:44 被阅读0次

事情要从MapperFactoryBean开始说起

image.png image.png

所以MapperFactoryBean对象绑定一个SqlSessionTemplate对象;

image.png

getObject方法获取mapper接口的代理对象

image.png image.png image.png image.png image.png image.png

关于mapper接口的注册、解析

MapperRegistry

image.png

MapperProxy实现了InvocationHandler接口

 @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {

// 1、 如果是Object类定义的方法, 且子类没有重写, 比如hashCode,toString,equals等方法, 则直接走Object类的方法
      if (Object.class.equals(method.getDeclaringClass())) { 
        return method.invoke(this, args);
//2、 如果是接口默认方法, 因为接口允许实现静态方法、默认方法; 非抽象方法的调用说明用户已经自己实现了代码逻辑, 所以不用代理非抽象方法
      } else if (isDefaultMethod(method)) {
// 直接调用接口默认方法
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }

// 3、 代理接口抽象方法, 用MapperMethod类来实现
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

  private MapperMethod cachedMapperMethod(Method method) {
// MapperProxyFactory.methodCache保存了方法对应的MapperMethod实例,
// 如果方法对应的MapperMethod实例已经存在,则复用
// 否则, 创建一个新的MapperMethod实例,并缓存到MapperProxyFactory.methodCache

    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
      mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
      methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
  }

相关文章

网友评论

      本文标题:mapper接口代理对象、MapperProxy

      本文链接:https://www.haomeiwen.com/subject/xzmonctx.html