Detailed explanation of the difference between resulttype and resultMap

Table of contents

1. Different objects

1. resultMap

2. resultType

3. Analysis

2. Different descriptions

1、resultMap

2、resulTtype

3. Different types of applications


1. Different objects

1. resultMap

If the column names of the query results are inconsistent with the entity attributes, define a resultMap to create a mapping relationship between the column names and the pojo attribute names (sample code is as follows).

<!--结果返回类型采用resultMap定义-->
<select id="findCardById" parameterType="int" resultMap="findCard">
    select * from card where id=#{id}
</select>

<!--对上述resultMap进行自定义映射-->
<resultMap id="findCard" type="card">
    <id property="id" column="id"/>
    <result property="number" column="number"/>
</resultMap>

2. resultType

resultType uses resultType for output mapping. Only when the column name displayed in the query result is consistent with the attribute name of the entity, the column can be mapped successfully.

<!--结果返回类型采用resultType定义-->
<select id="findCardById" parameterType="int" resultType="card">
    select * from card where id=#{id}
</select>

3. Analysis

As can be seen from the above example code, both resultType and resultMap can be implemented for the mapping of the same class.

  1. For resultMap, in addition to being declared in the <select> tag, <resultMap></resultMap> needs to be used separately to implement custom mapping between entity attributes and database table column names, which is suitable for multi-table queries .
  2. For resultType, you only need to use the resultType attribute in the <select> tag to declare the result return type, which is suitable for single-table queries .

2. Different descriptions

1、resultMap

The way to handle a one-to-one table connection is usually to add entities of another table B nested in the entities of the main table A, and then use the <association> element in mapper.xml to process the connection to another table B, where The value of select in the <association> element is the unique identifier of the SQL statement corresponding to table B, which is generally the namespace + the ID of the SQL statement.

In the following example, the person entity and the card entity have a one-to-one relationship. The query requirement is: query the user's id, name, age, sex and card number based on the id value in the person table. However, only number corresponds to it in the person table. The id value of the card table, so resultMap needs to be used.

1.1 Cad class

package com.chen.pojo;
public class Card {
    private int id;
    private String number;
    //toString方法、set以及get方法省略
}

1.2 Cad class corresponding database table

 1.3 Cad class corresponding mapping file

<mapper namespace="com.chen.mapper.CardMapper" >
    <select id="findCardById" parameterType="int"   resultMap="findCard">
        select * from card where id=#{id}
    </select>
    <resultMap id="findCard" type="card">
        <id property="id" column="id"/>
        <result property="number" column="number"/>
    </resultMap>
</mapper>

1.4 Person class

package com.chen.pojo;
public class Person {
    private int id;
    private String name;
    private int age;
    private String sex;
    //toString方法、set以及get方法省略
}

 1.5 Person class should be used as database table

1.6 Person class corresponding mapping file

<select id="findPersonById"
            parameterType="Integer"
            resultMap="CardWithPerson">
        select * from person where id=#{id}
    </select>
    <!--自定义结果集映射,对上述resultMap进行映射处理-->
    <resultMap id="CardWithPerson" type="person">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <!--使用association实现一对一映射
        property为实体属性
        javaType为该实体属性的类型
        select指向SQL语句值为对应语句的唯一标识,由于该参数来自上一条语句的及如果,
        所以值为即namespace+id
        column为所需属性对应在库表中的列名
        -->
        <association property="cardnumber" javaType="Card" column="card_id"
            select="com.chen.mapper.CardMapper.findCardById"/>
    </resultMap>

2、resulTtype

resultType cannot query the result and map it to the pojo attribute of the pojo object. Choose whether to use resultType or resultMap according to the need to query and traverse the structure set. Suitable for single table queries.

3. Different types of applications

1. Resultmap: When querying select mapping in mybatis, the return type can be resultType or resultMap. However, when using resultMap, you need to perform custom mapping processing on the resultMap, that is, use the <resultMap> element to define the mapping.

2. resulttype: resultType directly represents the return type, while resultMap is a reference to an external ResultMap, but resultType and resultMap cannot exist at the same time.
 

Guess you like

Origin blog.csdn.net/qq_26893841/article/details/127883346