简体   繁体   English

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

[英]Multiple ids in MyBatis resultMap - performance benefits?

I am using MyBatis with Java. 我正在使用MyBatis和Java。 Can I use multiple ids in the result map? 我可以在结果图中使用多个ID吗?

Code

My Result Java class looks like this: 我的结果Java类看起来像这样:

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

    // getters & setters
}

Currently, my MyBatis result map looks like this: 目前,我的MyBatis结果图如下所示:

<resultMap id="MyResult" type="MyClass">
    <result property="id1" column="id1" />
    <result property="id2" column="id2" />
    <result property="total" column="total" />
</resultMap>

This result map is used in this query: 此查询中使用此结果映射:

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

Everything works fine, but according to MyBatis documentation I should use id to gain some efficiency. 一切正常,但根据MyBatis文档,我应该使用id来获得一些效率。 I want to change the MyBatis result map to: 我想将MyBatis结果映射更改为:

<resultMap id="MyResult" type="MyClass">
    <id property="id1" column="id1" />
    <id property="id2" column="id2" />
    <result property="total" column="total" />
</resultMap>

Questions 问题

  1. Will it work the same as before? 它会像以前一样工作吗?

[Later Edit]: tested, it seems that it does not screw up the groupings and the rows, so I would say that it works as before and as expected. [稍后编辑]:测试过,似乎它没有搞砸分组和行,所以我会说它像以前一样工作并且如预期的那样。

  1. Will it bring the MyBatis performance benefits by using id? 它会通过使用id带来MyBatis性能优势吗?

Where did I look, but could not find an answer 我在哪里看,但找不到答案

  1. MyBatis documentation - they do not say or give example of a similar situation. MyBatis文档 - 他们没有说或给出类似情况的例子。
  2. Composite Key - however, I do not have a composite key, and I would rather not modify MyClass to create a pseudoclass ID that has id1, id2. 复合键 - 但是,我没有复合键,我宁愿不修改MyClass来创建一个id1,id2的伪类ID。

Yes, it will work the same as before, and according to documentation, it will gain some efficiency, that you will appreciate when you work with massive rows. 是的,它将像以前一样工作,并且根据文档,它将获得一些效率,当您处理大量行时,您将会欣赏。 Anyway, my recommendation is to use the second result map, in order to be the most accurate in the definition of your resultmap according to your database estructure. 无论如何,我的建议是使用第二个结果图,以便根据您的数据库结构在结果图的定义中最准确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM