Mybatis源码解析-读取properties配置文件

408 阅读1分钟

SqlSessionFactoryBuilder解析配置文件过程

/**
 * 解析properties标签
 *
 * @param context
 *
 * @throws Exception
 */
private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
        // 1
        Properties defaults = context.getChildrenAsProperties();
        // 2
        String resource = context.getStringAttribute("resource");
        // 3
        String url = context.getStringAttribute("url");
        if (resource != null && url != null) {
            // 4
            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) {
            // 5
            defaults.putAll(Resources.getResourceAsProperties(resource));
        } else if (url != null) {
            // 6
            defaults.putAll(Resources.getUrlAsProperties(url));
        }
        // 7
        Properties vars = configuration.getVariables();
        if (vars != null) {
            // 如果variables不为空,则填充到Properties对象中
            defaults.putAll(vars);
        }
        // 8
        parser.setVariables(defaults);
        // 9
        configuration.setVariables(defaults);
        // 从这里可以看出,优先级从低到高是: 子标签 -> resource/url -> configuration.variables【这里是new XMLConfigBuilder的时候可以传入的】 ,且最终结果是把Properties塞到了XPathParser以及Configuration中
    }
}

1.先解析properties标签是否有property子标签,读取里面的name和value属性并放入到Properties对象中 2.properties标签下的resource属性
3.properties标签下的url属性
4.resource和url不可以同时配置不然不知道哪个优先
5.resource,则解析资源路径的配置到Properties中(此时是先解析的xml中的property子标签 所以会存在properties文件覆盖掉xml中的配置。properties中的配置优先级高)
6.url,则去网络请求,然后读取配置到Properties中
7.首先获取configuration中现有的variables(也是Properties类型) (优先级SqlSessionFactoryBuilder.build(properties)>xml引入的properties>property子标签定义的属性)
8.然后把Properties配置信息塞入到XPathParser中
9.再把Properties配置信息塞入到Configuration的variables中

1.

image.png

2.

image.png

配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "./mybatis-3-config.dtd">

<configuration>
    <!--引入外部资源  -->
    <properties resource="org/apache/ibatis/mytest/db.properties">
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </properties>
    <typeAliases>
                <typeAlias  alias="student" type="org.apache.ibatis.mytest.StudentDto"/>
    </typeAliases>
    <!--设置默认的环境为开发环境  -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
        <environment id="development2">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url2}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- <package name="com.zzz.mybatis.mapper"/> -->
        <mapper resource="org/apache/ibatis/mytest/StudentMapper.xml"/>
    </mappers>
</configuration>