mybatis源码分析(1) 如何实现动态代理

1、mybatis动态代理框架

在这里插入图片描述

2、动态代理简单介绍
  • 通过实现 InvocationHandler 接口创建自己的调用处理器;
  • 通过为 Proxy 类指定 ClassLoader 对象和一组 interface 来创建动态代理类;
  • 通过反射机制获得动态代理类的构造函数,其唯一参数类型是调用处理器接口类型;
  • 通过构造函数创建动态代理类实例,构造时调用处理器对象作为参数被传入。

这边有一篇博文很好的介绍了动态代理如何使用:点此跳转

3、mybatis动态代理代码实现

我们一般调用时会使用

UserDao mapper = session.getMapper(UserDao.class); 因为SqlSesseion为接口,默认的SqlSession结构的实现是DefaultSqlSession

这时我们看到了实际上是调用的DefaultSqlSession类的getMapper函数。DefaultSqlSession类的getMapper函数很简短,实际上也只是做了一层封装,实际是在Configuration类中实现

  public <T> T getMapper(Class<T> type) {
    return configuration.getMapper(type, this);
  }

我们继续看到Configuration类,同样也是做了一层封装,实现在MapperRegistry类中,并且在Configuration类中已经传入了当前的sqlSession,这样对于不同的sqlSession可以进行维度分离

 public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

继续深入进入来到MapperRegistry,这一层的函数中出现了MapperProxyFactory类,这时MapperProxy的生产工厂类,而MapperProxy就是我们所要关注的代理类

 public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
 	//在一开始我们利用XMLConfigBuild类读取配置信息时会将一个class
 	//和生成一个MapperProxyFactory一起放入knownMappers的map表中
 	//这样我们对于每一个dao层的类,都有一个MapperProxyFactory来对应他
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      //最需要关注的函数,这个函数会生成一个代理对象
      //我们最终可以通过这个返回的代理对象来进行操作
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

正如mapperProxyFactory.newInstance(sqlSession)是最需要关注的函数,他在函数会传入sqlSession环境,同样对不同sqlSession做出了区分。接下来我们来看下这个函数的实现

public T newInstance(SqlSession sqlSession) {
	//根据传入信息生成一个代理对象
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethodInvoker> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;  //具体方法实现的缓存
  }


 protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

利用上面三个函数就是生成了一个代理对象,这里相信一定会有疑问,这边还没进行委托类的委托类实现的绑定,利用methodCache这个一开始为空的,这样我们应该怎么找到一个方法的实现呢?

4、代理方法的注入

利用代理的性质,我们在外部调用接口函数的时候,具体会走到代理类的invoke函数,我们看到Proxy.newProxyInstance中传入了mapperProxy这个代理类,那么就会调用这个类的invoke函数

 @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      //如果是一般的函数,可以直接调用
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      } else {
      	//调用特定的函数,首先会进入到cachedInvoker(method)中
        return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
  }

来看下cachedInvoker函数,这个函数会返回具体代理类对象,也就是真正需要进行invoke调用的对象MapperMethodInvoker,

  private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
    try {
     //这是一个如果没有就添加的函数,
      return methodCache.computeIfAbsent(method, m -> {
        if (m.isDefault()) {
          try {
            if (privateLookupInMethod == null) {
              return new DefaultMethodInvoker(getMethodHandleJava8(method));
            } else {
              return new DefaultMethodInvoker(getMethodHandleJava9(method));
            }
          } catch (IllegalAccessException | InstantiationException | InvocationTargetException
              | NoSuchMethodException e) {
            throw new RuntimeException(e);
          }
        } else {
          return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
        }
      });
    } catch (RuntimeException re) {
      Throwable cause = re.getCause();
      throw cause == null ? re : cause;
    }
  }

但是MapperMethodInvoker也只是一个代理类的接口,具体实现在PlainMethodInvoker上

public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
      return mapperMethod.execute(sqlSession, args);
    }

PlainMethodInvoker类的invoke函数也会调用MapperMethod的execute函数来执行真正的sql语句。在新建MapperMethod函数时,会根据配置信息创建SqlCommand(用来标识sql语句是什么类型,例如insert,delete,select等)和MethodSignature(这个类中会保存sql的一些数据,例如传入参数的类型,传出参数的类型等),具体sql执行在execute中执行

public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    switch (command.getType()) {
      case INSERT: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.insert(command.getName(), param));
        break;
      }
      case UPDATE: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.update(command.getName(), param));
        break;
      }
      case DELETE: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.delete(command.getName(), param));
        break;
      }
      case SELECT:
        if (method.returnsVoid() && method.hasResultHandler()) {
          executeWithResultHandler(sqlSession, args);
          result = null;
        } else if (method.returnsMany()) {
          result = executeForMany(sqlSession, args);
        } else if (method.returnsMap()) {
          result = executeForMap(sqlSession, args);
        } else if (method.returnsCursor()) {
          result = executeForCursor(sqlSession, args);
        } else {
          Object param = method.convertArgsToSqlCommandParam(args);
          result = sqlSession.selectOne(command.getName(), param);
          if (method.returnsOptional()
              && (result == null || !method.getReturnType().equals(result.getClass()))) {
            result = Optional.ofNullable(result);
          }
        }
        break;
      case FLUSH:
        result = sqlSession.flushStatements();
        break;
      default:
        throw new BindingException("Unknown execution method for: " + command.getName());
    }
    if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
      throw new BindingException("Mapper method '" + command.getName()
          + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
    }
    return result;
  }

这边sql的执行时通过对应的sqlsession来调用相应的执行器来执行,这边因为篇幅就不细讲了。

这样,我们就可以在xml中定义一个sql语句,然后在dao层创建一个对应的接口,要注意xml中的namespace必须和dao层的全限定地址相同,传入参数传出参数和xml中定义相同,这样我们就只需要定义一个接口,mybatis会自动帮我们进行动态代理来进行调用执行sql语句。

其实动态代理本质上就是讲函数调用在程序运行时才感知到,不会在编译期就知道了,这样有利于程序的复用。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值