java 将集合('select *')映射到 MyBatis 中的字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7913343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:59:20  来源:igfitidea点击:

Mapping collection ('select *') to field in MyBatis

javamybatis

提问by David Warsow

I am stacked. I would like to replace direct use of sql in favor of mybatis faramework. I would like to select list of accounts with filled properties map.

我堆积如山。我想用mybatis faramework代替直接使用 sql 。我想选择带有填充属性映射的帐户列表。

But lets start from the beginning, first Account class

但是让我们从头开始,第一个 Account 类

public class Account {
     private int id;
     ...
     private Map<String, String> properties;
     ...
     //setters / getters
}

Mapper interface for Account is obvious and mapping file contains selects

帐户的映射器接口很明显,映射文件包含选择

<select id="getAccountById" resultMap="account">
       select ... from account where id = #{id}
</select>

<select id="getAccountProperties" resultType=map>
       select * from properties where id=#{id}
</select>

First selection returns Account object, second java.util.Map contains column name/ valuepair.

第一个选择返回 Account 对象,第二个 java.util.Map 包含列名/对。

I would like that each account object contain map with properties, so I iterate over list of accounts and selects its properties by id

我希望每个帐户对象都包含带有属性的映射,因此我遍历帐户列表并按 id 选择其属性

for(Account account : accountList) {
    int id = account.getId();
    Map properites = mapper.getAccountProperties(id);
    account.setProperties(properties);
}

And basically it works, but for 200 accounts it takes about 2 minutes, and it is not acceptable.

而且基本上是可以的,但是200个账号大概需要2分钟,是不行的。

I hope that using resultMapwith collectionwill speed it up. But the question is how to do it. How should resultMap="account"looks like

我希望使用resultMapwithcollection会加快速度。但问题是如何去做。应该怎么resultMap="account"

<resultMap id="account" type="Account">
   <id property="id" column="id">
   ...
   <collection property="properties" javaType="map" column="id" select="getAccountProperties" />
</resultMap>

In this case selected account object does not contain any properties. The big question is: How to associate properties with account object?

在这种情况下,选定的帐户对象不包含任何属性。最大的问题是:如何将属性与帐户对象相关联?

回答by Andrea Colleoni

If you use the following resultmap

如果您使用以下结果图

<resultMap id="account" type="Account">
    <result property="id" column="id">
    <result property="properties" column="id" select="getAccountProperties" />
</resultMap>

Then for each account MyBatis executes the getAccountProperties statement passing the value of the column id as a parameter, but you must allow to accept it in the select tag:

然后对于每个帐户 MyBatis 执行 getAccountProperties 语句,将列 id 的值作为参数传递,但您必须允许在 select 标签中接受它:

<select id="getAccountProperties" resultClass="java.util.Map" parameterClass="java.lang.Integer" >
    select * from properties where id=#value#
</select>