MyBatis 3.2.8 User manual 4.1.5 Result Maps

Source: Internet
Author: User


Clearly such as the month QQ 605283073


Bachelor of Science in English translation major software engineering Hobby Java Learning 2 years +


Pure hobby, use spare time to translate. Please forgive me for the first attempt to translate.


4.1.5 Result Maps

Result Mapping (Resultmap) is the most important and powerful element in the MyBatis framework.

It saves you the code that gets the data 90% from the jdbc result set , and in some cases even provides the jdbc Features that are not supported.

In fact, it may even require thousands of lines of code to write equivalent code that includes complex statement mappings with joins. The concise statement of the result mapping (resultmaps) design does not require explicit result mappings, and even more complex statements require only the required relationship description.

You've seen it. Examples of simple mappings with no explicit result mapping (resultmap) are as follows:


This statement will list all columns from the specifiedResulttype(Result type) is automatically mapped toHashMapof theKey(Key). Although it is very useful in most cases,HashMapcannot make a good model domain. Your application will be more likely to useJavaBeanorPOJOsas a model domain. MyBatissupport for both of these. Let's take a look at the followingJavaBean:

Package Com.someapp.model;public class User {private int id;private string Username;private string hashedpassword;public int GetId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String Gethashedpassword () {return hashedpassword;} public void Sethashedpassword (String hashedpassword) {This.hashedpassword = Hashedpassword;}}

The above class, based on the JavaBean style, contains 3 Properties : Id,username,hashedpassword. These 3 attribute names are exactly the same as the class names in the SELECT statement. This JavaBean can be easily mapped to ResultSet(Result set) as HashMap.

<select id= "Selectusers" resulttype= "Com.someapp.model.User" >select ID, username, hashedpasswordfrom some_ Tablewhere id = #{id}</select>

Remembertypealiases (type aliases)it's your friend. Learn to use type aliases so you don't have to enter the full classpath. For example:

<!--in Config XML file--><typealias type= "Com.someapp.model.User" alias= "User"/><!--in SQL Mapping XML F Ile--><select id= "Selectusers" resulttype= "User" >select ID, username, hashedpasswordfrom some_tablewhere id = #{id}</select>

In these casesMyBatisCreate result sets automatically behind the scenes (Resultmapautomatically map columns by name toJavaBeanthe corresponding attribute. If the column name does not exactly match the alias you can use for the Query statement column (the standardSQLstyle) to make the label match. For example:

<select id= "Selectusers" resulttype= "User" >selectuser_id as "id", user_name as "UserName", Hashed_password as " Hashedpassword "from Some_tablewhere ID = #{id}</select>

The great thing about Resultmaps is that you learn a lot, but haven't seen his effect !

These simple examples require a bit of configuration that you can see.

For the sake of demonstration, let's look at the problem of using a different external result mapping (resultmap) To resolve the mismatch between the column name and the property name in the previous example.

<resultmap id= "Userresultmap" type= "User" ><id property= "id" column= "user_id"/><result property= " Username "column=" user_name "/><result property=" password "column=" Hashed_password "/></resultmap>

This statement is implemented by referencing the result mapping (RESULTMAP) Attribute (Note: We removed the result type). For example:

<select id= "Selectusers" resultmap= "Userresultmap" >select user_id, user_name, Hashed_passwordfrom some_ Tablewhere id = #{id}</select>

Now it would be nice if the world were always so simple.

MyBatis 3.2.8 User manual 4.1.5 Result Maps

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.