Mybatis 源码解析(三) - properties标签

文章个人学习源码所得,若存在不足或者错误之处,请大家指出。

Properties配置格式如下:
Configuration.xml中:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties>
        <property name="username" value="cig-root"/>
        <property name="password" value="cig-root"/>
    </properties>

    ...
</configuration>

当然properties的信息也可以配置到.properties文件中:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=root

对于properties节点的解析没有什么好说的,主要是properties可以在不同的位置进行设置,例如可以在:
SqlSessionFactoryBuilder的SqlSessionFactory buid(Reader reader, Properties properties)传入,那么这几种properties的加载顺序是什么呢。
按照优先级由低到高来说明:
1) SqlSessionFactoryBuilder实例build时传入,这种properties的设置方式优先级是最低的,当后续配置文件中读取到相应的值时,会将SqlSessionFactory buid(Reader reader, Properties properties)时传入的properties全部冲掉,所以在使用时要注意。
2) Configuration.xml中properties标签下内容,当解析到properties标签时,会先将properties标签下子标签的值解析出来,例如上面的username=cig-root和password=cig-root。
3) .properties文件是优先级最高的,当之前所有的properties信息解析完毕后,会对.properties文件进行解析,解析时,会覆盖Configuration.xml中同名的信息,例如上面.properties文件中的username=root和password=root会覆盖掉username=cig-root和password=cig-root,所以在配置时要注意尽量不要在多个地方对其进行配置。

猜你喜欢

转载自blog.csdn.net/securitit/article/details/47301785
今日推荐