3

MyBatis 3.0.5 のマッピング機能を調べています。データベースは、組み込みモードで実行中の H2 (1.3.160) です。ユーザーマニュアルの助けを借りて、簡単な部品が機能するようになりました。しかし、バッキング ストレージとしてaSetを使用する a をマッピングするのに苦労しています。HashMap

カスタム セットをフィールドとして持つカスタム コレクションの Java コードを次に示します (簡潔にするために簡略化しています)。

public class CustomCollection 
{
    @JsonProperty
    private CustomSet<CustomItem> customItems;

    public CustomCollection()
    {
        customItems = new CustomSet<CustomItem>();
    }

    // other stuff  
}

これがCustomSetコードです(これも簡略化されています)

public class CustomSet<E extends CustomItemInterface> extends AbstractSet<E>
{
    private ConcurrentHashMap<String, E> items;

    public CustomSet()
    {
        items = new ConcurrentHashMap<String, E>();
    }

    // other stuff  
}

マッピング インターフェイスは次のとおりです。

public interface CustomCollectionMapper 
{
    CustomCollection select(@Param("somename") String s1, @Param("someothername") String s2);
}

これは、Mybatis フレームワークへの呼び出しを行うコードです。

SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) servletContext.getAttribute("SqlSessionFactory");
SqlSession session = sqlSessionFactory.openSession();
CustomCollection coll = null;
try 
{ 
    CustomCollectionMapper mapper = session.getMapper(CustomCollectionMapper.class);
    coll = mapper.select(param1, param2);
} 
finally 
{ 
    session.close(); 
} 

これまでのところ、マッピング XML を考え出すことができました。

<select id="select" resultMap="CustomCollectionMapping">
-- What goes here???
</select>

<resultMap type="com.example.CustomCollection" id="CustomCollectionMapping">
  <association property="customItems" javaType="com.example.customSet">
    <collection property="items" javaType="HashMap" ofType="com.example.CustomItem" select="selectCustomItems">
    </collection>
  </association>
</resultMap>

<select id="selectCustomItems" parameterType="map" resultType="com.example.CustomItem">  
  -- SQL query to return multiple CustomItem rows
</select>  

さまざまな反復を通じて、「結果が多すぎます」エラー、その他のエラー、または何も発生しませんでした (マッパー呼び出しから null が返されました) が、必要な結果は得られませんでした。SQL コードはそれ自体で正常に動作し、簡単な select ステートメントで List を要求すると、行と ArrayList が返されます。私が抱えている問題は、ネストされたコレクション オブジェクトが適切に設定されていることです。

私は何度もマニュアルを読み、例を探しましたが、この目的のための正しいマッピング XML を思いつくことができませんでした。誰かが私を助けてくれるか、助けてくれる情報源を教えてくれれば幸いです。

すべての助けを前もって感謝します。

4

4 に答える 4

3

これは私の実際の例です:

<resultMap id="categoryPreferenceValueMap" type="SyncCategoryPreferenceValueModel">
         <id property="preferenceTypeId" column="preference_type_id" />
         <result property="title" column="title"/>
         <result property="index" column="type_index"/>
          <result property="updatedAt" column="category_updated_at" />
          <collection property="preferences" column="p_preference_id" ofType="SyncPreferenceModel" >
            <id property="preferenceId" column="p_preference_id" />
            <result property="title" column="p_preference_title" />
            <result property="index" column="preference_index" />
            <result property="updatedAt" column="preference_updated_at" />
            <collection property="preferenceValues" column="p_v_preference_value_id"  ofType="SyncPreferenceValueModel"  >
                <id property="preferenceValueId" column="p_v_preference_value_id" />
                <result property="preferenceValue" column="p_v_preference_value" />
                <result property="updatedAt" column="preference_value_updated_at" />
            </collection>  
         </collection>
    </resultMap>

これは私のクエリです

<select id="getPrefersenceWithValues" resultMap="categoryPreferenceValueMap">
    SELECT  
        PT.preference_type_id, PT.title, PT.type_index,PT.updated_at as  category_updated_at,
        P.preference_id as p_preference_id , P.title as p_preference_title  ,P.index as preference_index,

        P.updated_at as preference_updated_at,

        PV.preference_value_id as p_v_preference_value_id ,PV.preference_value as p_v_preference_value  

    FROM preference_types PT
    INNER JOIN preferences P ON PT.preference_type_id=P.preference_type_id 
        INNER JOIN preference_values PV ON P.preference_id=PV.preference_id 

    ORDER BY type_index
</select>

出力は次のとおりです。

 [
    {
      "preferenceTypeId": "1",
      "title": "abc BASICS",
      "index": "1",
      "updatedAt": 1,
      "preferences": [
        {
          "preferenceId": "1",
          "title": "xyz xyz",
          "preferenceTypeId": null,
          "gender": null,
          "index": 1,
          "updatedAt": 1,
          "preferenceValues": [
            {
              "preferenceId": null,
              "preferenceValueId": "2",
              "preferenceValue": "30-60",
              "gender": null,
              "updatedAt": 0
            },
            {
              "preferenceId": null,
              "preferenceValueId": "1",
              "preferenceValue": "0-30",
              "gender": null,
              "updatedAt": 0
            }
          ]
        }
      ]
    }
  ]
于 2015-11-21T11:35:41.080 に答える
0

これでうまくいくように見えResultHandlerますが、XML マッピング ファクトリの利点を残して、大量のコードを書く必要があります。また、返された行ごとにハンドラーが 1 回呼び出されるという事実も気になります。

mybatis-user グループのメッセージで、Mybatisは実際には私が望むことをしないことがわかりました (仲介者を取り除いたとしても、MybatisCustomSetが必要な方法でハッシュマップを作成できるようには見えません)。そのため、そのトピックの OP が行っていることを実行することにしました。リストを取得し、HashMap自分自身にデータを入力します。これにより、マッピングを介してオブジェクトを取得し、少量のコードで必要なものを設定できHashMapます。

于 2011-10-08T18:20:05.577 に答える
0

カスタムResultHandlerを試してみてください。ただし、 CustomSet は機能するはずです。今後も調査していきますので、何かわかりましたら更新します。

于 2011-10-05T15:28:18.697 に答える