Detailed explanation of spring mybatis configuration items

Posted by j7n5u on Mon, 24 Jan 2022 08:58:22 +0100

pom

<!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>

web-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
    <!--Database configuration,Path pointing database.properties-->
    <context:property-placeholder location="classpath:properties/jdbc.properties"/>
    <!--Configure the data source, which is used here druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <!-- Initialize connection size -->
        <property name="initialSize" value="10" />
        <!-- Maximum number of connections used in connection pool -->
        <property name="maxActive" value="20" />
        <!-- Connection pool minimum idle -->
        <property name="minIdle" value="0" />
        <!-- Gets the maximum connection wait time -->
        <property name="maxWait" value="60000" />
        <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

        <property name="validationQuery" value="${jdbc.validationQuery}" />
<!--        <property name="testOnBorrow" value="false" />-->
<!--        <property name="testOnReturn" value="false" />-->
<!--        <property name="testWhileIdle" value="true" />-->

        <!-- Configure how often to detect idle connections that need to be closed. The unit is milliseconds -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- Configure the minimum lifetime of a connection in the pool, in milliseconds -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        <!-- open removeAbandoned function -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800 Seconds, that is, 30 minutes -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- close abanded Output error log when connecting -->
<!--        <property name="logAbandoned" value="false" />-->

        <!-- Monitoring database -->
        <!-- <property name="filters" value="stat" /> -->
<!--        <property name="filters" value="mergeStat" />-->
    </bean>
    <!--SqlSessionFactory to configure-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--The data source points to the previously configured dataSource-->
        <property name="dataSource" ref="dataSource"/>
        <!--binding Mybatis Configuration file for-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--        appoint mapper.xml position-->
        <property name="mapperLocations" value="classpath*:maps/**/*.xml"/>
    </bean>
    <!--to configure Mapper mapping-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--        injection sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--        Auto scan package bean-->
        <property name="basePackage" value="com.qx.springmvcdemo.mapper"/>
    </bean>
</beans>

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>
    <settings>
        <!--        Field underline to hump-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

mybatis configuration item details

 

Set parametersdescribeEffective valueDefault value
cacheEnabled This configuration affects the global switch of the cache configured in all mappers. true | false true
lazyLoadingEnabled Global switch for delayed loading. When on, all associated objects are loaded late. In a specific association, the switch state of the item can be overridden by setting the fetchType property. true | false false
aggressiveLazyLoading When enabled, calling any delay attribute will make the object with delay load attribute load completely; Conversely, each property will be loaded on demand. true | false true
multipleResultSetsEnabled Whether to allow a single statement to return multiple result sets (compatible driver is required). true | false true
useColumnLabel Use column labels instead of column names. Different drivers will have different performances in this regard. For details, please refer to the relevant driver documents or observe the results of the drivers used by testing these two different modes. true | false true
useGeneratedKeys JDBC is allowed to support automatic generation of primary keys. Driver compatibility is required. If it is set to true, this setting forces the automatic generation of primary keys. Although some drivers are incompatible, they can still work normally (such as Derby). true | false False
autoMappingBehavior Specify how MyBatis should automatically map columns to fields or properties. NONE means to cancel automatic mapping; PARTIAL automatically maps only result sets that do not have nested result set mappings defined. FULL automatically maps any complex result set (whether nested or not). NONE, PARTIAL, FULL PARTIAL
autoMappingUnknownColumnBehavior Specify the behavior when detects an unknown column (or unknown property type) of automatic mapping target.
  • NONE: Do nothing
  • WARNING: Output warning log (The log level of 'org.apache.ibatis.session.AutoMappingUnknownColumnBehavior' must be set to WARN)
  • FAILING: Fail mapping (Throw SqlSessionException)
NONE, WARNING, FAILING NONE
defaultExecutorType Configure the default actuator. SIMPLE is an ordinary actuator; REUSE executor will REUSE prepared statements; The BATCH executor reuses the statements and performs BATCH updates. SIMPLE REUSE BATCH SIMPLE
defaultStatementTimeout Set the timeout, which determines the number of seconds the driver waits for a response from the database. Any positive integer Not Set (null)
defaultFetchSize Sets the driver a hint as to control fetching size for return results. This parameter value can be override by a query setting. Any positive integer Not Set (null)
safeRowBoundsEnabled Allow paging (rowboundaries) in nested statements. If allow, set the false. true | false False
safeResultHandlerEnabled Allows paging (ResultHandler) in nested statements. If allow, set the false. true | false True
mapUnderscoreToCamelCase Whether to enable automatic hump naming rule (camel case) mapping, that is, from the classic database column name a_ A similar mapping from column to the classic Java property name aColumn. true | false False
localCacheScope MyBatis uses the Local Cache mechanism to prevent circular references and accelerate repeated nested queries. The default value is SESSION, in which case all queries executed in a SESSION are cached. If the value is set to state, the local SESSION is only used for STATEMENT execution, and different calls to the same SqlSession will not share data. SESSION | STATEMENT SESSION
jdbcTypeForNull Specify a JDBC type for a NULL value when no specific JDBC type is provided for the parameter. Some drivers need to specify the JDBC type of the column. In most cases, they can directly use the general type, such as NULL, VARCHAR or OTHER. JdbcType enumeration. Most common are: NULL, VARCHAR and OTHER OTHER
lazyLoadTriggerMethods Specifies which object's method triggers a deferred load. A method name list separated by commas equals,clone,hashCode,toString
defaultScriptingLanguage Specifies the default language for dynamic SQL generation. A type alias or fully qualified class name. org.apache.ibatis.scripting.xmltags.XMLDynamicLanguageDriver
callSettersOnNulls Specifies whether to call the setter (put in case of map object) method of the mapping object when the value in the result set is null Keyset () is useful when initializing dependent or null values. Note that basic types (int, boolean, etc.) cannot be set to null. true | false false
logPrefix Specifies the prefix that MyBatis adds to the log name. Any String Not set
logImpl Specify the specific implementation of the log used by MyBatis. If it is not specified, it will be found automatically. SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING Not set
proxyFactory Specifies the proxy tool used by Mybatis to create objects with deferred loading capability. CGLIB | JAVASSIST JAVASSIST (MyBatis 3.3 or above)
vfsImpl Specifies VFS implementations Fully qualified class names of custom VFS implementation separated by commas. Not set
useActualParamName Allow referencing statement parameters by their actual names declared in the method signature. To use this feature, your project must be compiled in Java 8 with -parameters option. (Since: 3.4.1) true | false true

Topics: Mybatis Spring MVC