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.
But lets start from the beginning, first Account class
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 / value pair.
I would like that each account object contain map with properties, so I iterate over list of accounts and selects its properties by 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.
I hope that using resultMap with collection will speed it up. But the question is how
to do it. How should resultMap="account" looks like
<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?
If you use the following 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: