简体   繁体   中英

Multiple ids in MyBatis resultMap - performance benefits?

I am using MyBatis with Java. Can I use multiple ids in the result map?

Code

My Result Java class looks like this:

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

    // getters & setters
}

Currently, my MyBatis result map looks like this:

<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. I want to change the MyBatis result map to:

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

Where did I look, but could not find an answer

  1. MyBatis documentation - they do not say or give example of a similar situation.
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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