Mybatis global configuration file mybatis-config.xml

1. The usefulness of the global configuration file

Mybatis can configure data sources, transaction managers, runtime behavior, processing aliases, type processing, plug-ins and other information through configuration files. When the mybatis application is initialized, the program will parse the global configuration file, use the configured information to instantiate the Configuration component, and complete the initialization of the basic configuration. In the global configuration file of mybatis, the corresponding configuration is completed through the corresponding label. The overall label structure is as follows:

insert image description here
Note: The tags in the global configuration file must be written in the above order when writing, otherwise there will be problems; in addition, when initializing mybatis, it is not necessary to use the xml configuration file, but it can also be implemented through the java api, because no matter which method is used, it will be instantiated into the configuration class Configuration when mybatis starts and initializes.

2. The purpose of the label

What are the uses of the tags introduced above? Let’s take a look at it through a table

insert image description here

3. Detailed introduction and use of labels

properties

Information for importing external properties configuration files, two ways to use:

<properties resource="类入类路径下的资源"></properties>
<properties url="网络或者磁盘路径"></properties>

insert image description here
insert image description here

settings

It is mainly to control some behaviors of mybatis when it is running. Here are some commonly used attributes. For those not involved, you can go to mybatis official website to view mybatis – MyBatis 3 | Configuration

<settings>
	<!--    全局性开启和关闭任何缓存    -->
	<setting name="cacheEnabled" value="true"/>
	<!-- 延迟加载的全局开关 -->
	<setting name="lazyLoadingEnabled" value="true"/>
	<!-- 允许单条语句返回多个结果集 -->
	<setting name="multipleResultSetsEnabled" value="true"/>
	<!-- 允许使用数据字段列的别名代替数据列名 -->
	<setting name="useColumnLabel" value="true"/>
	<!-- 设置为true时,允许jdbc自动生成主键 -->
	<setting name="useGeneratedKeys" value="false"/>
	<!-- 设置超时时间,它决定数据库驱动等待数据库响应的秒数 -->
	<setting name="defaultStatementTimeout" value="25"/>
	<!-- 是否开启驼峰命名自动映射 -->
	<setting name="mapUnderscoreToCamelCase" value="false"/>
</settings>

typeAliases

Alias ​​processor, create an alias for some java classes whose names (paths) are too long, and use the alias in the future

The specific use is as follows:

<!-- 用来为java类型设置一个简称,主要用于降低冗余的全限定类型名书写 -->
<typeAliases>
	<!-- 直接指定一个类型,为其取别名 -->
	<typeAlias alias="User" type="com.app.test.mapper.User" />
 
	<!-- 给包起别名,其包下对应的所有类型的别名在没有使用@Alias注解的情况下会使用 Bean 的首字母小写的非限定类名来作为它的别名 -->
	<package name="com.app.test.mapper"/>
	<!-- 例如此处,设置后,com.app.test.mapper下所有的Bean的别名为 Bean的首字母小写的非限定类名 -->
 
	<!-- 在为包起别名基础上,也可以通过注解方式设置别名, @Alias('别名') -->
	
</typeAliases>

insert image description here
There are some built-in aliases in mybatis, as follows:
insert image description here

typeHandlers

typeHandlers is a type processor. jdk 1.8 has implemented all JSR310 specifications, and various date and time processors written by mybatis based on JSR310 can be used. Versions before mybatis 3.4 require us to manually register these processors, and later versions are automatically registered. There is nothing to say about this. I feel that the current label is used to configure custom processors.

Developers can customize the type handler by implementing the org.apache.ibatis.type.TypeHandler interface, or inheriting a very convenient class org.apache.ibatis.type.BaseTypeHandler. Since I haven't defined the type processor myself during the development process, I won't explain it in detail here. If you need to customize it, you can check the mybatis official website.

plugins

MyBatis allows interception of calls at a certain point during the execution of a mapped statement. I won't explain it in detail here, but it will be introduced in the implementation and use of plug-ins in mybatis later.

environments、environment、transactionManager、dataSource

These four tags are nested and used together, as follows:

insert image description here
Multiple environments can be configured in environments to represent different data sources, each data source corresponds to an environment, and each environment has a unique id to distinguish different data sources.

transactionManager is used to configure the transaction manager. There are two ways: JDBC and MANAGED. Generally, JDBC can be used. In the spring+mybatis project, no configuration is required here, and spring manages transactions.

dataSource is used to configure data source connection information, transaction isolation level, number of connections, timeout, etc.

<!--  enviroments中可以配置多个environment, 其中default属性指定某种环境, 可以达到快速切换的效果  -->
<environments default="development">
	<!-- 用于配置一个具体的环境信息,id属性代表当前环境的唯一标识,enviroments中的default属性就是根据这里的id进行转换 -->
	<environment id="development">
		<!-- 事务管理器:
				type属性:事务管理器的类型,可以是自带的JDBC,也可以是自定义的事务管理器
				自定义的事务管理器实现TransactionFactory接口,设置type时,使用全类名
		-->
		<transactionManager type="JDBC"/>
		<!-- 数据源:
				type属性:数据源类型有三个UNPOOLED、POOLED、JNDI
				也可以使用自定义数据源,自定义的数据源实现DataSourceFactory接口,设置type时,使用全类名
		-->
		<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>

insert image description here

databaseIdProvider

Support multiple database vendors, the specific use is as follows:

<!-- 配置支持多个厂商的数据库 -->
<databaseIdProvider type="DB_VENDOR">
	<property name="MySQL" value="mysql"/>
	<property name="Oracle" value="oracle"/>
</databaseIdProvider>

The database configured here is used in the sql mapping file, specifying the database type corresponding to the sql statement

<?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.app.test.mapper.UserMapper">
    <select id="selectUser" parameterType="long" resultType="com.app.test.mapper.User" databaseId="mysql">
        select * from oa_user where id = #{
    
    id}
    </select>
 
    <select id="selectUser" parameterType="long" resultType="com.app.test.mapper.User" databaseId="oracle">
        select * from oa_user where id = #{
    
    id}
    </select>
</mapper>

mappers

Use the mappers tag to register the sql mapping file into the global configuration file

<mappers>
	<mapper resource="com/app/test/mapper/UserMapper.xml"/>
	
	<!-- 批量注册 -->
	<package name="com.app.test.mapper"/>
</mappers>

The above is a brief introduction to the mybatis global configuration file.

The article has been slightly modified, refer to the article: https://blog.csdn.net/chaizepeng/article/details/119605823

Guess you like

Origin blog.csdn.net/u014641168/article/details/130319671