gpt4 book ai didi

java - MyBatis:在一次查询中通过注释进行收集

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:20 25 4
gpt4 key购买 nike

我有一个 xml 映射器 - 一个选择和一个结果映射器。它工作没有问题。但我想使用注释。我的映射器:

  <resultMap id="readItemsRM" type="com.company.Item">
<id property="id" column="Id"/>
<result property="comment" column="Comment"/>
<collection property="textItems" ofType="com.company.TextItem">
<id property="id" column="TxId"/>
<result property="name" column="TxName"/>
</collection>
</resultMap>

所以我喜欢这个

   @Results({
@Result(id=true, property="id", column="Id"),
@Result(property="comment", column="Comment"),
///,???
})
public List<Item> select();

我不明白如何在不执行更多 sql 查询的情况下通过注释映射我的集合。正如我发现的所有示例一样,假设执行了一个查询。请帮忙。

最佳答案

AFAIK,如果您使用带注释的映射,则不能使用 JOIN

来自doc ,关于@Many的用法,

A mapping to a collection property of a complex type. Attributes: select, which is the fully qualified name of a mapped statement (i.e. mapper method) that can load a collection of instances of the appropriate types, fetchType, which supersedes the global configuration parameter lazyLoadingEnabled for this mapping. NOTE You will notice that join mapping is not supported via the Annotations API. This is due to the limitation in Java Annotations that does not allow for circular references.

如果愿意,您可以直接使用带注释的 ResultMap:

@Select("SELECT QUERY")
@ResultMap("readItemsRM")
public List<Item> select();

关于java - MyBatis:在一次查询中通过注释进行收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27201782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com