mybatis--optimizing configuration in the MyBatis configuration file

Source: Internet
Author: User

Original: http://www.cnblogs.com/xdp-gacl/p/4264301.html

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:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><Configuration>    <Environmentsdefault= "Development">        <EnvironmentID= "Development">            <TransactionManagertype= "JDBC" />            <!--Configure database connection information -            <DataSourcetype= "Pooled">                < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver" />                < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/mybatis" />                < Propertyname= "username"value= "root" />                < Propertyname= "Password"value= "XDP" />            </DataSource>        </Environment>    </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:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><Configuration>    <!--referencing the db.properties configuration file -    <PropertiesResource= "Db.properties"/>    <!--Development: Development mode work: Working mode -    <Environmentsdefault= "Development">        <EnvironmentID= "Development">            <TransactionManagertype= "JDBC" />            <!--Configure database connection information -            <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>        </Environment>    </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:

<!---<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, each time write such a long list of content is very troublesome, And we want to be able to write 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 conf.xml file for the entity class = "Me.gacl.domain.User "Define an alias as"_User", as follows:
Add the following configuration to the <configuration></configuration> tag in the Conf.xml file:

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

This allows you to define an alias of _user for the Me.gacl.domain.User class , and then _user represents the Me.gacl.domain.User class, so that any place in the SQL mapping XML file that needs to refer to the Me.gacl.domain.User class can be replaced with _user , which achieves a simplification of the entity class reference.

In addition to using <typealias type= "Me.gacl.domain.User" alias= "_user"/> This way to set an alias for an entity class individually, We can also set aliases for all entity classes under a package in bulk using the following method:

<!--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"/> means setting aliases for all entity classes under this package. 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--optimizing 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.