Detailed configuration parameters for the key XML field mappings in the Java MyBatis Framework _java

Source: Internet
Author: User
Tags aliases rollback

Properties
These are externalized, replaceable properties that can also be configured in a typical Java property configuration file, or passed through child elements of a properties element. For example:

<properties resource= "Org/mybatis/example/config.properties" >  
  <property name= "username" value= "dev_" User "/>  
  <property name=" Password value= "F2fa3!33tyyg"/>  
</properties>  

The properties can be used throughout the configuration file to implement dynamic configuration using replaceable properties. Like what:

<datasource type= "Pooled" >  
  <property name= "Driver" value= "${driver}"/> <property name=  
  "url" Value= "${url}"/>  
  <property name= "username" value= "${username}"/> <property name= "password"  
  Value= "${password}"/>  


The username and password in this example will be replaced by the values set in the properties element. The driver and URL properties will be replaced with values from the included Config.properties file.

Settings

1.cacheEnabled
This configuration enables the global mapper to enable or disable caching.
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, objects with deferred load properties will completely load arbitrary properties when invoked. Otherwise
Each property will be loaded as needed.
Valid values: True,false
Default value: True

4.mult ipleresultsetsenabled
Allow or disallow multiple result sets to be returned from a single statement (requires a suitable driver)
Valid values: True,false
Default value: True

5.useColumnLabel
Use column labels instead of column names. Different drives differ in this convenient performance. Reference driver documentation or full measurement
Try two ways to decide which drive to use.
Valid values: True,false
Default value: True

6.useGeneratedKeys
Allows JDBC to support generated keys. Need to fit the driver. If set to true this setting forces
The generated key is used, although some drivers are rejected compatible but still valid (for example, Derby)
Valid values: True,false
Default value: False

7.autoMappingBehavior
Specifies how MyBatis automatically maps columns to fields/properties. Partial will only automatically map simple, no nested results.
Full automatically maps arbitrary and complex results (nested or otherwise)
Valid values: None,partial,full
Default value: PARTIAL

8.defaultExecutorType
Configures the default executor. There's nothing special about simple actuators. A preprocessing statement is used by the reuse execution.
BATCH Statement and Batch update
Valid values: Simple,reuse,batch
Default value: Simple

9.defaultStatementTimeout
Sets the timeout time, which determines when the driver waits for a database response.
Valid values: Any,positive,integer
Default value: Not Set (null)

An example of setting an information element, the full configuration looks like this:

<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 a short name that is named for the Java type. It is only related to the XML configuration, and is used only to reduce the redundant part of the fully qualified name of the class. For example:

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


Using this configuration, "Blog" can be used as an alternative to the "Domain.blog.Blog" used in place. For ordinary Java types, there are many built-in type aliases. They are both case insensitive, due to overloaded names, to be aware of the native type of special handling.

Typehandlers
whether the MyBatis sets a parameter in a preprocessing statement, or when a value is fetched from the result set, the type processor is used to convert the obtained value to the Java type in the appropriate way. The following table describes the default type processor.

You can override the type processor or create your own type processor to handle unsupported or non-standard types. But this situation is quite small!!

Objectfactory
MyBatis Each time a new instance of the result object is created, it is completed with a objectfactory instance. If a parameter mapping exists, the default objectfactory does more work than instantiating the target class with a default constructor or a constructor with parameters. If you want to rewrite the default objectfactory, you can create your own. Instance slightly.

Plugins
MyBatis allows you to intercept a call that has been executed by a mapped statement at some point. By default, MyBatis allows the use of Plug-ins to intercept method calls:

1.Executor
(update, query, Flushstatements, Commit, rollback, gettransaction, close, isclosed)

2.ParameterHandler
(Getparameterobject, Setparameters)

3.ResultSetHandler
(Handleresultsets, Handleoutputparameters)

4.StatementHandler
(Prepare, parameterize, batch, update, query)

Environments
MyBatis can be configured in a variety of environments. This will help you apply the SQL mapping to a variety of databases.
A very important question to remember: You can configure a variety of environments, but you may only choose one for each sqlsessionfactory instance. So, if you want to connect two databases, you need to create two sqlsessionfactory instances, one for each database. And if it's three databases, you need three instances, and so on.
The environment element defines how the environment is configured, as an 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. The default environment ID (for example: default= "Development").
2. The environment ID (for example: id= "development") defined by each environment element.
3. The configuration of the transaction manager (for example: Type= "JDBC").
4. The configuration of the data source (for example: type= "pooled").

TransactionManager
There are two types of transaction managers in MyBatis (that is, type= "[jdbc| MANAGED] ".
1.JDBC---This configuration simply uses the JDBC submission and rollback settings directly. It relies on the connections that are obtained from the data source to manage the transaction scope.
2.MANAGED---This configuration has little to do. It never commits or rolls back a connection. It allows the container to manage the entire lifecycle of the transaction (such as the context of the spring or JEE Application server). By default, it closes the connection. However, some containers do not 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 of these transaction managers requires any attributes. However, they are all type aliases, and to replace them, you need to place the fully qualified name or type alias of your own class, which references your implementation class for the Transacfactory interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.