The properties configuration element can be used to externalize the configuration values into a properties file and use the properties' key names as placeholders. In the preceding configuration, we have externalized the database connection properties into the application.properties file and used placeholders for the driver, URL, and so on.
1. Configure the database connection parameters in application.properties as follows:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatisdemo jdbc.username=root jdbc.password=admin
2. In mybatis-config.xml, use the placeholders for the properties defined in application.properties.
<properties resource="application.properties"> <property name="jdbc.username" value="db_user"/> <property name="jdbc.password" value="verysecurepwd"/> </properties> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource>
Also, you can configure the default parameter values inside the <properties> element; they will be overridden by the values in the properties file if there are properties with the same key name.
<properties resource="application.properties"> <property name="jdbc.username" value="db_user"/> <property name="jdbc.password" value="verysecurepwd"/> </properties>
Here, the username and password values db_user and verysecurepwd will be overridden by the values in application.properties if the application. peroperties file contains the key names jdbc.username and jdbc.password.