MyBatis configuration file Mybatis-config.xml Configuration resolution

Source: Internet
Author: User

1.MyBatis uses XML or annotations to configure the statement objects that need to be executed, mapping through the SQL in Java objects and statement objects, generating the final SQL statement, The MyBatis framework then executes the SQL and maps the result to a Java object and returns.

The 2.MyBatis configuration file contains information that affects mybatis behavior, and the document is structured as follows:

(1) Properties Property

You can add a Db.properties property file in Classpath to configure the properties of the connection data:

Mysql_driver=com.mysql.jdbc.driver
Mysql_url=jdbc:mysql://localhost:3306/mybatisdb?characterencoding=utf8
mysql_username=root
Mysql_password=root
Configure <properties/> properties in the configuration file to load the property file:

<!--load Property file--> 
<properties resource= "Db.properties"/>
Properties in the properties file can be taken to the configuration file to replace the property values that need to be dynamically configured:

<!--dataSource Value data source configuration, pooled is the implementation of the data source connection pool of the JDBC Connection object-->  
            <datasource type= "Pooled" >  
                <property Name= "Driver" value= "${mysql_driver}"/> <property name=  
                "url" value= "${mysql_url}"/>  
                Name= "username" value= "${mysql_username}"/> <property name= "password"  
                value= "${mysql_password}"/>  
            </dataSource>  
This configuration can make the configuration more flexible, you need to modify the property value directly to the property file to modify it.

(2) Settings setting

Primarily used to configure properties that change MyBatis run-time behavior, such as configuration logs

<settings>  
        <setting name= "Logimpl" value= "log4j"/>  

(3) typealiases type naming

Primarily used to alias classes, you can reduce the redundancy of class fully qualified names.

The format is as follows:

<typeAliases>
	<!--define a single alias 
	<typealias type= "Com.domain.User" alias= "User"/>
	-->
	<!--batch alias definition, scan all the classes under the entire package, alias is the class name (can be the first case)-->
	<package name= "Com.po"/> <package name=
	"other packages"/ >
</typeAliases>

(4) Typehandlers type processor

Whether MyBatis sets a parameter in a preprocessing statement (PreparedStatement), or when a value is fetched from the result set, the class processor converts the obtained value to the Java type in the appropriate way.

(5) Objectfactory Object Factory

MyBatis each time a new instance of the result object is created, it is completed using an object factory (objectfactory) instance.

(6) Plugins plugin

(7) Environments environment

<environments default= "Development" >  
        <environment id= "Development" >  
            <!--Specify transaction management type, type= " JDBC "refers to the direct and simple use of JDBC's commit and rollback settings-->  
            <transactionmanager type=" jdbc "/>  
            <!--datasource data source configuration, Pooled is the implementation of the data source connection pool for the JDBC Connection object-->  
            <datasource type= "Pooled" > <property name= "Driver" value= "${"  
                Mysql_driver} "/>  
                <property name=" url "value=" ${mysql_url} "/> <property name=" username "value="  
                ${mysql_username} "/>  
                <property name= password" value= "${mysql_password}"/>  
            </dataSource>  
        </environment>  
</environments>  

Environment Environment variables

The environment ID defined by each environment element, optionally named

TransactionManager transaction Manager

<transactionmanager type= "JDBC"/>  
JDBC: Directly using the JDBC Commit and rollback settings, it relies on the connections from the data source to manage the transaction scope.

MANAGED: It hardly does anything. It never commits or rolls back a connection, but instead lets the container manage the entire lifecycle of the transaction. By default, it closes the connection, and you can set the CloseConnection property to False to prevent its default shutdown behavior.

DataSource Data source

(8) Databaseidprovider database manufacturer identification

(9) Mappers Mapper

There are several ways to map files:

<!--Mappers told MyBatis where to find the mapping file for the persisted class-->  
    <mappers> 
    	<!--use the Classpath to find resource files--> 
        <mapper Resource= "Com/mapper/usermapper.xml"/>  
    </mappers>
    <mappers> 
    	<!--using local files--> 
        <mapper url= "File:///F:/mapper/UserMapper.xml"/>  
    </mappers>  
    <mappers> 
    	<!- -Use interface class--> 
        <mapper class= "Com.mapper.UserMapper"/>  
    </mappers>  
    <mappers> 
    	<!--Use package name--> 
        <package name= "Com.mapper"/>
    </mappers>  
Note: When using interface classes and using package names, Usermapper.xml and Usermapper.java (interfaces) must be in the same directory and must have the same name.







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.