Resultmap of mybatis learning notes

Source: Internet
Author: User

When using mybatisResultmap

Compared with resultclass, resultmap can adapt to more complex link ing, and allows you to specify the Data Type of a field. It supports "select *" and does not require defining Mappings of all returned fields in the resultset.

Configure the following in resultmap:

 

<Resultmap id = "baseresultmap" type = "full path of the class"> <ID column = "Table ID column" property = "corresponding class attribute" jdbctype = "type of data inserted into the database "/> <result column =" other columns in the table "property =" "jdbctype =" "/>
/* If the class to be mapped contains objects as class attributes (the class contains other classes), use association in resultmap,
If collection is used for a collection (one-to-multiple), if column is used for collection, it is generally used with select */<association property = "projectorg" javatype = "com. ldrc. SRM. ou. dal. dataobject. projectinfo. project. projectorg "resultmap =" projectorgmapper. baseresultmap "> </association> <collection property =" rolelist "column =" us_id "oftype =" com. zjx. entity. system. role"
Resultmap = "rolemapper. baseresultmap" select = "rolemapper. selectuserroles">
</Collection> </resultmap>

Resultmap is the most common and important attribute. The value of the resultmap attribute is equal to the pre-defined resultmap element.

(See the following example ). Using resultmap can control how data is retrieved from the result set and which attribute matches which field. Unlike the automatic resing method of resultclass, The resultmap attribute allows you to specify the Data Type of a field, and the replacement value of null for complex type ing (including other Java Beans, set types, and basic type packaging classes ). The detailed discussion of the resultmap is described in a later chapter. Here we only provide an example of the resultmap of the relevant statement.

<Resultmap id = "get-product-result" class = "com. ibatis. example. Product">

<Result property = "ID" column = "prd_id"/>

<Result property = "Description" column = "prd_description"/>

</Resultmap>

<Statement id = "getproduct" resultmap = "get-product-result">

Select * from product

</Statement>

In the preceding example, with the resultmap definition, the resultset obtained by the query statement is mapped to the product pair.

Image. The "ID" attribute value defined by resultmap is assigned with the "pro_id" field value, and the "Description" attribute value is assigned with the "prd_description" field value. Note that resultmap supports "select *" and does not require the resing of all returned fields in the resultset.

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.