【问题标题】:mybatis <id> <result> tag differences in <resultMap>mybatis <id> <result> 标签在 <resultMap> 中的区别
【发布时间】:2018-02-11 08:04:12
【问题描述】:

我做了很多研究,但无济于事。在 Mybatis 文档中,它说

id – 一个 ID 结果;将结果标记为 ID 将有助于提高整体性能

result - 注入到字段或 JavaBean 属性中的正常结果

如果它们都可以注入到一个字段中,并且 id 具有性能改进,那么是什么阻止我完全不使用 id 呢?这只是我将主键映射到 id 标签的语义吗?

我试过了:

<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>

<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>

声明

<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>

陈述结果

       t_id    t_name           s_id

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

我使用 id 或 result 得到相同的结果,那么 resultMap 中的&lt;result&gt;&lt;id&gt; 标签有什么区别?

【问题讨论】:

    标签: mybatis


    【解决方案1】:

    使用&lt;id&gt;&lt;result&gt; 得到相同结果的原因是:当你在&lt;resultMap&gt; 中使用&lt;collection&gt; 时,如果你声明&lt;id&gt;(s),MyBatis 将只使用&lt;id&gt; (s) 对具有相同&lt;id&gt;(s) 值的行进行分组,如果您没有声明任何&lt;id&gt;,它将使用所有&lt;result&gt;s 对具有相同&lt;result&gt;(s) 值的行进行分组。

    如果结果是:

           t_id    t_name           s_id
    
    |          1 | An      |      38 |
    |          1 | An      |      39 |
    |          1 | An      |      40 |
    |          2 | An      |      41 |
    |          2 | An      |      42 |
    |          2 | An      |      43 |
    

    当您将&lt;resultMap&gt; 声明为

    <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>
    

    只有一位老师有六个学生。

    但是当你将&lt;resultMap&gt; 声明为

    <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>
    

    你有两个老师,两个都有三个学生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2016-07-23
      • 2018-12-07
      相关资源
      最近更新 更多