【Mybatis】- mapper.java和mapper.xml是如何映射起来的-源码分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ldb987/article/details/88872311

上篇博客我们讲到sqlSession是如何产生的。这篇博客我们讲讲mapper.java和mapper.xml是如何映射起来的。

程序中我们只有mapper.java接口,没有mapper实现类,那么是如何调用方法的呢?

mybatis里所有mapper接口的实现类都可以看做是mapperProxy,mapper代理类,然后调用MapperProxy.invoke()方法,invoke()方法会执行相应sql语句,并将结果返回。

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;
  }

  @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 if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    //开始执行sql语句
    return mapperMethod.execute(sqlSession, args);
  }
}

那么是如何调用的mapperProxy代理类呢?且看一下分解:

1、程序在sqlSession中会调用configuration的getMapper方法

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

2、当时使用configuration的getMapper方法时,会调用mapperRegistry的getMapper方法

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

3、当使用mapperRegistry的getMapper方法时,会调用mapperProxyFactory.newInstance(sqlSession)方法,得到一个MapperProxy对象

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      // 转移到mapperProxyFactory
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

4、在这里,先通过T newInstance(SqlSession sqlSession)方法会得到一个MapperProxy对象,然后调用T newInstance(MapperProxy<T> mapperProxy)生成代理对象然后返回。

  @SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    //所有动态代理类的方法调用,都会交由InvocationHandler接口实现类里的invoke()方法去处理。这是动态代理的关键所在。
    //动态代理我们写的dao接口,第一个参数是类加载器,第二个参数是mapper接口,第三个参数是mapper代理类
    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);

}
  

5、而查看MapperProxy的代码,可以看到如下内容:

public class MapperProxy<T> implements InvocationHandler, Serializable {

  //mapper动态代理实现InvocationHandler接口,重写invoke方法
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        //此处表示InvocationHandler的invoke方法,动态代理最后都会调用InvocationHandler的invoke方法
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    //sqlSession我们在上一篇博客以获取,现在有得到了mapper接口的代理类,所以此处开始执行sql
    return mapperMethod.execute(sqlSession, args);
  }
}

该MapperProxy类实现了InvocationHandler接口,并且实现了该接口的invoke方法。

通过这种方式,我们只需要编写Mapper.java接口类,当真正执行一个Mapper接口的时候,就会转发给MapperProxy.invoke方法,而该方法则会调用后续的sqlSession.cud>executor.execute>prepareStatement等一系列方法,完成SQL的执行和返回。

通过以上的mapperProxy代理类,我们就可以方便的使用mapper.java和mapper.xml了。

猜你喜欢

转载自blog.csdn.net/ldb987/article/details/88872311