栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis生成代理对象的过程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis生成代理对象的过程

版本

    org.mybatis
    mybatis
    3.5.4

两个重要的类 MapperProxy

mybatis的mapper是通过jdk动态代理实现的,而 InvocationHandler 的实现类是org.apache.ibatis.binding.MapperProxy。

MapperMethod

Mapper类的方法,实际时调用了 MapperMethod 的 execute 方法。

生成代理对象的调用时序
  1. SqlSession 的 getMapper(Class type) 为起点,返回的是 a mapper bound to this SqlSession。

  2. 调用 Configuration 的 getMapper(Class type, SqlSession sqlSession)。

  3. 调用 MapperRegistry 的 getMapper(Class type, SqlSession sqlSession):
    根据 type 在 knownMappers(Map类型) 中获取 MapperProxyFactory 实例对象。
    knownMappers 需要先完成初始化,要调用addMapper;knownMappers key为Class,value为 MapperProxyFactory

  4. 调用 MapperProxyFactory 的 newInstance(SqlSession sqlSession) 生成并返回Mapper对象。具体如下:
    调用 new MapperProxy(sqlSession, mapperInterface, methodCache) 获取 InvocationHandler 的实现类 MapperProxy;
    调用 Proxy.newProxyInstance 返回jdk动态代理对象。

补充:MapperProxy实现了InvocationHandler接口,调用invoke方法时,如果 methodCache 没有缓存,则先缓存。
MapperProxy#methodCache类型为Map
methodCache是被保存在MapperProxy对应的MapperProxyFactory中。

MapperMethodInvoker 两个实现类的构造器参数分析
  • 需要代理的方法
    • 如果是default方法,返回的是 DefaultMethodInvoker 类型对象,其构造器参数是 MethodHandle。
    • 如果不是default方法,返回的是 PlainMethodInvoker 类型对象,其构造器参数是 MapperMethod
  • 如果是 PlainMethodInvoker,最终调用的是 MapperMethod 的 execute 方法。
MapperMethod 的 execute 方法 如果Mapper调用的是select方法

处理参数后,调用 sqlSession 中的 select 方法,有三个实现类,
接下来可以了解,SqlSessionTemplate、SqlSessionManager有用到了装饰者模式,还有 DefaultSqlSession。

DefaultSqlSession 的 selectList 分析
  1. 通过 configuration 获取 MappedStatement。
  2. 调用 Executor 的 query。
MapperProxy只有一个构造方法
public class MapperProxy implements InvocationHandler, Serializable {
    
    private final SqlSession sqlSession;
    private final Class mapperInterface;
    private final Map methodCache;
    public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) {
        this.sqlSession = sqlSession;
        //MapperProxyFactory中只有这两个字段,methodCache 在调用本类的 cachedInvoker 时缓存的。
        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 {
                //获取缓存方法(没有则生成),然后调用。cachedInvoker返回的是 MapperMethodInvoker 对象。
                return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
            }
        } catch (Throwable t) {
            throw ExceptionUtil.unwrapThrowable(t);
        }
    }
    
    interface MapperMethodInvoker {
        Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable;
    }
}
实例的创建在 MapperProxyFactory#newInstance
public class MapperProxyFactory {

    private final Class mapperInterface;
    private final Map methodCache = new ConcurrentHashMap<>();

    public MapperProxyFactory(Class mapperInterface) {
        this.mapperInterface = mapperInterface; 
    }

    public Class getMapperInterface() {
        return mapperInterface;
    }

    public Map getMethodCache() {
        return methodCache;
    }

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

    public T newInstance(SqlSession sqlSession) {
        final MapperProxy mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
        return newInstance(mapperProxy);
    }

}
public class MapperRegistry {

  private final Configuration config;
  
  private final Map, MapperProxyFactory> knownMappers = new HashMap<>();

  public MapperRegistry(Configuration config) {
    this.config = config;
  }

  public  T getMapper(Class type, SqlSession sqlSession) {
    final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) 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);
    }
  }
}

参考:
MyBatis 学习记录3 MapperMethod类

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287092.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号