MyBatis Study Summary (iii)--optimize configuration in MyBatis configuration file (reprint)

Source: Internet
Author: User

Aloof, pale wolf

Only to find a way to succeed, not to find excuses for failure!

MyBatis Study Summary (iii)--optimize configuration in MyBatis configuration file The configuration of the connection database is placed in a single properties file

Previously, we wrote the connection configuration information of the database directly in the MyBatis Conf.xml file, as follows:

1<?XML version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd">3<Configuration>4<EnvironmentsDefault= "Development">5<EnvironmentId= "Development">6<TransactionManagerType= "JDBC"/>7<!--Configure Database connection Information-8<DataSourceType= "Pooled">9<PropertyName= "Driver"Value= "Com.mysql.jdbc.Driver"/>10<PropertyName= "url"Value= "Jdbc:mysql://localhost:3306/mybatis"/>11<PropertyName= "username"Value= "Root"/>12<property name= "password"  Value= "XDP" />13 </datasource>14 </ environment>15 </environments>16 17 </ configuration>        

In fact, we can completely write the connection configuration information of the database in a properties file, and then reference the properties file in the Conf.xml file, as follows:

1. Create a new db.properties file in the SRC directory, as shown in:

  

In the Db.properties file to write the connection database required to use the database driver, the connection URL address, user name, password, as follows:

1 driver=com.mysql.jdbc.Driver2 url=jdbc:mysql://localhost:3306/mybatis3 name=root4 PASSWORD=XDP     

2. Refer to the Db.properties file in the MyBatis Conf.xml file as follows:

1<?XML version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd">3<Configuration>45<!--Referencing the db.properties configuration file-6<PropertiesResource= "Db.properties"/>7<!--8Development: Development model9Work: Working mode10-11<EnvironmentsDefault= "Development">12<EnvironmentId= "Development">13<TransactionManagerType= "JDBC"/>14<!--Configure Database connection Information-15<DataSourceType= "Pooled">16<!--The Value property value references the values configured in the Db.properties configuration file-17<PropertyName= "Driver"Value= "${driver}"/>18<PropertyName= "url"Value= "${url}"/>19<PropertyName= "username"Value= "${name}"/>20<property name= "password"  Value= "${password}" / >21 </datasource< Span style= "COLOR: #0000ff" >>22 </environment>23 </environments>< Span style= "COLOR: #008080" >24 25 </configuration>        
Define aliases for entity classes, simplifying references in SQL mapping XML files

Previously, when we referenced entity classes in the SQL mapping XML file, we needed to write the full class name of the entity class (package name + class name), as follows:

<!---<id= "addUser" parametertype= "me.gacl.domain.User" > INSERT INTO Users (name,age) values (#{name},#{age})</Insert>   

ParameterType= "me.gacl.domain.User" here write the entity class User's full class name me.gacl.domain.User, It's a lot of trouble to write a long list of things every time, and we want to be able to do it in the following form

<id= "addUser2" parametertype= "_user"> insert INTO Users ( Name,age) VALUES (#{name},#{age})</Insert>        

Parametertype= " _ User "This is a lot easier to write, in order to achieve this effect, we need to =" in the Conf.xml file for the entity class me.gacl.domain.User " define an alias for " _ User "as follows:
Add the following configuration to the <configuration></configuration> tag in the Conf.xml file:

 <typealiases > <typealias type< Span style= "COLOR: #0000ff" >= "Me.gacl.domain.User"  alias = "_user" /></typealiases>       

This can be me.gacl.domain.User _user, after _user represents the me.gacl.domain.User Me.gacl.domain.User < The _user can be used anywhere in the span style= "color: #000000" category Instead, this achieves the purpose of simplifying the reference to the entity class.

In addition to the use of <typealias type= "Me.gacl.domain.User" alias= "_user"/> In this way, you can set aliases for an entity class individually, and we may also use the following method to set up aliases for all entity classes under a package, as follows:

<!--Configure an alias for an entity class to configure the entity class name so that it can be used instead of entity classes when referencing entity classes, for shorthand purposes-<Typealiases> <!-- Configure an alias for the entity class Me.gacl.domain.User _user --> <!-- <typealias type= "Me.gacl.domain.User" alias= "_user"/> -- > <!-- To configure aliases for all entity classes under the Me.gacl.domain package, mybatis The default setting alias is to remove the simple class name of the package where the class is located, such as Me.gacl.domain.User, the alias of the entity class will be set to the user --> <package name=" Me.gacl.domain "/> </typealiases>    

< Package name= "Me.gacl.domain"/> is an alias that is set for all entity classes that are underneath the bundle. MyBatis The default setting alias is to remove the simple class name of the package in which the class is located, such as Me.gacl.domain.User, the alias of the entity class will be set to user.

MyBatis Study Summary (iii)--optimize configuration in MyBatis configuration file (reprint)

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.