"Turn" MyBatis Learning Summary (iii)--optimize the configuration in the MyBatis configuration file

Source: Internet
Author: User

"Turn" MyBatis Learning Summary (iii)--optimize the configuration in the MyBatis configuration file

One, 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" />Ten                 < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/mybatis" /> One                 < Propertyname= "username"value= "root" /> A                 < Propertyname= "Password"value= "XDP" /> -             </DataSource> -         </Environment> the     </Environments> -      - </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>4 5     <!--referencing the db.properties configuration file -6     <PropertiesResource= "Db.properties"/>7     <!-- 8 Development: Development model9 work: Working modeTen       - One     <Environmentsdefault= "Development"> A         <EnvironmentID= "Development"> -             <TransactionManagertype= "JDBC" /> -             <!--Configure database connection information - the             <DataSourcetype= "Pooled"> -                 <!--the Value property value references the values configured in the Db.properties configuration file - -                 < Propertyname= "Driver"value= "${driver}" /> -                 < Propertyname= "url"value= "${url}" /> +                 < Propertyname= "username"value= "${name}" /> -                 < Propertyname= "Password"value= "${password}" /> +             </DataSource> A         </Environment> at     </Environments> -      - </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:

 <!--  create user  -->  <  insert  id  = "AddUser"   ParameterType  Span style= "color: #0000ff;" >= "  Me.gacl.domain.User  "     </ 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 much 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 of " _ User Span style= "color: #000000;" ", as follows:
in the Conf.xml file <configuration></ The following configuration is added to the configuration> tag:

 <  typealiases  >  <  typealias  type  = "Me.gacl.domain.User"   alias  = "_user"  />  </ typealiases  >  

This can be Me.gacl.domain.User _user, after _user Represents the me.gacl.domain.User < Span style= "color: #000000;" The Me.gacl.domain.User for the SQL mapping XML file that you want to refer to. < Span style= "color: #000000;" You can use _user Instead, this achieves a simplification of the entity class reference.

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 user  -        < Packagename= "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.

"Turn" MyBatis Learning Summary (iii)--optimize the configuration in the MyBatis configuration file

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.