框架 - Mybatis 源码一步步深入(三)

简介
上一章我们大概了解了SqlSessionFactoryBuilder和Configuration两个类。这一章我们主要了解XMLConfigBuilder类,它是Mybatis配置文件解析入口。

BaseBuilder 类
在了解XMLConfigBuilder 之前我们先了解一下BaseBuilder

public abstract class BaseBuilder

BaseBuilder是一个抽象类

BaseBuilder 属性

// 上一篇讲过,mybatis所有配置
protected final Configuration configuration;
// 类型别名注册器,里面持有类别名和类的键值对。
protected final TypeAliasRegistry typeAliasRegistry;
// 类型处理注册器。
protected final TypeHandlerRegistry typeHandlerRegistry;

BaseBuilder 构造函数

public BaseBuilder(Configuration configuration) {
    this.configuration = configuration;
    this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
    this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
}

BaseBuilder 初始化时必须要传configuration对象。通过上一篇我们知道Configuration的typeAliasRegistry类型别名注册器和typeHandlerRegistry类型处理注册器是在Configuration初始化时就被创建。

BaseBuilder 获取配置

public Configuration getConfiguration() {
    return configuration;
}

BaseBuilder 字符串转型

protected Pattern parseExpression(String regex, String defaultValue) {
    return Pattern.compile(regex == null ? defaultValue : regex);
}
protected Boolean booleanValueOf(String value, Boolean defaultValue) {
    return value == null ? defaultValue : Boolean.valueOf(value);
}
protected Integer integerValueOf(String value, Integer defaultValue) {
    return value == null ? defaultValue : Integer.valueOf(value);
}
protected Set stringSetValueOf(String value, String defaultValue) {
    value = value == null ? defaultValue : value;
    return new HashSet<>(Arrays.asList(value.split(",")));
}

BaseBuilder 根据别名获取JDBC类型

protected JdbcType resolveJdbcType(String alias) {
    if (alias == null) {
        return null;
    }
    try {
        return JdbcType.valueOf(alias);
    } catch (IllegalArgumentException e) {
        throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
    }
}

BaseBuilder 根据别名获取结果集类型

protected ResultSetType resolveResultSetType(String alias) {
    if (alias == null) {
        return null;
    }
    try {
        return ResultSetType.valueOf(alias);
    } catch (IllegalArgumentException e) {
        throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
    }
}

BaseBuilder 根据类型别名获取对应的类型处理器

protected TypeHandler resolveTypeHandler(Class javaType, String typeHandlerAlias) {
    // 判断类型别名是否为空   
    if (typeHandlerAlias == null) {
        return null;
    }
    // 调用下面resolveClass方法
    Class type = resolveClass(typeHandlerAlias);
    // 类型不为空并且不是一个TypeHandler时抛异常
    if (type != null && !TypeHandler.class.isAssignableFrom(type)) {
        throw new BuilderException("Type " + type.getName() + 
                " is not a valid TypeHandler because it does not implement TypeHandler interface");
    }
    // 到这一定是TypeHandler
    Class> typeHandlerType = (Class>) type;
    // 调用resolveTypeHandler
    return resolveTypeHandler(javaType, typeHandlerType);
}
protected TypeHandler resolveTypeHandler(Class javaType, Class> typeHandlerType) {
    // 判断typeHandlerType 是否为空
    if (typeHandlerType == null) {
        return null;
    }
    // 到类型处理注册器中找这个typeHandlerType
    TypeHandler handler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerType);
    // handler为空就反射出一个
    if (handler == null) {
        handler = typeHandlerRegistry.getInstance(javaType, typeHandlerType);
    }
    // 返回handler
    return handler;
}
protected  Class resolveClass(String alias) {
    // 判断别名是否为空
    if (alias == null) {
        return null;
    }
    try {
        // 调用resolveAlias方法
        return resolveAlias(alias);
    } catch (Exception e) {
        throw new BuilderException("Error resolving class. Cause: " + e, e);
    }
}
protected  Class resolveAlias(String alias) {
    // 到类型别名注册器中找
    return typeAliasRegistry.resolveAlias(alias);
}

BaseBuilder 根据别名获取参数类型

protected ParameterMode resolveParameterMode(String alias) {
    if (alias == null) {
        return null;
    }
    try {
        return ParameterMode.valueOf(alias);
    } catch (IllegalArgumentException e) {
        throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
    }
}

BaseBuilder 创建proxyFactory

protected Object createInstance(String alias) {
    Class clazz = resolveClass(alias);
    if (clazz == null) {
        return null;
    }
    try {
        return resolveClass(alias).getDeclaredConstructor().newInstance();
    } catch (Exception e) {
        throw new BuilderException("Error creating instance. Cause: " + e, e);
    }
}

本篇主要讲XMLConfigBuilder,因为XMLConfigBuilder继承BaseBuilder,所以先讲了BaseBuilder

XMLConfigBuilder 类

public class XMLConfigBuilder extends BaseBuilder

XMLConfigBuilder 是BaseBuilder 的其中一个子类,它的作用是把MyBatis的XML及相关配置解析出来,然后保存到Configuration中。

XMLConfigBuilder 属性

// Configuration解析标识
private boolean parsed;
// 解析器模块
private final XPathParser parser;
// 环境变量
private String environment;
// 反射工厂
private final ReflectorFactory localReflectorFactory = new DefaultReflectorFactory();

XMLConfigBuilder 构造函数

public XMLConfigBuilder(Reader reader) {
    this(reader, null, null);
}
public XMLConfigBuilder(Reader reader, String environment) {
    this(reader, environment, null);
}
public XMLConfigBuilder(Reader reader, String environment, Properties props) {
    this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
}
public XMLConfigBuilder(InputStream inputStream) {
    this(inputStream, null, null);
}
public XMLConfigBuilder(InputStream inputStream, String environment) {
    this(inputStream, environment, null);
}
public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
    this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
}
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
    // 调用父类构造函数
    super(new Configuration());
    // 初始化ErrorContext
    ErrorContext.instance().resource("SQL Mapper Configuration");
    // 设置属性
    this.configuration.setVariables(props);
    // 设置Configuration未解析
    this.parsed = false;
    // 设置环境变量
    this.environment = environment;
    // 设置解析器
    this.parser = parser;
}

所有的构造函数最终都走到最后一个里面,前其实都是为了拿到parser对象,对于XPathParser解析器模块是怎么构造parser的下一篇我们主要盘它。这里创建了Configuration对象,根据前一篇我们知道系统自带的类别名会在它初始化时注册。初始化了ErrorContext,下下篇将它。初始化时并没有

XMLConfigBuilder 核心方法

// Mybatis配置文件核心解析方法
public Configuration parse() {
    // 是否已经解析过,解析过就抛异常
    if (parsed) {
        throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    // 设置解析标志
    parsed = true;
    // 正在的解析方法
    parseConfiguration(parser.evalNode("/configuration"));
    // 返回解析后的configurtaion对象
    return configuration;
}
// Mybatis配置文件解析元素
private void parseConfiguration(XNode root) {
    try {
        // 解析properties节点
        propertiesElement(root.evalNode("properties"));
        // 解析settings节点
        Properties settings = settingsAsProperties(root.evalNode("settings"));
        // 加载settings
        loadCustomVfs(settings);
        loadCustomLogImpl(settings);
        // 解析typeAliases节点
        typeAliasesElement(root.evalNode("typeAliases"));
        // 解析plugins节点
        pluginElement(root.evalNode("plugins"));
        // 解析objectFactory节点
        objectFactoryElement(root.evalNode("objectFactory"));
        // 解析objectWrapperFactory节点
        objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
        // 解析reflectorFactory节点
        reflectorFactoryElement(root.evalNode("reflectorFactory"));
        settingsElement(settings);
        // 解析environments节点
        environmentsElement(root.evalNode("environments"));
        // 解析databaseIdProvider节点
        databaseIdProviderElement(root.evalNode("databaseIdProvider"));
        // 解析typeHandlers节点
        typeHandlerElement(root.evalNode("typeHandlers"));
        // 解析mappers节点
        mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
        throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
}

后面的会针对每个标签解析单独将。

XMLConfigBuilder 解析settings节点

private Properties settingsAsProperties(XNode context) {
    if (context == null) {
        return new Properties();
    }
    Properties props = context.getChildrenAsProperties();
    // Check that all settings are known to the configuration class
    MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
    for (Object key : props.keySet()) {
        if (!metaConfig.hasSetter(String.valueOf(key))) {
            throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
        }
    }
    return props;
}

XMLConfigBuilder 解析settings配置

private void loadCustomVfs(Properties props) throws ClassNotFoundException {
    String value = props.getProperty("vfsImpl");
    if (value != null) {
        String[] clazzes = value.split(",");
        for (String clazz : clazzes) {
            if (!clazz.isEmpty()) {
                @SuppressWarnings("unchecked")
                Class vfsImpl = (Class)Resources.classForName(clazz);
                configuration.setVfsImpl(vfsImpl);
            }
        }
    }
}
private void loadCustomLogImpl(Properties props) {
    Class logImpl = resolveClass(props.getProperty("logImpl"));
    configuration.setLogImpl(logImpl);
}

XMLConfigBuilder 解析typeAliases节点

private void typeAliasesElement(XNode parent) {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeAliasPackage = child.getStringAttribute("name");
                configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
            } else {
                String alias = child.getStringAttribute("alias");
                String type = child.getStringAttribute("type");
                try {
                    Class clazz = Resources.classForName(type);
                    if (alias == null) {
                        typeAliasRegistry.registerAlias(clazz);
                    } else {
                        typeAliasRegistry.registerAlias(alias, clazz);
                    }
                } catch (ClassNotFoundException e) {
                    throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
                }
            }
        }
    }
}

XMLConfigBuilder 解析plugin节点

private void pluginElement(XNode parent) throws Exception {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            String interceptor = child.getStringAttribute("interceptor");
            Properties properties = child.getChildrenAsProperties();
            Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).getDeclaredConstructor().newInstance();
            interceptorInstance.setProperties(properties);
            configuration.addInterceptor(interceptorInstance);
        }
    }
}

XMLConfigBuilder 解析objectFactory节点

private void objectFactoryElement(XNode context) throws Exception {
    if (context != null) {
        String type = context.getStringAttribute("type");
        Properties properties = context.getChildrenAsProperties();
        ObjectFactory factory = (ObjectFactory) resolveClass(type).getDeclaredConstructor().newInstance();
        factory.setProperties(properties);
        configuration.setObjectFactory(factory);
    }
}

XMLConfigBuilder 解析objectWrapperFactory节点

private void objectWrapperFactoryElement(XNode context) throws Exception {
    if (context != null) {
        String type = context.getStringAttribute("type");
        ObjectWrapperFactory factory = (ObjectWrapperFactory) resolveClass(type).getDeclaredConstructor().newInstance();
        configuration.setObjectWrapperFactory(factory);
    }
}

XMLConfigBuilder 解析reflectorFactory节点

private void reflectorFactoryElement(XNode context) throws Exception {
    if (context != null) {
        String type = context.getStringAttribute("type");
        ReflectorFactory factory = (ReflectorFactory) resolveClass(type).getDeclaredConstructor().newInstance();
        configuration.setReflectorFactory(factory);
    }
}

XMLConfigBuilder 解析properties节点

private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
        Properties defaults = context.getChildrenAsProperties();
        String resource = context.getStringAttribute("resource");
        String url = context.getStringAttribute("url");
        if (resource != null && url != null) {
            throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
        }
        if (resource != null) {
            defaults.putAll(Resources.getResourceAsProperties(resource));
        } else if (url != null) {
            defaults.putAll(Resources.getUrlAsProperties(url));
        }
        Properties vars = configuration.getVariables();
        if (vars != null) {
            defaults.putAll(vars);
        }
        parser.setVariables(defaults);
        configuration.setVariables(defaults);
    }
}

XMLConfigBuilder 解析setting中子节点

private void settingsElement(Properties props) {
    configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
    configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));
    configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
    configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));
    configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
    configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), false));
    configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
    configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
    configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
    configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
    configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
    configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
    configuration.setDefaultResultSetType(resolveResultSetType(props.getProperty("defaultResultSetType")));
    configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
    configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
    configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
    configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
    configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
    configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
    configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
    configuration.setDefaultEnumTypeHandler(resolveClass(props.getProperty("defaultEnumTypeHandler")));
    configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
    configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
    configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
    configuration.setLogPrefix(props.getProperty("logPrefix"));
    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
}

XMLConfigBuilder 解析环境节点

private void environmentsElement(XNode context) throws Exception {
    if (context != null) {
        if (environment == null) {
            environment = context.getStringAttribute("default");
        }
        for (XNode child : context.getChildren()) {
            String id = child.getStringAttribute("id");
            if (isSpecifiedEnvironment(id)) {
                TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
                DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
                DataSource dataSource = dsFactory.getDataSource();
                Environment.Builder environmentBuilder = new Environment.Builder(id)
                        .transactionFactory(txFactory)
                        .dataSource(dataSource);
                configuration.setEnvironment(environmentBuilder.build());
            }
        }
    }
}

XMLConfigBuilder 解析databaseIdProvider节点

private void databaseIdProviderElement(XNode context) throws Exception {
    DatabaseIdProvider databaseIdProvider = null;
    if (context != null) {
        String type = context.getStringAttribute("type");
        // awful patch to keep backward compatibility
        if ("VENDOR".equals(type)) {
            type = "DB_VENDOR";
        }
        Properties properties = context.getChildrenAsProperties();
        databaseIdProvider = (DatabaseIdProvider) resolveClass(type).getDeclaredConstructor().newInstance();
        databaseIdProvider.setProperties(properties);
    }
    Environment environment = configuration.getEnvironment();
    if (environment != null && databaseIdProvider != null) {
        String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource());
        configuration.setDatabaseId(databaseId);
    }
}

XMLConfigBuilder 解析环境中transactionManager节点

private TransactionFactory transactionManagerElement(XNode context) throws Exception {
    if (context != null) {
        String type = context.getStringAttribute("type");
        Properties props = context.getChildrenAsProperties();
        TransactionFactory factory = (TransactionFactory) resolveClass(type).getDeclaredConstructor().newInstance();
        factory.setProperties(props);
        return factory;
    }
    throw new BuilderException("Environment declaration requires a TransactionFactory.");
}

XMLConfigBuilder 解析环境中dataSource节点

private DataSourceFactory dataSourceElement(XNode context) throws Exception {
    if (context != null) {
        String type = context.getStringAttribute("type");
        Properties props = context.getChildrenAsProperties();
        DataSourceFactory factory = (DataSourceFactory) resolveClass(type).getDeclaredConstructor().newInstance();
        factory.setProperties(props);
        return factory;
    }
    throw new BuilderException("Environment declaration requires a DataSourceFactory.");
}

XMLConfigBuilder 解析typeHandlers节点

private void typeHandlerElement(XNode parent) {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeHandlerPackage = child.getStringAttribute("name");
                typeHandlerRegistry.register(typeHandlerPackage);
            } else {
                String javaTypeName = child.getStringAttribute("javaType");
                String jdbcTypeName = child.getStringAttribute("jdbcType");
                String handlerTypeName = child.getStringAttribute("handler");
                Class javaTypeClass = resolveClass(javaTypeName);
                JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
                Class typeHandlerClass = resolveClass(handlerTypeName);
                if (javaTypeClass != null) {
                    if (jdbcType == null) {
                        typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
                    } else {
                        typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
                    }
                } else {
                    typeHandlerRegistry.register(typeHandlerClass);
                }
            }
        }
    }
}

XMLConfigBuilder 解析mappers节点

private void mapperElement(XNode parent) throws Exception {
    // 头节点不能为空
    if (parent != null) {
        // 遍历子节点
        for (XNode child : parent.getChildren()) {
            // 子节点是否是package
            if ("package".equals(child.getName())) {
                String mapperPackage = child.getStringAttribute("name");
                configuration.addMappers(mapperPackage);
            } 
            // 子节点是mapper
            else {
                // 获取属性
                String resource = child.getStringAttribute("resource");
                String url = child.getStringAttribute("url");
                String mapperClass = child.getStringAttribute("class");
                // 配置resource
                if (resource != null && url == null && mapperClass == null) {
                    ErrorContext.instance().resource(resource);
                    InputStream inputStream = Resources.getResourceAsStream(resource);
                    XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
                    mapperParser.parse();
                } 
                // 配置url
                else if (resource == null && url != null && mapperClass == null) {
                    ErrorContext.instance().resource(url);
                    InputStream inputStream = Resources.getUrlAsStream(url);
                    XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
                    mapperParser.parse();
                } 
                // 配置mapperClass
                else if (resource == null && url == null && mapperClass != null) {
                    Class mapperInterface = Resources.classForName(mapperClass);
                    configuration.addMapper(mapperInterface);
                } 
                // 否则抛异常
                else {
                    throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
                }
            }
        }
    }
}

XMLConfigBuilder 判断环境

private boolean isSpecifiedEnvironment(String id) {
    if (environment == null) {
        throw new BuilderException("No environment specified.");
    } else if (id == null) {
        throw new BuilderException("Environment requires an id attribute.");
    } else if (environment.equals(id)) {
        return true;
    }
    return false;
}

你可能感兴趣的:(框架 - Mybatis 源码一步步深入(三))