MyBatis resultMap 中的多个 id - 性能优势?

Cri*_*tea 5 java mybatis

我正在使用 MyBatis 和 Java。我可以在结果映射中使用多个id吗?

代码

我的 Result Java 类如下所示:

public class MyClass {
    private int id1;
    private int id2;
    private int total;

    // getters & setters
}
Run Code Online (Sandbox Code Playgroud)

目前,我的 MyBatis 结果图如下所示:

<resultMap id="MyResult" type="MyClass">
    <result property="id1" column="id1" />
    <result property="id2" column="id2" />
    <result property="total" column="total" />
</resultMap>
Run Code Online (Sandbox Code Playgroud)

此结果映射用于此查询:

<select id="mySelect" resultMap="MyResult" parameterType="Map">
    select
        id1,
        id2,
        sum(total) as total
    from
        myTable
    group by
        id1,id2;
</select>
Run Code Online (Sandbox Code Playgroud)

一切工作正常,但根据 MyBatis 文档,我应该使用id来获得一些效率。我想将 MyBatis 结果映射更改为:

<resultMap id="MyResult" type="MyClass">
    <id property="id1" column="id1" />
    <id property="id2" column="id2" />
    <result property="total" column="total" />
</resultMap>
Run Code Online (Sandbox Code Playgroud)

问题

  1. 它会像以前一样工作吗?

[稍后编辑]:经过测试,它似乎没有搞乱分组和行,所以我想说它像以前一样工作并且符合预期。

  1. 使用id会给MyBatis带来性能上的好处吗?

我在哪里查看,但找不到答案

  1. MyBatis 文档- 他们没有说出或给出类似情况的示例。
  2. 复合键- 但是,我没有复合键,而且我不想修改 MyClass 来创建具有 id1、id2 的伪类 ID。

小智 2

是的,它将像以前一样工作,并且根据文档,它将获得一些效率,当您处理大量行时,您会欣赏到这一点。无论如何,我的建议是使用第二个结果映射,以便根据您的数据库结构最准确地定义结果映射。