Article Directory
1. Configuration overview
MyBatis
The configuration of will affect MyBatis
the settings and attribute information of the behavior. The following figure shows the attributes in the configuration file: It
should be noted mybatis-config.xml
that these attributes must be written in the order above!
Second, the role of each configuration
1. Properties
The function of this configuration is to reference an external configuration file. You can create a new .properties
file, write some configuration information for connecting to the database into the file, and then properties
import it with attributes. The demonstration is as follows:
Create a new configuration file db.properties
::
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/MyBatis_DB?serverTimezone=GMT
username=root
password=root
Then mybatis-config.xml
write the attribute:
<!-- properties 引入外部配置文件 -->
<properties resource="db.properties">
<!--
也可以在这里直接写
<property name="xxxx" value="xxxx" />
-->
</properties>
If an attribute is configured in more than one place, MyBatis will be loaded in the following order:
- First read the properties specified in the properties element body.
- Then read the property file under the classpath according to the resource attribute in the properties element, or read the property file according to the path specified by the url attribute, and overwrite the previously read property with the same name.
- Finally, the attribute passed as the method parameter is read, and the attribute with the same name read before is overwritten.
2. Settings
settings
It is a Mybatis
global configuration, MyBatis
some default configuration has been stipulated in it, if other configuration is needed, settings
just write it in the label.
An settings
example of a fully configured element is as follows:
<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>
The following table lists several more important configurations:
Set name | description | Effective value | Defaults |
---|---|---|---|
cacheEnabled |
Globally enable or disable any cache configured in all mapper configuration files |
true | false |
true |
lazyLoadingEnabled |
Delayed load global switch | true | false |
false |
logImpl |
Specify MyBatis the specific implementation of the log used, if not specified, it will be automatically searched |
SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | NO_LOGGING |
Not set |
3. typeAliases type alias
When writing MyBatis
a xml
document, you returnType
must fill in the full class name of the entity class every time you fill in it. For example,
this is really super troublesome. typeAliases
The function is only to give an alias for these full class names to simplify writing.
There are three ways of aliasing:
- Alias a single entity class
<typeAliases> <typeAlias alias="User" type="com.wzq.domain.User" /> </typeAliases>
- Specify a package name and convert all entity classes under the package to aliases (the default is the lowercase of the first letter of the class name)
<typeAliases> <package name="com.wzq.domain"/> </typeAliases>
- Use annotations ( you need to write in the previous step ), you can add annotations to the class name to alias
@Alias("User") public class User{ ... }
4. typeHandlers type processor
MyBatis
Statements provided pretreatment ( PreparedStatement
time) concentrated parameters or values taken from a result, the acquired values are converted in a suitable manner type processor Java
type. The following table describes some of the default type processors.
5. objectFactory object factory
Every MyBatis
time a new instance of the result object is created, it will use an object factory ( ObjectFactory
) to instantiate instances to complete the work. What the default object factory needs to do is to instantiate the target class, either through the default no-argument constructor, or through the existing parameter mapping to call the constructor with parameters. If you want to override the default behavior of the object factory, you can do so by creating your own object factory.
6, plugins
MyBatis
The plug-in function is still very powerful, and there are many extensions, such as:
mybatis-generator-core
mybatis-plus
- Universal
mapper
- and many more
7, environments environment configuration
MyBatis
Multiple environments can be configured, but: Although multiple environments can be configured, only one environment can be selected for each SqlSessionFactory instance.
<!-- default字段定义了当前使用的是哪一套配置 -->
<environments default="development">
<environment id="development">
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<!-- type = "POOLED" 规定了这里有数据库链接池 -->
<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>
<!-- 另一套配置 -->
<environment id="test">
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<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>
8, mappers mapper
mappers
The role is: register the binding Mapper
file, it has four ways to bind
Method 1: Useresource
<mappers>
<mapper resource="com/wzq/mapper/UserMapper.xml"/>
</mappers>
Method 2: Useclass
<mappers>
<mapper class="com.wzq.mapper.UserMapper"/>
</mappers>
But here needs Mapper
to Mapper.xml
be in the same package with the file, and have the same name, so is the third way!
Method 3: Register all mapper interface implementations in the package as mappers
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
The fourth method is to use url
binding, which is less used, so it will not be listed here!