Differences in use of Resultmap and Resulttype

Source: Internet
Author: User

There are two ways to process the results returned by the database:

Resulttype: When you use Resultype to do the return result processing, the fields queried by SQL must correspond to the fields in the corresponding Pojo, so resulttype is most appropriate for single-table queries.

Resultmap: If the fields in the Pojo do not want to use the field names in the database, they can be processed with Resultmap.

Multi-Table query: If you are a one-off connection query, you need to create a new Pojo,pojo that includes two tables in the fields that need to be queried

Resultmap: When using RESULTMAP for SQL statements to return result type processing, it is often necessary to define RESULTMAP for Pojo and corresponding table fields in Mapper.xml.

<!--order Query the Resultmap of the associated user maps the results of the entire query to Cn.itcast.mybatis.po.Orders--  
    <resultmap type= " Cn.itcast.mybatis.po.Orders "id=" Ordersuserresultmap ">  
        <!--Configure the map's order information--  
        <!--ID: Specifies the unique identity in the query column, Unique identification in the order information, if multiple columns constitute a unique identity, configure multiple ID column  
            : Unique identifier for order Information  
          property: The unique identity column of the order information is mapped to which attribute  
        in orders-- <id column= "id" property= "id"/>  
        <result column= "user_id" property= "UserId"/>  
        <result column= "Number" property= "number"/>  
        <result column= "Createtime" property= "Createtime"/>  
        <result column= "Note" property= "note"/>         
    

Resultmap for a one-to-one table connection is typically to add pojo that nest another table in the Pojo of the primary table, and then use the association node element in Mapper.xml to handle the connection to the other table. For example:

<!--order Query the Resultmap of the associated user maps the results of the entire query to Cn.itcast.mybatis.po.Orders--<resultmap type= "Cn.itcast.mybatis.po. Orders "id=" Ordersuserresultmap "> <!--Configure the map's order information--<!--ID: Specifies the unique identity in the query column, the unique identifier in the order information, if any  
        Multiple columns make a unique identity, configure multiple ID column: Unique identifier for order Information property: The unique identity column of the order information is mapped to which attribute in orders-- <id column= "id" property= "id"/> <result column= "user_id" property= "UserId"/> <  
        Result column= "number" property= "number"/> <result column= "createtime" property= "Createtime"/> <result column= "Note" Property=note/> <!--Configure the associated user information for the mapping-<!--Associati On: Used to map information about a single object in an association query property: To map the user information for the associated query to which attribute in orders--<association property= "user  
            "Javatype=" Cn.itcast.mybatis.po.User > <!--ID: Unique identity of the associated query user column: Specifies the columns that uniquely identify the user information Javatype: Mapping to UserWhich property--<id column= "user_id" property= "id"/> <result column= "user Name "property=" username "/> <result column=" sex "property=" sex "/> <result column="  Address "property=" Address "/> </association> </resultMap>
If a one-to-many table connection, such as the order table and the order schedule is a one-to-many connection, if the SQL statement is not processed, because an order corresponds to a number of order details, so the query results for the order table data will be duplicated, for example:

The resultmap is handled by adding an attribute in the list,list to the order schedule in the Pojo of the Order table data, which is handled in Mapper.xml as follows:

!--orders and Order Details Resultmap use extends inheritance without configuring the mapping of order information and user information--<resultmap type= "Cn.itcast.mybatis.po.Or  DERs "id=" Ordersandorderdetailresultmap "extends=" Ordersuserresultmap "> <!--Order Information--<!--  
        User Information-<!--use extends inheritance without configuring the mapping of order information and user information-<!--Order Details An Order association query has multiple details to be mapped using collection collection: Map the associated query to multiple records to the property in the collection object: Map The associated query to multiple records to Cn.itcast.mybatis . po. Orders which property OfType: Specifies the type that is mapped to the Pojo in the list collection properties--<collection property= "OrderDetails" ofty Pe= "Cn.itcast.mybatis.po.Orderdetail" > <!--ID: Order Details uniquely identifies the property: to map the unique identification of the order details to the CN.ITC  
            Which property of Ast.mybatis.po.Orderdetail--<id column= "orderdetail_id" property= "id"/> <result column= "items_id" property= "Itemsid"/> <result column= "items_num" property= "item  
       Snum "/>     <result column= "orders_id" property= "Ordersid"/> </collection> </re  Sultmap>
In the query, although an order information corresponding to a number of order details, because a number of information is stored in the list, so the query will no longer appear duplicate data, to achieve the effect of deduplication

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.