Understand the common configuration notes of Mybatis - Advanced

Keywords: Java Mybatis Back-end

to configure

3.1. Core configuration file

The MyBatis configuration file contains settings and attribute information that deeply affect MyBatis behavior. The top-level structure of the configuration document is as follows:

  • configuration

    • properties

    • settings

    • typeAliases

    • typeHandlers

    • objectFactory (object factory)

    • plugins

    • environments

      • Environment (environment variable)

        • Transaction manager

        • dataSource

    • databaseIdProvider (database vendor ID)

    • mappers

3.2. Environment configuration

Mybatis can configure multiple environments

However, remember that although multiple environments can be configured, each SQLSessionFactory can only select one to instantiate

<environments default="test">
​
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/shop?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </dataSource>
    </environment>
    <environment id="test">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </dataSource>
    </environment>
</environments>

In the above code, two sets of database configurations are configured, but the configuration needs to be used for free switching according to the ID.

The default transaction manager used by Mybatis is JDBC, and the connection pool is POOLED

3.3. Properties

We can implement the reference configuration file by using properties

These properties can be configured externally and can be switched flexibly. They can be configured in the previous configuration file or passed using properties.

1. Write a configuration file: db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/shop?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
username=root
password=123456

2. The following is to import the data in the configuration file we just created in the core configuration file

Use the properties tag. Note that the position of the -- > -- > tag should be written on the < environments > tag, which is specified by Mybatis, otherwise an error will be reported.

<?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>
    <!--        Import external profile-->
    <properties resource="db.properties"></properties>
    
    <environments default="test">
        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
​
<!--    every last Mapper.xml All documents need to be in Mybatis Register in the core configuration file!!-->
    <mappers>
        <mapper resource="com/ajie/demo/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

In this way, you can get the values in the db.properties configuration file.

3.4 type aliases

When we do not alias the entity class, we have to write a series of addresses when using the return value type in UserMapper.xml, which is very inconvenient. For example:

<!--    select Query all users-->
    <select id="getUserList" resultType="com.ajie.demo.dao.User">
        select * from shop.mtsuser;
    </select>

In the core configuration file, you can use typeAliases to alias entity classes:

Mode 1:

<!--    Alias an entity class-->
    <typeAliases>
        <typeAlias type="com.ajie.demo.pojo.User" alias="User"/>
    </typeAliases>

Mode 2:

<!--    Alias mode 2-->
<typeAliases>
    <package name="com.ajie.demo.pojo"/>
</typeAliases>

Difference: Method 1 can customize the name of entity class, but it is not recommended when there are a large number of entity classes;

Method 2 defines that the name of the entity class is the lowercase of the name of the entity class. For example, the alias above is user. This method is applicable when there are a large number of entity classes.

Posted by Rohan Hill on Fri, 12 Nov 2021 18:18:23 -0800