MyBatis(3.2.3) - Configuring MyBatis using XML, Properties

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis(3.2.3) - Configuring MyBatis using XML, Properties相关的知识,希望对你有一定的参考价值。

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.

 

以上是关于MyBatis(3.2.3) - Configuring MyBatis using XML, Properties的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis(3.2.3) - Configuring MyBatis using XML, Properties

MyBatis(3.2.3) - hello world

MyBatis(3.2.3) - Integration with Spring

探囊取物,取出MyBatis内部事务

MyBatis(3.2.3) - Paginated ResultSets using RowBounds

MyBatis(3.2.3) - Multiple results as a map