MyBatis:通过一个查询中的注释进行收集 [英] MyBatis:collection via annotation in one query

查看:339
本文介绍了MyBatis:通过一个查询中的注释进行收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an xml mapper - one select and one result mapper. It works without problems. But I want to use annotations. My mapper:

  <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查询的情况下通过注释映射我的集合。正如我发现的所有示例都假设执行了一个查询。请帮助。

I can't understand how can I map my collection via annotation without executing one more sql query. As all the examples I found suppose executing one more query. Please help.

推荐答案

AFAIK,如果你不能使用 JOIN s正在使用带注释的映射。

AFAIK, you cannot use JOINs if you are using mapping with annotations.

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


到复杂类型的集合属性的映射。属性:
select,它是映射语句的完全限定名称(即
映射器方法),可以加载
适当类型的实例集合fetchType,它取代全局
配置参数lazyLoadingEnabled用于此映射。 注意
您会注意到Annotations
API不支持连接映射。这是因为Java Annotations中的限制不是
允许循环引用。

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

You can directly use your ResultMap with annotations if you like:

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

这篇关于MyBatis:通过一个查询中的注释进行收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆