The difference between ResultMap and ResultType

Develop a habit, like first and then watch!!!

The difference between the two is mainly in the return type.

The data types we may return in mybatis are mainly the following two:

  • Single collection-ResultType, ResultMap

  • Multiple collections-ResultMap

So what kind of data is called a single set, and what kind of data is called a multiple set? We can understand through the following two pictures:

Single collection :

Insert picture description here

Multiple sets :

Insert picture description here

After reading the above two pictures, everyone can basically know the difference between them, so what is the specific use scenario of the two of them?

Let me give you two examples

At 获取所有用户的数据the time obviously we just need to get information to the user and does not need to obtain any additional data.

But in 进行用户的角色校验time, it is clear that we need not only to get information about the user, and secondly we also need to get the user's roles, then obviously we returned data can not only contain user information, so we have the data encapsulated in the above-mentioned multiple collections In order to facilitate our role verification.

After understanding the above concepts, everyone basically understands the difference between the two, but everyone has to ask again. Above you said that ResultMap can be used for both a single collection and multiple collections, so why don't we use them all? What about ResultMap? How about using ResultType?

This is mainly because ResultType虽然只针对单个集合, but he is 可以直接调用我们已经编写好的实体类, but ResultMap is different, 不管如何都需要我们进行自定义so it is mainly used in the case of multiple collections , and ResultType is still used in the case of a single collection.

In this way, everyone can basically understand the difference between the two of them. After understanding the difference, we will explain how to use them in detail:

  • ResultType

ResultType is relatively simple to use. As we have said above, we can directly call our entity class, so we basically just copy the relative path of the entity class directly. Here is a chestnut:

<select id="selectAll" resultType="com.auguigu.gmall.bean.PmsProductInfo">
select * from pms_product_info
</select>

It can be seen that we only need to directly assign the path of the entity class we need to return to the ResultType, which is simple and convenient.

  • ResultMap

But ResultMap is relatively more troublesome, in fact, the main thing is 需要告诉Mybatis你是将那几个实体类进行多重组合的, so that the rest can be done by mybatis. Or through the following chestnuts, we will explain in detail.

Two entity classes of multiple sets:

  • PmsProductSaleAttr
public class PmsProductSaleAttr implements Serializable {
    
    

    @Id
    @Column
    Integer id;

    @Column
    Integer productId;

    @Column
    Integer saleAttrId;

    @Column
    String saleAttrName;

    @Transient
    List<PmsProductSaleAttrValue> pmsProductSaleAttrValueList;
}
  • PmsProductSaleAttrValue
public class PmsProductSaleAttrValue implements Serializable {
    
    
    @Id
    @Column
    Integer id;

    @Column
    Integer productId;

    @Column
    Integer saleAttrId;

    @Column
    String saleAttrValueName;

    @Transient
    Integer isChecked;

}

After that, let's take a look at the code we defined in the mapper.xml file:

<select id="selectspuSaleAttrListCheckBySku" resultMap="selectspuSaleAttrListCheckBySkuMap">
        SELECT
            sa.id as sa_id,
            sav.id as sav_id,
            sa.*,
            sav.*,
            if(ssav.sku_id,1,0) as isChecked
        FROM
            pms_product_sale_attr sa
                INNER JOIN pms_product_sale_attr_value sav ON sa.product_id = sav.product_id
                AND sa.sale_attr_id = sav.sale_attr_id
                AND sa.product_id = #{
    
    productId}
                LEFT JOIN pms_sku_sale_attr_value ssav ON sav.id = ssav.sale_attr_value_id
                AND ssav.sku_id = #{
    
    skuId}
                ORDER BY sav.id
    </select>
    <resultMap id="selectspuSaleAttrListCheckBySkuMap" type="com.auguigu.gmall.bean.PmsProductSaleAttr" autoMapping="true">
        <result column="sa_id" property="id"></result>
        <collection property="pmsProductSaleAttrValueList" ofType="com.auguigu.gmall.bean.PmsProductSaleAttrValue" autoMapping="true">
            <result column="sav_id" property="id"></result>
        </collection>
    </resultMap>

Before analyzing the code, let's take a look at the data we get after the SQL statement is executed.

Insert picture description here

You can see that several field names in the data we get are repeated, which makes it difficult for mybatis to match, so our focus is to tell mybatis how to match.

First of all, the ResultMap is filled with the name of the ResultMap we have defined below, and then we will focus on how we define this ResultMap.

First, let's look at the ResultMap part first, and then look at the Collection

<resultMap id="selectspuSaleAttrListCheckBySkuMap" type="com.auguigu.gmall.bean.PmsProductSaleAttr" autoMapping="true">
        <result column="sa_id" property="id"></result>
</resultMap>

We first need to define a name for our ResultMap, that is, id, and then we need to define the type of ResultMap, where type is the outermost entity object in our multiple collection, and then we need to define the primary key of the entity object. , Column refers to the field name in the returned data we defined, and property refers to the primary key we defined in the entity class, and the remaining fields can be automatically processed by mybatis through autoMapping=true.

<collection property="pmsProductSaleAttrValueList" ofType="com.auguigu.gmall.bean.PmsProductSaleAttrValue" autoMapping="true">
            <result column="sav_id" property="id"></result>
</collection>

After understanding how the ResultMap is defined, the collection looks relatively simple. Here we only need to look at the properties in the collection. Here are the objects of the next level collection defined in the outermost entity class. Name, as shown in the figure below:

Insert picture description here
The second thing is the type here.It can be seen that the type here is not the same as the ResultMap above.It is still type or oftype, which will more express the concept of multiple collections.

In this way, our ResultMap object is defined.

The code word is not easy. If it is helpful to you, you can follow my official account. Newcomers need your support!
Insert picture description here
Do not click to see, you are also beautiful, click to see, you are
better!

Guess you like

Origin blog.csdn.net/lovely__RR/article/details/110133718