zoukankan      html  css  js  c++  java
  • MyBatis-获取 xxxMapper(源码)

    Main 方法,mybatis 版本为 3.5.0

    使用 MapperProxyFactory 创建一个 MapperProxy 的代理对象

    代理对象里面包含了 DefaultSqlSession(Executor)

    InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    session = sqlSessionFactory.openSession();
    DeptMapper mapper = session.getMapper(DeptMapper.class);

    session.getMapper(DeptMapper.class)

    org.apache.ibatis.session.defaults.DefaultSqlSession

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

    configuration.<T>getMapper(type, this)

    org.apache.ibatis.session.Configuration

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

    mapperRegistry.getMapper(type, sqlSession)

    org.apache.ibatis.binding.MapperRegistry

    // 返回代理类
    @SuppressWarnings("unchecked")
    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 {
            return mapperProxyFactory.newInstance(sqlSession);
        } catch (Exception e) {
            throw new BindingException("Error getting mapper instance. Cause: " + e, e);
        }
    }

    mapperProxyFactory.newInstance(sqlSession)

    org.apache.ibatis.binding.MapperProxyFactory

    @SuppressWarnings("unchecked")
    protected T newInstance(MapperProxy<T> mapperProxy) {
        // 用JDK自带的动态代理生成映射器
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
    }
    
    public T newInstance(SqlSession sqlSession) {
        final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
        return newInstance(mapperProxy);
    }

    new MapperProxy<>(sqlSession, mapperInterface, methodCache)

    org.apache.ibatis.binding.MapperProxy

    /**
     * 映射器代理
     */
    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 {
                // 代理以后,所有 Mapper 的方法调用时,都会调用这个 invoke 方法
                // 并不是任何一个方法都需要执行调用代理对象进行执行,如果这个方法是 Object 中通用的方法(toString、hashCode等)则无需执行
                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);
            }
            // 去缓存中找 MapperMethod
            final MapperMethod mapperMethod = cachedMapperMethod(method);
            // 执行
            return mapperMethod.execute(sqlSession, args);
        }
    
        // 去缓存中找 MapperMethod
        private MapperMethod cachedMapperMethod(Method method) {
            return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
        }
    
        private Object invokeDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable {
            final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            final Class<?> declaringClass = method.getDeclaringClass();
            return constructor
                    .newInstance(declaringClass, MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
                    .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
        }
    
        /**
         * Backport of java.lang.reflect.Method#isDefault()
         */
        private boolean isDefaultMethod(Method method) {
            return (method.getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC && method.getDeclaringClass().isInterface();
        }
    }

    时序图


    https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis

  • 相关阅读:
    Failure [INSTALL_FAILED_ALREADY_EXISTS]
    在CentOS 7上构建RAID5、LVM和SAMBAserver(5)——架设SAMBAserver
    adb命令具体解释(二)——手机缺失sqlite3时操作数据库的多种解决方式
    【环境配置】Linux的经常使用命令
    windowsclient开发--使你的client执行时记住上次关闭的大小和位置
    【13】以对象管理资源
    C++ 初始化与赋值
    C++ 变量初始化规则
    C++ 构造和析构
    【20】宁以pass-by-reference-to-const替换pass-by-value
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10560794.html
Copyright © 2011-2022 走看看