Differences between ResultMap and ResultType in use

Hello, I'm architecture Jun, an architect who can write code and recite poetry. Today, let's talk about the difference between ResultMap and ResultType in use. I hope it can help you make progress!!!

When using mybatis for database connection, there are usually two ways to process the results returned by SQL statements, one is resultType, the other is resultMap. Let's talk about my understanding and understanding of these two

resultType: when using resultType to process the returned result type of SQL statement, the field queried by SQL statement must have the same field corresponding to it in the corresponding pojo, and the content in resultType is the position of pojo in this project.

Therefore, for single table query, resultType is the most appropriate. However, if you do not want to use the field name defined in the database table when writing pojo, you can also use resultMap to process the corresponding. In the case of a one-to-one join query, a new pojo needs to be created. The pojo includes all the fields to be queried in the two tables. The processing method in this place is usually to create a pojo that inherits one table field, and then add the fields to be queried in another table. For one to many queries, if you use inner join queries, you are likely to find duplicate fields. Use double for loop nesting.

resultMap: when using resultMap to process the return result type of SQL statement, it is usually required in mapper Define resultMap in XML to correspond pojo and corresponding table fields.

[java] view plain copy

<!-- User associated with order query resultMap  
    Map the results of the entire query to cn.itcast.mybatis.po.Orders in  
     -->  
    <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">  
        <!-- Configure mapped order information -->  
        <!-- id: Specifies the unique in the query column A unique identifier in the order information One identifier. If multiple columns form a unique identifier, configure multiple columns id  
            column: Order information only I. identification column  
            property: Order information only I. identification Column mapped to Orders Which attribute in  
          -->  
        <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>  

resultMap usually handles one-to-one table connection by adding a pojo nested in another table in the pojo of the main table, and then in mapper XML uses the association node element to join another table. For example:

[html] view plain copy

<!-- User associated with order query resultMap  
    Map the results of the entire query to cn.itcast.mybatis.po.Orders in  
     -->  
 <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">  
 <!-- Configure mapped order information -->  
        <!-- id: Specifies the unique in the query column A unique identifier in the order information One identifier. If multiple columns form a unique identifier, configure multiple columns id  
            column: Order information only I. identification column  
            property: Order information only I. identification Column mapped to Orders Which attribute in  
          -->  
 <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 user information associated with the mapping -->  
        <!-- association: Information used to map Association queries to individual objects  
        property: To map the user information of the associated query to Orders Which attribute in  
         -->  
 <association property="user"  javaType="cn.itcast.mybatis.po.User">  
            <!-- id: Association query user I. identification  
            column: Specify only A column identifying user information  
            javaType: Map to user Which attribute of  
             -->  
 <id column="user_id" property="id"/>  
 <result column="username" property="username"/>  
 <result column="sex" property="sex"/>  
 <result column="address" property="address"/>  
 
 </association>  
 </resultMap>  

If the one to many table connection method is used, for example, the order table and the order details table are one to many connections. If the sql statement is not processed, because an order corresponds to multiple order details, the query results will be repeated for the order table data

The processing method of resultMap is to add a list in the pojo of the order table data. The list is the attribute of the order details, which is displayed in mapper XML adopts the following processing methods:

[html] view plain copy

!-- Of orders and order details resultMap  
    use extends Inheritance. There is no need to configure the mapping of order information and user information in  
     -->  
 <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap">  
 <!-- Order information -->  
 <!-- User information -->  
 <!-- use extends Inheritance. There is no need to configure the mapping of order information and user information in -->  
 
 
        <!-- Order details  
        Multiple details have been found in an order Association query, which should be used collection Mapping  
        collection: Multiple records are mapped to the collection object for association query  
        property: Map Association query to multiple records cn.itcast.mybatis.po.Orders Which attribute  
        ofType: Specify mapping to list In collection properties pojo Type of  
         -->  
 <collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail">  
            <!-- id: Order details I. identification  
            property:The order details to be I. identification Map to cn.itcast.mybatis.po.Orderdetail Which attribute of  
              -->  
 <id column="orderdetail_id" property="id"/>  
 <result column="items_id" property="itemsId"/>  
 <result column="items_num" property="itemsNum"/>  
 <result column="orders_id" property="ordersId"/>  
 </collection>  
 
 
 </resultMap>  

During query, although one order information corresponds to multiple order details, because multiple information details are stored in the list, duplicate data will not appear after query, which achieves the effect of de duplication

This concludes today's article. Thank you for reading, Java architect must see I wish you a promotion and a raise and good luck every year.

Added by truegilly on Tue, 11 Jan 2022 08:59:28 +0200