4. MyBatis several areas that can be optimized

Source: Internet
Author: User
Tags aliases

Rather than optimization, it's better to make the configuration simpler.


One: The configuration of the connection database is placed in a single property file

Create a new Db.properties property file under the SRC path and put the database connection information in the file, and then import the property file reference in the Conf.xml file to be better managed.

1. Create a new db.properties file

2. Write attribute Information

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/77/DA/wKiom1ZvuJvyza1TAACHDXbmMfc702.png "title=" db.png "alt=" Wkiom1zvujvyza1taachdxbmmfc702.png "/>

3. Import the properties file in Conf.xml

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/77/D9/wKioL1ZvuP7RvYW2AABtbqofi8c386.png "title=" in.png "alt=" Wkiol1zvup7rvyw2aabtbqofi8c386.png "/>


4. Referencing values in the properties file

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/77/D9/wKioL1ZvuWmw72H0AADD_u0GLKk131.png "title=" Use.png "alt=" Wkiol1zvuwmw72h0aadd_u0glkk131.png "/>


Two: Define aliases for entity classes, simplifying references in SQL mapping files.

This is the case when you previously referred to the person type in the Personmapper.xml file.

Resulttype= "Com.mybatis.entities.Person"//package name +; class Name

This kind of thing is more complicated to write when it is quoted more often. Complex is certainly a simple method.

In Conf.xml, use this tag (<typeAliases></typeAliases>) to alias an entity class. You can then refer to this alias in the Personmapper.xml file.

There are two methods of aliases:

1. Alias the class directly

Add the following label to the <configuration> tab in the Conf.xml file

<typealiases><typealias type= "Com.mybatis.entities.Person" alias= "_person"/></typealiases>

Next alias is an alias, we can use it directly in the Personmapper.xml file _person.

<select id= "getpersons" parametertype= "int" resulttype= "_person" >select ID, name, age from person</select>


2. Aliases for Wrap

Add the following label to the <configuration> tab in the Conf.xml file

<typeAliases><!--<typealias type= "Com.mybatis.entities.Person" alias= "_person"/>--><package Name= "Com.mybatis.entities"/></typealiases>

Note: Using the package sub-label, the value of name fills in the packages name where the entity class resides. It is possible to refer directly to the class name when referencing it.

<select id= "Getperson" parametertype= "int" resulttype= "person" >select ID, name, age from person WHERE id=#{id}< /select>

I personally prefer the second method (to wrap aliases).


This blog post source code click Http://pan.baidu.com/s/1pJXiGVT Download.


This article is from the "focus on Java,linux Technology" blog, please be sure to keep this source http://wuqinglong.blog.51cto.com/9087037/1723209

4. MyBatis several areas that can be optimized

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.