gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:08:12 24 4
gpt4 key购买 nike

我很累。我想用 mybatis faramework 代替直接使用 sql。我想选择具有填充属性映射的帐户列表。

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

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

Account 的 Mapper 接口(interface)很明显,映射文件包含选择

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

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

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

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

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

基本上它可以工作,但是对于 200 个帐户大约需要 2 分钟,这是 Not Acceptable 。

我希望将 resultMapcollection 一起使用会加快速度。但问题是如何去做吧。 resultMap="account" 应该是什么样子

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

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

最佳答案

如果你使用下面的resultmap

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

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

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

关于java - 映射集合 ('select *' ) 到 MyBatis 中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7913343/

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