Properties
These are externalized, alternative properties that can also be configured in a typical Java property configuration file or passed through child elements of properties elements. For example:
<properties resource="org/mybatis/example/config.properties"> <property name="username" value="dev_user"/> <property name="password" value="F2Fa3!33TYyg"/> </properties>
The properties in it can be used throughout the configuration file, and replaceable properties are used to implement dynamic configuration. for example:
<dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource>
In this example, username and password will be replaced by the values set in the properties element. Driver and url properties will be replaced by the values contained in the config.properties file.
Settings
1.cacheEnabled
This configuration enables the global mapper to enable or disable cache.
Valid values: true, false
Default value: true
2.lazyLoadingEnabled
Enable or disable lazy loading globally. When disabled, all associated objects are loaded instantly.
Valid values: true, false
Default value: true
3.aggressiveLazyLoading
When enabled, an object with a delayed loading property will fully load any property when called. otherwise,
Each attribute will be loaded as needed.
Valid values: true, false
Default value: true
4.mult ipleResultSetsEnabled
Allow or do not allow multiple result sets to be returned from a separate statement (requires suitable drivers)
Valid values: true, false
Default value: true
5.useColumnLabel
Use column labels instead of column names. Different drivers are convenient for different performances. Refer to the driver documentation or test the full method to determine the driver used.
Valid values: true, false
Default value: true
6.useGeneratedKeys
Allows JDBC to support generated keys. Need a suitable drive. If set to true, this setting forces the generated keys to be used, and although some drivers refuse to be compatible, they are still valid (such as Derby)
Valid values: true, false
Default value: false
7.autoMappingBehavior
Specifies how MyBatis automatically maps columns to fields/attributes. PARTIAL will only automatically map simple and has no nested results.
FULL will automatically map any complex results (necked or otherwise)
Valid values: NONE, PARTIAL, FULL
Default value: PARTIAL
8.defaultExecutorType
Configure the default executor. There is nothing special about the SIMPLE actuator. The REUSE executor reuses preprocessing statements.
BATCH executor reuse statements and batch update valid values: SIMPLE, REUSE, BATCH
Default value: SIMPLE
9.defaultStatementTimeout
Set the timeout time, which determines the time the driver waits for a database response.
Valid values: Any, positive, integer
Default value: Not Set(null)
An example of setting information elements, the complete configuration 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="enhancementEnabled" value="false"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25000"/> </settings>
typeAliases
A type alias is to name a short name for Java types. It is only related to XML configuration and is only used to reduce the redundant parts of the fully qualified name of the class. For example:
<typeAlias alias="Author" type="domain.blog.Author"/> <typeAlias alias="Blog" type="domain.blog.Blog"/> <typeAlias alias="Comment" type="domain.blog.Comment"/> <typeAlias alias="Post" type="domain.blog.Post"/> <typeAlias alias="Section" type="domain.blog.Section"/> <typeAlias alias="Tag" type="domain.blog.Tag"/> </typeAlias>
With this configuration, "Blog" can be used as arbitrarily instead of where "domain.blog.Blog" is used. For normal Java types, there are many built-in type alias. They are both case-insensitive, and due to overloaded names, you should pay attention to special handling of native types.
typeHandlers
Whether MyBatis sets a parameter in a preprocessing statement or takes a value from the result set, the type processor is used to convert the obtained value into Java type in the appropriate way. The following table describes the default type processor.
You can rewrite the type processor or create your own type processor to handle unsupported or non-standard types. But this situation is quite rare! !
objectFactory
MyBatis is done using an ObjectFactory instance every time a new instance of the result object is created. If the parameter map exists, the default ObjectFactory does no more work than instantiating the target class using the default constructor or constructor with parameters. If you want to rewrite the default ObjectFactory, you can create your own. Example omitted.
plugins
MyBatis allows you to intercept calls executed by mapped statements at a certain point. By default, MyBatis allows plugins to intercept method calls:
1.Executor
(update, query, flushStatements, commit, rollback, getTransaction, close, isClose)
2. ParameterHandler
(getParameterObject, setParameters)
3.ResultSetHandler
(handleResultSets, handleOutputParameters)
4.StatementHandler
(prepare, parameterize, batch, update, query)
Environments
MyBatis can be configured with multiple environments. This will help you apply SQL mappings to multiple databases.
A very important question to remember: you can configure multiple environments, but you can only select one for each SqlSessionFactory instance. So, if you want to connect to two databases, you need to create two SqlSessionFactory instances, one for each database. And if it is three databases, you need three instances, and so on.
Environment elements define how to configure the environment, for example:
<environments default="development"> <environment id="development"> <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> Note here:
1. Default environment ID (for example: default="development").
2. The environment ID defined by each environment element (for example: id=”development”).
3. Transaction manager configuration (for example: type="JDBC").
4. Data source configuration (for example: type="POOLED").
transactionManager
There are two types of transaction managers in MyBatis (i.e. type="[JDBC|MANAGED]".
1.JDBC---This configuration directly and simply uses JDBC's commit and rollback settings. It relies on connections obtained from data sources to manage transaction scope.
2.MANAGED---This configuration does almost nothing. It never commits or rolls back a connection. And it allows the container to manage the entire life cycle of a transaction (such as the context of Spring or JEE application server). By default it closes the connection. However some containers don't want this, so if you need to stop it from the connection, set the closeConnection property to false. For example:
<transactionManager type="MANAGED"> <property name="closeConnection" value="false"/> </transactionManager>
Neither transaction manager requires any properties. However, they are all type aliases, and to replace them, you need to place your own fully qualified name or type aliases of your own class, which reference your implementation class for the TransacFactory interface.