2011-10-27 8 views
1

私はスタックしています。私はmybatisのためにSQLの直接使用を置き換えたいfaramework。 塗りつぶしたプロパティマップを持つ勘定の一覧を選択したいと思います。 MyBatisのフィールドにコレクション( 'select *')をマッピングします

しかし、アカウントの開始、最初のAccountクラス

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

マッパーインタフェースから起動することができます明らかであるとマッピングファイルを選択

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

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

最初の選択に戻りますが、アカウント

オブジェクト、第二java.utilのが含まれています。マップは 列名/ のペアを含んでいます。

私は、各アカウントオブジェクトのプロパティでマップ含んでいるので、私は、アカウントのリストを反復処理するとid

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

でそのプロパティを選択し、基本的に、それは動作しますが、200個のアカウントのそれは約2分かかりますことを希望それは受け入れられません。

resultMapcollectionを使用するとスピードアップすることを願っています。しかし、問題はどのようにそれを行うには です。どのようにresultMap="account"この場合

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

のように見えますが、アカウントオブジェクトは、任意のプロパティが含まれていません選択しなければなりません。 大きな質問は、プロパティをアカウントオブジェクトに関連付ける方法です。

答えて

3

あなたがそれぞれに続いて、次のresultmapに

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

を使用している場合はMyBatisのアカウントは、パラメータとして、列IDの値を渡しgetAccountPropertiesステートメントを実行していますが、selectタグでそれを受け入れることを許可する必要があります。

<select id="getAccountProperties" resultClass="java.util.Map" parameterClass="java.lang.Integer" > 
    select * from properties where id=#value# 
</select> 
関連する問題