mybatis tag differences in

2020-08-02 07:17发布

问题:

I have done a lot of research and no avail. In Mybatis documentation, it says

id – an ID result; flagging results as ID will help improve overall performance

result – a normal result injected into a field or JavaBean property

If they can both inject into a field, and id has performance improvement, so what stops me from only using id at all? Is this just semantic that I will map the primary key to id tag?

I have tried:

<resultMap id="teacherMap" type="me.funmap.model.Teacher" autoMapping="true">
    <result column="t_id" property="t_id"/>
    <collection property="students" javaType="List" ofType="me.funmap.model.Student" autoMapping="true">
        <result column="s_id" property="s_id"/>
    </collection>
</resultMap>

and

<resultMap id="teacherMap" type="me.funmap.model.Teacher" autoMapping="true">
    <id column="t_id" property="t_id"/>
    <collection property="students" javaType="List" ofType="me.funmap.model.Student" autoMapping="true">
        <id column="s_id" property="s_id"/>
    </collection>
</resultMap>

the statement

<select id="getAllTeacher" resultMap="teacherMap">
    SELECT teacher.t_id, t_name, s_id from teacher INNER JOIN student ON teacher.t_id = student.t_id
</select>

result of statement

       t_id    t_name           s_id

|          1 | teacher      |      38 |
|          1 | teacher      |      39 |
|          1 | teacher      |      40 |
|          1 | teacher      |      41 |
|          1 | teacher      |      42 |
|          1 | teacher      |      43 |

I get the same result using either id or result, so what is the difference between <result> and <id> tag in resultMap?

回答1:

The reason why you get the same result using either <id> or <result> is: when you use <collection> inside <resultMap>, if you declare <id>(s), MyBatis will only use <id>(s) to group rows having the same <id>(s) value, if you don't declare any <id>, it will use all <result>s to group rows having the same <result>(s) value.

If the result is:

       t_id    t_name           s_id

|          1 | An      |      38 |
|          1 | An      |      39 |
|          1 | An      |      40 |
|          2 | An      |      41 |
|          2 | An      |      42 |
|          2 | An      |      43 |

When you declare the <resultMap> as

<resultMap id="teacherMap" type="me.funmap.model.Teacher" autoMapping="true">
    <id column="t_name" property="t_name"/>
    <result column="t_id" property="t_id"/>
    <collection property="students" javaType="List" ofType="me.funmap.model.Student" autoMapping="true">
        <result column="s_id" property="s_id"/>
    </collection>
</resultMap>

You get only one teacher who has six students.

But when you declare the <resultMap> as

<resultMap id="teacherMap" type="me.funmap.model.Teacher" autoMapping="true">
    <result column="t_name" property="t_name"/>
    <result column="t_id" property="t_id"/>
    <collection property="students" javaType="List" ofType="me.funmap.model.Student" autoMapping="true">
        <result column="s_id" property="s_id"/>
    </collection>
</resultMap>

You get two teacher, and both have three students.



标签: mybatis