MyBatis的properties配置

MyBatis 的 properties 配置

属性

这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递。可以以 key - value 的形式存储这些配置,使用方法如下

  1. 在构造函数中传入
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in, environment, props);
  1. 直接使用子标签
<properties>
  <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://192.168.137.100:3306/yczuoxin?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true"/>
  <property name="username" value="root"/>
  <property name="password" value="root"/>
</properties>

就可以在其他标签中,如 dataSource 标签中使用,冒号后面是如果没有设置时读取的默认值( 3.4.2 开始支持)

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username:root}"/>
            <property name="password" value="${password:root}"/>
        </dataSource>
    </environment>
</environments>
  1. 配置 resource 地址
<properties resource="jdbc.properties"></properties>

并在 resource 文件夹下创建对应的文件 jdbc.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://192.168.137.100:3306/yczuoxin?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=root

在上述 dataSource 标签中使用

  1. 配置 url 地址
<properties resource="xxx.cn"></properties>

会使用 url 去读取流作为配置

如果同时出现以上几种配置的情况会按照以下优先级,resource 和 url 不能同时配置

  • 在 properties 元素体内指定的属性首先被读取。
  • 然后根据 properties 元素中的 resource 属性读取类路径下属性文件或根据 url 属性指定的路径读取属性文件,并覆盖已读取的同名属性。
  • 最后读取作为方法参数传递的属性,并覆盖已读取的同名属性。

读取配置

读取配置的源码如下

private void propertiesElement(XNode context) throws Exception {
    // 判断配置文件又没有 properties 标签
    if (context != null) {
        Properties defaults = context.getChildrenAsProperties();
        String resource = context.getStringAttribute("resource");
        String url = context.getStringAttribute("url");
        // resource 和 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();
        // 传入的配置中有相同的配置会覆盖 resource 或者 url 配置的数据
        if (vars != null) {
            defaults.putAll(vars);
        }
        // 放入 XPathParser 和 Configuration 中
        parser.setVariables(defaults);
        configuration.setVariables(defaults);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值