Mybatis configuration file

Mybatis configuration file

configure profile header:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-// Config 3.0//EN"

Mapping mapping file header:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-// Mapper 3.0//EN"

Elements contained

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-// Config 3.0//EN"
<configuration> <!-- to configure -->
    <properties/> <!-- attribute -->
    <settings/> <!-- set up -->
    <typeAliases/> <!-- Type naming -->
    <typeHandlers/> <!-- Type processor -->
    <objectFactory/> <!-- Object factory -->
    <plugins/> <!-- plug-in unit -->
    <environments> <!-- Configure the environment -->
        <environment> <!-- environment variable -->
            <transactionManager/> <!-- Transaction manager -->
            <dataSource/> <!-- data source -->
    <databaseIdProvider/> <!-- Database manufacturer identification -->
    <mappers/> <!-- Mapper -->

However, it should be noted that the order of MyBatis configuration items cannot be reversed. If their order is reversed, an exception will occur during MyBatis startup, resulting in the program not running.

1.typeAliases function
Label introduction:
In the SQL mapping configuration file of MyBatis, parameters such as parameterType and resultType are often used to set the input / output parameters of SQL statements. Generally, the parameters are data of Java type, with basic data type or encapsulation type, but generally, the full path name of the type should be declared, such as "java.lang.String", "java.util.HashMap" or "com.pjb.mybatis.po.User", as follows:

<select id="findUserByUsername" parameterType="java.lang.String" resultType="">
    SELECT * FROM USER WHERE username LIKE '%${value}%'

Then, is it possible to declare the full path name of the class somewhere like in a Java class, and then use only the alias of the type when using it? By setting the typeAliases attribute in the global configuration file of MyBatis, you can set the type alias for the input / output parameters in the SQL mapping file, and then use the alias when specifying the input / output parameter type in the SQL mapping configuration file. The configuration is as follows:

<!--Alias settings-->
    <typeAlias alias="user" type=""/>
    <typeAlias alias="str" type="java.lang.String"/>

At this time, alias can be used to specify the type of input / output parameters in the SQL mapping configuration file:

<select id="findUserByUsername" parameterType="str" resultType="user">
    SELECT * FROM USER WHERE username LIKE '%${value}%'

Of course, JavaBean type encapsulated classes are generally placed under a package (such as the com.pjb.mybatis.po package in this example). It is cumbersome to configure aliases one by one. Therefore, MyBatis provides a method to define aliases in batch. Specify the package name, and the program will add aliases to all wrapper classes under the package. The rule for c program to define alias is that the first letter of the class name of the corresponding wrapper class becomes lowercase. The configuration is as follows:

<!--Alias settings-->
    <package name=""/>

Alias can also be realized by annotation. The implementation method is to add "@alias" annotation in the declaration header of the class that needs to specify an alias, and the parameter is the alias corresponding to the class. The code is as follows:

public class User{
    //Other codes

settings tab
In MyBatis, settings is the most complex configuration, which can deeply affect the operation of the bottom layer of MyBatis, but in most cases, it can be run by using the default value, so in most cases, it does not need to be configured in large quantities, just need to modify some common rules, such as automatic mapping, hump naming mapping, cascading rules, whether to start the cache, Executor type, etc. Settings configuration item description

There are many configuration items in settings, but not too many are really used. We can study the commonly used configuration items clearly, such as cacheEnabled for caching, lazyloading enabled and aggregatelazy loading for cascading, autoMappingBehavior and mapUnderscoreToCamelCase for automatic mapping, defaultExecutorType for actuator type, etc.

Here is a full configuration example:

    <setting name="cacheEnabled" value="true"/>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="multipleResultSetsEnabled" value="true"/>
    <setting name="useColumnLabel" value="true"/>
    <setting name="useGeneratedKeys" value="false"/>
    <setting name="autoMappingBehavior" value="PARTIAL"/>
    <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
    <setting name="defaultExecutorType" value="SIMPLE"/>
    <setting name="defaultStatementTimeout" value="25"/>
    <setting name="defaultFetchSize" value="100"/>
    <setting name="safeRowBoundsEnabled" value="false"/>
    <setting name="mapUnderscoreToCamelCase" value="false"/>
    <setting name="localCacheScope" value="SESSION"/>
    <setting name="jdbcTypeForNull" value="OTHER"/>
    <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>

Tags: Java MySQL Mybatis

Posted by wisedj on Sat, 16 Jul 2022 04:35:20 +0930