[MyBatis] several commonly used configurations to optimize our core configuration files (simple, easy to understand)

Keywords: Mybatis xml JDBC MySQL

After setting up the MyBatis environment, we can make some common optimizations or other settings for MyBatis

catalog

Before that, our core configuration file was written like this

After that, our core configuration file is written like this

Reference external profile

Log factory

Hump naming conversion

Entity class alias

Mapper

Before that, our core configuration file was written like this

<?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">
<!--Core profile-->
<configuration>

    <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?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/robot/dao/UserMapper.xml"/>
    </mappers>
</configuration>

After that, our core configuration file is written like this

db.properties 

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

 mybatis-config.xml

<?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 can also be added, but external configuration takes precedence-->
    <properties resource="db.properties"/>

<!--    Log factory-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

<!--    Alias entity class-->
    <typeAliases>
<!--        <typeAlias type="com.robot.pojo.User" alias="User"/>-->
        <package name="com.robot.pojo"/>
    </typeAliases>
    
    <environments default="development">
        <environment id="development">
            <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>
    <mappers>
        <mapper class="com.robot.dao.UserMapper"/>
    </mappers>
</configuration>

Reference external profile

In < Properties > we refer to the external configuration file db.properties , so it can be configured as above in < environment >

<properties resource="db.properties"/>

Log factory

logImpl is used in < setting >. It is a log factory, and the following value is STDOUT_LOGGING for standard log output

<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

Of course, there are other log outputs. In the future, I will introduce how to use LOG4J separately

Hump naming conversion

mapUnderscoreToCamelCase used in < setting > means to enable hump naming conversion. For example, the field name in our database is use_name, it will be converted to userName in Java after the conversion is enabled

<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

Entity class alias

The first way is to directly name the entity class under this path as User

<typeAliases>
    <typeAlias type="com.robot.pojo.User" alias="User"/>
</typeAliases>

The second way is to scan the package. The alias of entity class under the package is lowercase. For example, if an entity class is user, its alias is user

<typeAliases>
    <package name="com.robot.pojo"/>
</typeAliases>

But in the second package scanning mode, if the entity class uses annotation, its alias is the value in the annotation

Mapper

Register to bind our Mapper file

The first way: each Mapper.xml All need to be registered

<mappers>
    <mapper resource="com/robot/dao/UserMapper.xml"/>
</mappers>

The second way: use class file binding to register each Mapper.xml All need to be registered

<mappers>
    <mapper class="com.robot.dao.UserMapper"/>
</mappers>

Third way: use package scanning

<mappers>
    <package name="com.robot.dao"/>
</mappers>

Note: if the latter two methods are used, the interface and its Mapper configuration file must have the same name and be under the same package

summary

The above are some common configuration optimizations that can be done after MyBatis is built. Of course, the configuration is far more than these. The above are just some common ones

Note: the tags in the core configuration file are in order, for example, < Properties > is the first one, and < setting > can only be behind it. If it is written in front of it, an error will be reported. The specific order is as follows

Posted by veenasv on Sat, 13 Jun 2020 19:20:02 -0700