MyBatis configuration file (mybatis-config.xml)

The structure of the MyBatis configuration file is as follows

<?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 /><!-- 属性 -->
    <settings /><!-- 设置 -->
    <typeAliases /><!-- 类型命名 -->
    <typeHandlers /><!-- 类型处理器 -->
    <objectFactory /><!-- 对象工厂 -->
    <plugins /><!-- 插件 -->
    <environments><!-- 配置环境 -->
        <environment><!-- 环境变量 -->
            <transactionManager /><!-- 事务管理器 -->
            <dataSource /><!-- 数据源 -->
        </environment>
    </environments>
    <databaseIdProvider /><!-- 数据库厂商标识 -->
    <mappers /><!-- 映射器 -->
</configuration>

The element nodes in the mybatis-config.xml file are in a certain order, and the node positions must be sorted according to the above positions, otherwise a compilation error will occur.

The configuration element is the root node of the entire XML configuration file, and its role is equivalent to the manager of MyBatis. All MyBatis configuration information will be stored in it. 

properties tag

The properties tag can specify an external properties file (database.properties) through the resource attribute, or it can be configured through the properties sub-element.

1. Specify file

Use properties to specify external files, the code is as follows.

<properties resource="mybatisDemo/resources/database.properties"/>

database.properties is used to describe the configuration related to database connection, such as database driver, url to connect to the database, database user name, database password, etc.

2. properties sub-element configuration

Configure the username and password variables through the properties sub-element property, and then reference these variables in the environments node:

<properties>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</properties>

Reference the username and password variables in the environments node.

<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>

You can also write the attribute value directly in value without using the properties tag.

settings tag

The settings tag is used to configure the runtime behavior of MyBatis. It can profoundly affect the underlying operation of MyBatis. Generally, it does not require a lot of configuration. In most cases, its default value can be used.

There are many configuration items in settings, but not many are actually used. We can just study the commonly used configurations clearly. Descriptions of common configuration items in settings are shown in the following table

settings configuration item description
Configuration items effect Configuration options default value
cacheEnabled This configuration affects the global switch of the configuration cache in all mappers true|false true
lazyLoadingEnabled Global switch for lazy loading. When enabled, all associated objects are loaded lazily. In a specific association, the switch state of the item can be overridden by setting the fetchType attribute.  true|false false
aggressiveLazyLoading When enabled, calls to any lazy property will cause objects with lazy properties to be fully loaded; otherwise, each property will be loaded on demand. true|false Default value is true before version 3.4.1 (not included)
, false after
autoMappingBehavior Specifies how MyBatis should automatically map columns to fields or properties.
NONE means cancel automatic mapping.
PARTIAL means that it will only be automatically mapped, and nested result sets and mapped result sets are not defined.
FULL will automatically map arbitrarily complex result sets (whether nested or not)
NONE、PARTIAL、FULL PARTIAL
defaultExecutorType Configure the default executor. SIMPLE is a normal executor; REUSE will reuse prepared statements; BATCH executor will reuse statements and perform batch updates  SIMPLE、REUSE、BATCH SIMPLE
mapUnderscoreToCamelCase Whether to enable automatic camel case naming rule mapping, that is, a similar mapping from the classic database column name A_COLUMN to the classic Java attribute name aColumn true|false false

Configuration example:

<settings>
    <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"/>
</settings>

typeAliases tag

In order to not specify the fully qualified name of the class everywhere, we can define an alias using the typeAliases tag

typeHandlers tag

typeHandlers mainly convert the obtained values ​​into Java types reasonably. In typeHandler, it is divided into jdbcType and javaType, where jdbcType is used to define database types, and javaType is used to define Java types. The role of typeHandler is to undertake the mutual conversion between jdbcType and javaType.

MyBatis supports custom processing types. When customizing the processing type, you need to implement the org.apache.ibatis.type.TypeHandler interface or inherit the org.apache.ibatis.type.BaseTypeHandle class. For details, please refer to the official website: mybatis – MyBatis 3 | Configuration

environments tag

In the environments tab, you can configure multiple operating environments of MyBatis and map SQL to multiple different databases.

environment is a sub-tag of environments, which is used to configure a set of operating environment for MyBatis. It is necessary to specify the operating environment ID, transaction management, data source configuration and other related information.

We can connect to multiple databases by configuring multiple environment tags. It should be noted that one of them must be designated as the default operating environment (specified through default).

The environment tag provides two sub-tags, transactionManager and dataSource.

transactionManager tag

MyBatis supports two transaction managers, JDBC and MANAGED.

If a JDBC type transaction manager is used, the application server is responsible for transaction management operations such as commit, rollback, and so on. If you use a MANAGED type of transaction manager, the application server is responsible for managing the connection life cycle.

dataSource tag

Used to configure the connection properties of the database, such as the driver name, URL, username and password of the database to be connected.

The type attribute in dataSource is used to specify the data source type. There are the following three types.

1)UNPOOLED

UNPOOLED has no database connection pool and is inefficient. MyBatis requires opening and closing connections for every database operation, it is a bit slow and is usually used in simple applications.

2) PARTIES

For the POOLED data source type, MyBatis will maintain a database connection pool. And for each database operation, MyBatis uses connections from the connection pool and returns them to the pool after the operation is completed. Reduced initial connection and authentication time required to create new connections.

3)JNDI

For JNDI data source types, MyBatis will obtain the connection from the JNDI data source.

The sample code for the dataSource tag is as follows:

<dataSource type="POOLED">
    <!-- MySQL数据库驱动 -->
    <property name="driver" value="com.mysql.jdbc.Driver" />
    <!-- 连接数据库的URL -->
    <property name="url"
        value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</dataSource>

mapperstag

The mappers tag is used to specify the path of the MyBatis SQL mapping file.
mapper is a sub-tag of mappers. The resource attribute in mapper is used to specify the path of the SQL mapping file (class resource path). For example, the
name of the SQL mapping file is Student.xml, which is located in the package named net.cky.mapper. It can be configured like this: 

<mappers>
    <mapper resource="net/cky/mapper/Student.xml"/>
</mappers>

Guess you like

Origin blog.csdn.net/qq_43079001/article/details/132845309