MyBatis provides three ways to use properties:
1, property sub-elements.
2, properties file.
3, the program code is transmitted.
properties property-based system configured for some operating parameters on the XML file or general file properties, which can better facilitate the parameter modification.
File name is: mybatis_config.xml. Look at the properties and properties property use.
<?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="jdbc.driver" value="com.mysql.jdbc.Driver"></property>
<property name="jdbc.url" value="jdbc:mysql://localhost:3306/MyBatisDemo2"></property>
<property name="jdbc.username" value="root"/>
<property name="jdbc.password" value="root"/>
</properties>
<!--别名-->
<typeAliases>
<typeAlias alias="role" type=""/>
</typeAliases>
<!--数据库环境-->
<environments default="development">
<environment id="development">
<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>
<!--映射文件-->
<mappers>
<mapper resource=""/>
</mappers>
</configuration>
<proerties> erupted element <property> definition, only defined once and can be referenced everywhere . But when too many attribute parameters, recommends the following: the Properties file way to solve.
properties File name: jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hr?serverTimezone=GMT
jdbc.username=root
jdbc.password=root
There are two ways to use external configuration
The first attribute url
<!--注意顺序哦!-->
<properties url="file:///e:/政通路/课堂笔记/S2/day01_mybatis_demo/src/main/resources/jdbcConfig.properties">
</properties>
The second resource property (common)
<properties resource="jdbcConfig.properties"></properties>
Detailed configuration used:
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>