The usage of Ibatis Resultmap

Source: Internet
Author: User

Let's look at a specific example:

1     <ResultmapID= "Get-product-result"class= "Com.ibatis.example.Product" >2             <result Property= "id"column= "prd_id"/>3             <result Property= "description"column= "Prd_description"/>4     </resultmap>  5     <StatementID= "GetProduct"Resultmap= "Get-product-result" >6             SELECT * from PRODUCT7 </statement>  

Note Resultmap supports "SELECT *" and does not require the mapping of all returned fields to be defined resultset.

In the SQL Map framework, Result Map is an extremely important component. When executing a query mapped statement, RESULTMAP is responsible for mapping the column values of the result set to the attribute values of the Java bean. The structure of the Resultmap is as follows:

1 <ResultmapID= "Resultmapname"class= "Some.domain.Class"[Extends= "Parent-resultmap"]>2       <result Property= "PropertyName"column= "column_name"3             [ColumnIndex= "1"]4             [Javatype= "int"]5             [Jdbctype= "NUMERIC"]6             [Nullvalue= " -999999"]7             [Select= "Someotherstatement"]8       />  9       <result.../>  Ten       <result.../>   One       <result.../>   A </Resultmap> 

1.extends is an optional attribute that can be set to the name of another resultmap and based on it. Similar to inheriting a class in Java, the properties of the parent Resultmap will be part of the child Resutlmap. The properties of the parent Resultmap are always prepended to the child Resultmap property, and the parent resultmap must be defined before the child Resultmap. The class attributes of the parent Resultmap and child resultmap are not necessarily consistent, and they can have no relationship.

Attention! The column values of resultset are read in the order in which they are defined in Resultmap (this feature is useful in some JDBC driver that are not implemented well).

2. The value of the attribute columnindex is the field order number used to assign Java Bean properties in resultset.

Provides a scenario where we map the first few subscript fields of a dataset to the properties of a specified data object

3. The Nullvalue property specifies the substitution value for NULL in the database. Therefore, if a null value is read from resultset, the Java Bean property is assigned a value of NULL for the specified substitution value. The value of the property null can specify any value, but must be valid for the type of the Java Bean attribute.

4. The attribute Select is used to describe the relationship between objects and automatically load data for complex types (that is, user-defined type) properties. The value of the property select must be the name of another mapped statement element.

Let's talk about it. The property value must be the ID of a query statement that returns a collection of data, and the data class property that can be configured can be a primitive type, a composite type, or a collection type that includes multiple data types, all of which are row and no problem. One of its important existential significance is to describe the relationship between the different tables, and through this query, you can use the Select property when you want to query the related fields from another table without using the Join method.

1 <ResultmapID= "Demoresultmap"class= "Hashtable">  2        <result Property= "id"column= "id"/>  3        <result Property= "Children"column= "id"      4 Select= "Select_children"/>  5 </Resultmap>  6   7 <Statements>  8        <SelectID= "Select_children"ResultClass= "Childrenobject">  9 SELECT * from children WHERE parentid = #id #Ten        </Select>   One </Statements> 

The usage of Ibatis Resultmap

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: 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.