mapper接口代理对象、MapperProxy

事情要从MapperFactoryBean开始说起

4165335-717bcacc7630fe1c.png
image.png
4165335-f70a8b20bdfdcd1a.png
image.png

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

4165335-afa70d94a3c0169c.png
image.png

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

4165335-1fa3c6802f9044dd.png
image.png
4165335-0e6ebfebff62bdcd.png
image.png
4165335-990fd65004e379b4.png
image.png
4165335-ad5b05fa96a2894d.png
image.png
4165335-32c8533ce1069c19.png
image.png
4165335-ebc99801551e31b4.png
image.png

关于mapper接口的注册、解析

MapperRegistry

4165335-9c0617cc69330e38.png
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;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值