版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lingyiwin/article/details/84112808
Mybatis学习笔记04
01 新建属性文件jdbc.properties
driver、url需要根据自己使用的数据库具体配置,本文使用mysql数据库
## 数据库属性配置文件内容
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mysql?serverTimezone=UTC
jdbc.username=****
jdbc.password=****
02 修改Mybatis文件
原配置如下
<configuration>
<!-- 配置运行环境 -->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mysql?serverTimezone=UTC" />
<property name="username" value="****" />
<property name="password" value="****" />
</dataSource>
</environment>
</environments>
</configuration>
更新后的配置
<configuration>
<!-- 引入数据库配置文件 -->
<properties resource="jdbc.properties" />
<!-- 配置运行环境 -->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
</configuration>
03 properties
properties属性都是可外部配置且可动态替换 例如:
当需要更新配置时,既可以修改配置文件,也可以动态替换属性
<properties resource="jdbc.properties"> <!-- 外部配置-->
<property name="username" value="dev_user"/> <!-- 动态替换-->
<property name="password" value="F2Fa3!33TYyg"/> <!-- 动态替换-->
</properties>
04 properties 的属性 resource 和 url
相同处:resource / url 都是用来引入配置文件路径的
不同:
resource 标签用来引入项目的相对路径
url标签用来引入网络路径或者自盘路径下的资源
05 properties 属性 加载顺序
- 在 properties 元素体内指定的属性首先被读取。
- 然后根据 properties 元素中的 resource 属性读取类路径下属性文件或根据 url 属性指定的路径读取属性文件,并覆盖已读取的同名属性。
- 最后读取作为方法参数传递的属性,并覆盖已读取的同名属性。
properties 文件加载的源码. 可以看出文件加载顺序
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
/**
* 解析properties 属性中指定的属性。
*/
Properties defaults = context.getChildrenAsProperties();
String resource = context.getStringAttribute("resource"); //resource 制定的属性路径
String url = context.getStringAttribute("url"); //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.");
}
/**
* 根据 properties 元素中的 resource 属性读取类路径下属性文件,并覆盖properties 属性中指定的同名属性。
*/
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {
/**
* 根据properties元素中的url属性指定的路径读取属性文件,并覆盖properties 属性中指定的同名属性。
*/
defaults.putAll(Resources.getUrlAsProperties(url));
}
/**
* 获取方法参数传递的properties
* 创建XMLConfigBuilder实例时,this.configuration.setVariables(props);
*/
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
05 properties 占位符
从MyBatis 3.4.2开始,你可以为占位符指定一个默认值。
<dataSource type="POOLED">
<!-- ... -->
<property name="username" value="${username:ut_user}"/>
</dataSource>
这个特性默认是关闭的。如果你想为占位符指定一个默认值, 你应该添加一个指定的属性来开启这个特性。
<properties resource="org/mybatis/example/config.properties">
<!-- ... -->
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
</properties>