Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ids in MyBatis resultMap - performance benefits?

Tags:

java

mybatis

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.
like image 357
Cristian Ciobotea Avatar asked Oct 30 '22 18:10

Cristian Ciobotea


1 Answers

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.

like image 104
Jota Ge Avatar answered Nov 15 '22 06:11

Jota Ge