Introduction
The last blog summarized the core components of the mybatis framework. This blog summarized the elements of the mybatis core configuration file and the role of the configuration elements.
Summary
Mybatis implements ORM mapping through core configuration files, commonly named mybatis-config.xml. There are many elements in this configuration file. Let's start with a simple understanding of these elements through a hierarchical structure of the mybatis core configuration file.
<?xml version="1.0" encoding="UTF-8"?> < configuration > <! - Configuration - > Configuration < properties /> <! - attributes - > attributes Settings/> <! - Settings - > Settings <typeAliases/> <!--Naming of types--> <typeHandlers/> <!--Type Processor--> <objectFactory/><!--Object Factory--> <plugins/> <!--Plug-in--> < environments > <! - Configuration environment - > Configuration environment < environment > <! - environment variable - > environment variable <transactionManager/><!--Transaction Manager--> <dataSource/><!--Data Source--> </environment> </environments> <databaseIdProvider/> <!--Database vendor identification--> < mappers /> <! - mappers - > mappers </configuration>
content
Understanding the elements in the catalog structure in the overview, we need to analyze and understand the usage of each element one by one.
A property
Role: Configuring Properties
2. Configuration mode:
(1) property child element, configuration code example as follows
<properties>
<properties name="driver" value="com.mysql.jdbc.Driver"/>
<properties name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<properties name="username" value="root"/>
<properties name="password" value="****"/>
</properties>
How do configuration parameters be used in configuration files? <dataSource type="POOLED">
<properties name="driver" value="${driver}"/>
<properties name="url" value="{url}"/>
<properties name="username" value="${username}"/>
<properties name="password" value="${password}"/>
</dataSource>
(2) properties configuration file: It is easy to reuse in multiple configuration files and to modify and maintain.
properties file code example
#Database Profile
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=****
How to use it? Put the file under the source package and introduce it: <properties resource="jdbc.properties"/>.3. Program parameter transfer: Sometimes for data security, configure ciphertext in jdbc.properties and decrypt the program plaintext to ensure the security of the system.
Priorities of four configurations: the third is the highest priority of the attributes passed through the program method parameters, followed by the configuration files specified in the resource/url attributes, and the lowest priority is the attributes specified in the properties element. _
2 settings
settings is the most complex configuration of mybatis and the most important configuration. There are many configurations. I don't summarize them here, so how to configure them? The complete configuration example is as follows.
<settings>
<settings name="cacheEnable" value="true"/>
<settings name="lazyLoadingEnabled" value="true"/>
<settings name="multipleResultSetsEnabled" value="true"/>
<settings name="useColumnLabel" value="true"/>
<settings name="useGeneratedKeys" value="false"/>
<settings name="autoMappingBehavior" value="PARTIAL"/>
<settings name="defaultExecutorType" value="SIMPLE"/>
<settings name="defaultStatementTimeout" value="25"/>
<settings name="safeRowBoundsEnabled" value="false"/>
<settings name="mapUnderscoreToCamelCase" value="false"/>
<settings name="localCacheScope" value="SESSION"/>
<settings name="jdbcTypeForNull" value="OTHER"/>
<settings name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>
Three types Aliases
Role: Aliases can be used in the context of mybatis instead of full-qualified names for overly long classes.
Classification 2 (1) System Custom Aliases: The following table
(2) Custom aliases: The sample code is as follows, using role instead of the full path of its class to reduce configuration complexity.
<!--Define aliases--!>
<typeAliases>
<typeAlias alias="role" type="com.liming.domain.po.Role"/>
</typeAlias>
Four types handler
Function: Setting parameters in a Prepared Statement or fetching values from a ResultSet requires a registered typeHandler for type conversion.
Classification 2: (1) System Definition
TypeHandler registered by the system
(2) Code Example \\\\\\\\\\
public TypeHandlerRegistry(){
register(Boolean.class, new BooleanTypeHandler());
register(boolean.class, new BooleanTypeHandler());
register(JdbcType.BOOLEAN, new BooleanTypeHandler());
register(JdbcType.BIT, new BooleanTypeHandler());
......
register(Character.class, new CharacterTypeHandler());
register(char.class, new CharacterTypeHandler());
}
Common configurations: java type, JDBC type. TypeHandler converts parameters from javaType to jdbcType, or jdbcType to javaType when the results are fetched from the database.
(2) Customization
Register a custom typeHandler and override the typeHandler for string parameters.
<typeHandlers>
<typeHandler jdbc="VARCHAR" javaType="string"
handler="com.liming.domain.typeHandler.myStringTypeHandler"/>
</typeHandlers>
2) Register typeHandle by scanning
<typeHandlers>
<package name="com.liming.domain.typeHandler"/>
</typeHandlers>
Enumeration: mybatis provides two typeHandle s for transforming enumerated types: org.apache.ibatis.type.EnumTypeHandler and org.apache.ibatis.type.EnumOrdinalTypeHandler.
Five Object Factory
Function 1: When mybatis build result returns, it uses ObjectFactory to build POJO.
Classification 2: (1) By default, the default ObjectFactory in mybatis is provided by org.apache.ibatis.reflection.factory.DefaultObjectFactory.
(2) Customize and define a new ObjectFactory code example as follows. When implementing the code, our new ObjectFactory class directly inherits DefaultObjectFactory to implement the interface of ObjectFactory.
<objectFactory type="com.liming.domain.objectFactory.MyObjectFactory">
<property name="name" value="MyObjectFactory"/>
</objectFactory>
Six plugins
Seven environments
Function 1: Mainly used to configure connection pool data sources.
2 Most of the data sources are:
(1) Configuration of database source, configuration of data source connection information through dataSource tag, definition of various parameters of database by subtag property, type attribute represents configuration of database connection mode, providing the following four kinds:
UNPOOLED, Unpooled Data Source;
2) POOLED, Connect Pooled Data Source;
3) JNDI, JNDI Data Source;
4) Custom data source must implement the interface of org.apache.ibatis.datasource.DataSourceFactory
(2) Configuration of database transactions through transactionManager tags and subtags property to configure various attributes of data sources. type attributes include the following three configurations
JDBC, which manages transactions in JDBC mode, is commonly used in independent coding.
MANAGED, which manages transactions in a container manner, is commonly used in JNDI data sources.
(3) Customization, which allows developers to customize database transaction management methods for special applications;
(3) A code example of the complete configuration of a data source
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="autoCommit" value="false">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="****"/>
</dataSource>
</environment>
</environments>
Note: The default attribute in the environment indicates which data source configuration we start by default.Eight database IdProvider: Its function is to specify the SQL to run in the database provided by the corresponding database vendors. Generally, it has less application and less use of databases from different vendors. mybatis also provides system default rules and customized rules for database vendor identification, which can be learned by oneself if one is interested.
Nine mappers
Overview 1: Mapper is the most complex and core component of mybatis
2. Method of introducing mappers:
(1) Introducing the Mapper with File Path
(2) Introduce the Mapper with the Package Name<mappers> <mapper resource="Corresponding mapper Configuration file for full path of interface"> </mappers>
(3) Introducing Mapper with Class Registration<mappers> <mapper resource="Corresponding mapper Full Subgrade of Interface Pack"> </mappers>
(4) Introducing mappers with xml configuration files<mappers> <mapper resource="Corresponding mapper Path of interface class"> </mappers>
3. Introduce mapper steps<mappers> <mapper url="file:/// var/mappers + file path ">" </mappers>
Define the mapper interface
package com.liming.domain.mapper; import java.util.List; import com.liming.domain.po.User; public interface UserMapper{ public User getUser(Int id); }
(2) Configuration files corresponding to mapper interfaces: defining mapper mapping rules and SQL statements
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.liming.domain.mapper.UserMapper"> <select id="getUser" parameterType="int" resultType="com.liming.domain.po.User"> select id, role_name as roleName, note from t_role where id=#{id} </select> </mapper>
(3) Introducing a mapper into the core configuration file SqlMapConfig.xml
<mappers>
<mapper resource="com/liming/domain/mapper/userMapper.xml"/>
</mappers>
summary
Mybatis configuration file is a must for learning mybatis. The core is the compiling process and executing principle of configuration file. The premise is that we need to understand the composition of configuration file, as well as the elements and content of configuration file. We can accumulate more knowledge and constantly enrich our experience.