MyBatis properties

The properties attribute can configure some operating parameters for the system, and can be placed in XML files or properties files instead of  Java  codes. The advantage of this is that it is convenient to modify parameters without causing recompilation of the code. Generally speaking, MyBatis provides 3 ways for us to use properties, they are:

  • property child element.
  • properties file.
  • program code transfer.

property child element

Based on the following code, use the property sub-element to rewrite the relevant configuration of the database connection, as shown below.

<?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="driver" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="1128" />
    </properties>
    <typeAliases>
        <typeAlias alias="role" type="com.mybatis.po.Role"/>
    </typeAliases>
    <!--Database environment-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url"    value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8" />
                <property name="username" value="root" />
                <property name="password" value="1128" />
            </dataSource>
        </environment>
    </environments>
    <!-- Mapping file -->
    <mappers>
        <mapper resource="com/mybatis/mapper/RoleMapper.xml" />
    </mappers>
</configuration>

Here, the sub-element <property> definition under the element <properties> is used, and the database user name is defined with the string database.username, and then this defined property parameter can be introduced into the database definition, such as ${database.username} , so that it can be referenced everywhere after being defined once. But if there are hundreds or thousands of attribute parameters, it is obviously not a good choice to use this method, and you can use the properties file at this time.

Using the properties file

Using a properties file is a relatively common method. On the one hand, this file is very simple, and its logic is key-value correspondence. We can configure multiple key values ​​in one properties file, or put multiple key values ​​in multiple properties files. In , these are allowed, which is convenient for future maintenance and modification.

We create a file jdbc.properties and put it in the path of classpath, as shown below.

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/mybatis
database.username=root
database.password=1128

In MyBatis, the properties file is introduced through the attribute resource of <properties>.

<properties resource="jdbc.properties"/>

You can also import the property parameters of the properties file into the MyBatis configuration file by the method of ${database.username}. At this time, we can maintain our configuration content by maintaining the properties file.

Pass parameters using program passing

In a real production environment, database user passwords are kept secret from developers and others. In order to keep confidential, the operation and maintenance personnel generally need to encrypt the user and password into ciphertext and configure it in the properties file.

For developers and other personnel, they do not know their real user passwords, and it is impossible to use encrypted strings to connect to the database. At this time, it is often necessary to decrypt to get the real user and password.

Now assuming that the system has provided such a CodeUtils.decode(str) for decryption, then we need to decrypt the user name and password before creating the SqlSessionFactory, and then reset the decrypted string to the properties attribute, as follows Show.

String resource = "mybatis-config.xml";
InputStream inputStream;
Inputstream in = Resources.getResourceAsStream("jdbc.properties");
Properties props = new Properties();
props.load(in);
String username = props.getProperty("database.username");
String password = props.getProperty("database.password");
// Decrypt user and password, and reset in properties
props.put("database.username", CodeUtils.decode(username));
props.put ("database.password", CodeUtils.decode(password)); 
inputstream = Resources.getResourceAsStream(resource);
//Overwrite the original properties attribute parameters by means of program delivery
SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputstream, props);

First use the Resources object to read a jdbc.properties configuration file, then get its original configured user and password, decrypt and reset, and finally use the build method of SqlSessionFactoryBuilder to pass multiple properties parameters to complete.

This will overwrite the previously configured ciphertext, so that the database can be connected, and it also meets the requirements of the operation and maintenance personnel for database user and password security.

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/132268596