5

このクラスがあるとしましょう:


    Class A {
        int id;
        int[] b;
        // Other properties
    }

    Class B {
        int id;
        // Other properties
    }

クラス A はクラス B と 1 対多の関係にあります。私は既に B オブジェクトをキャッシュして ID で返すサービスを持っています。

テーブルスキーマは次のようになります


    Table a:
    -------
      int id,
      prop1,
      etc

    Table a_to_b_map
    ----------------
      int a_id,
      int b_id

では、これを iBatis でどのようにマッピングすればよいでしょうか。

B オブジェクトは既にキャッシュされているため、ID のリストを A オブジェクトに取得し、サービスを使用して As を強化したいと考えています。

誰かがそれを行う方法を提案できますか?

私が考えることができる2つの可能な代替案は次のとおりです。

  1. A (AtoB マップ) に内部クラスを作成し、iBatis 構成で選択クエリを使用してこれを設定します。
  2. iBatis resultMap/select 内で、別の select を使用して B id のリストを取得します (config でこれを行う方法についてはよくわかりません)。
4

2 に答える 2

1

mybatis3では少し違います。これを行うには、2つのselectステートメントを指定するか、joinを使用してコレクションタグを使用してresultMapを作成します。

<resultMap id=”blogResult” type=”Blog”&gt;
   <collection property="posts" javaType=”ArrayList” column="blog_id"
      ofType="Post" select=”selectPostsForBlog”/>
</resultMap>

<select id=”selectBlog” parameterType=”int” resultMap=”blogResult”&gt;
    SELECT * FROM BLOG WHERE ID = #{id}
    </select>
<select id=”selectPostsForBlog” parameterType=”int” resultType="Author">
    SELECT * FROM POST WHERE BLOG_ID = #{id}
    </select>

または、joinを使用できます

<select id="selectBlog" parameterType="int" resultMap="blogResult">
select
    B.id as blog_id,
    B.title as blog_title,
    B.author_id as blog_author_id,
    P.id as post_id,
    P.subject as post_subject,
    P.body as post_body,
from Blog B
    left outer join Post P on B.id = P.blog_id
where B.id = #{id}
</select>

結果マップを実行します

<resultMap id="blogResult" type="Blog">
  <id property=”id” column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
  </collection>
</resultMap>

ここでibatisユーザーガイドから完全なtotorialを入手できます:

http://svn.apache.org/repos/asf/ibatis/java/ibatis-3/trunk/doc/en/iBATIS-3-User-Guide.pdf

于 2010-10-20T13:46:34.437 に答える
0

あなたの質問を正しく理解できたかどうかわかりません。

A の id に基づいてクエリを実行すると仮定すると、2 つのテーブルを結合するクエリを ibatis で作成するのはどうでしょうか。

select * 
from a, a_to_b_map 
where a.id = #id# and a.id = a_to_b_map.a_id

次に、「queryForMap」を使用して、a_id vs (クエリからのレコードのコレクション) のハッシュマップを返すことができます。カスタム メソッドを使用して、このデータ構造を「A」のオブジェクトに変換します

于 2009-02-04T07:26:55.387 に答える