我在 Java 中使用 MyBatis。我可以在结果映射中使用多个 id 吗?
代码
我的结果 Java 类如下所示:
public class MyClass {
private int id1;
private int id2;
private int total;
// getters & setters
}
目前,我的 MyBatis 结果图是这样的:
<resultMap id="MyResult" type="MyClass">
<result property="id1" column="id1" />
<result property="id2" column="id2" />
<result property="total" column="total" />
</resultMap>
此结果映射用于此查询:
<select id="mySelect" resultMap="MyResult" parameterType="Map">
select
id1,
id2,
sum(total) as total
from
myTable
group by
id1,id2;
</select>
一切正常,但根据 MyBatis 文档,我应该使用 id 来获得一些效率。我想将 MyBatis 结果映射更改为:
<resultMap id="MyResult" type="MyClass">
<id property="id1" column="id1" />
<id property="id2" column="id2" />
<result property="total" column="total" />
</resultMap>
问题
- 它会像以前一样工作吗?
[稍后编辑]:经过测试,它似乎没有搞砸分组和行,所以我会说它像以前一样工作,符合预期。
- 使用 id 会带来 MyBatis 的性能优势吗?
我在哪里看,但找不到答案
- MyBatis documentation - 他们没有说或举例说明类似情况。
- Composite Key - 但是,我没有复合键,我宁愿不修改 MyClass 来创建具有 id1、id2 的伪类 ID。
最佳答案
是的,它会像以前一样工作,并且根据文档,它会提高一些效率,当您处理大量行时,您会很感激。无论如何,我的建议是使用第二个结果图,以便根据您的数据库结构最准确地定义您的结果图。
关于java - MyBatis resultMap 中的多个 id - 性能优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40082017/