2012-10-26 95 views
3

我需要使用mybatis映射爲另一個集合中的對象設置集合。Mybatis嵌套集合無法正確使用列前綴

它適用於我w/o使用columnPrefix,但我需要它,因爲有很多repeenable列。

 <resultMap id="ParentMap" type="org.example.mybatis.Parent"> 
     <id column="Id" jdbcType="VARCHAR" property="id" /> 
     <result column="Name" jdbcType="VARCHAR" property="name" /> 
     <result column="SurName" jdbcType="VARCHAR" property="surName" /> 

     <collection property="childs" 
     javaType="ArrayList" ofType="org.example.mybatis.Child" 
     resultMap="ChildMap" columnPrefix="c_"/>  
    </resultMap> 

<resultMap id="ChildMap" type="org.example.mybatis.Parent"> 
    <id column="Id" jdbcType="VARCHAR" property="id" /> 
    <result column="ParentId" jdbcType="VARCHAR" property="parentId" /> 
    <result column="Name" jdbcType="VARCHAR" property="name" /> 
    <result column="SurName" jdbcType="VARCHAR" property="surName" /> 
    <result column="Age" jdbcType="INTEGER" property="age" /> 

    <collection property="toys" 
     javaType="ArrayList" ofType="org.example.mybatis.Toy" 
     resultMap="ToyMap" columnPrefix="t_"/> 
</resultMap> 

<resultMap id="ToyMap" type="org.example.mybatis.Toy"> 
    <id column="Id" jdbcType="VARCHAR" property="id" /> 
    <result column="ChildId" jdbcType="VARCHAR" property="childId" /> 
    <result column="Name" jdbcType="VARCHAR" property="name" /> 
    <result column="Color" jdbcType="VARCHAR" property="color" /> 
</resultMap> 

<sql id="Parent_Column_List"> 
    p.Id, p.Name, p.SurName, 
</sql> 

<sql id="Child_Column_List"> 
    c.Id as c_Id, c.ParentId as c_ParentId, c.Name as c_Name, c.SurName as c_Surname, c.Age as c_Age, 
</sql> 

<sql id="Toy_Column_List"> 
    t.Id as t_Id, t.Name as t_Name, t.Color as t_Color 
</sql> 

<select id="getParent" parameterType="java.lang.String" resultMap="ParentMap" > 
    select 
    <include refid="Parent_Column_List"/> 
    <include refid="Child_Column_List" /> 
    <include refid="Toy_Column_List" /> 
    from Parent p 

    left outer join Child c on p.Id = c.ParentId 
    left outer join Toy t on c.Id = t.ChildId 
    where p.id = #{id,jdbcType=VARCHAR} 

隨着columnPrefix一切工作正常,但嵌套的玩具集合爲空。 對數據庫的sql查詢正常工作,並且所有玩具都已加入。

可能是我錯過了什麼,或者這是與mybatis的錯誤?

+0

不知道它是否有關,但爲什麼所有的列都是「」而不是? – cporte

+0

小錯,不相關 –

+0

不錯的問題與我同樣的問題,我以我的方式找到了這個解決方案。請檢查 –

回答

6

我有和你一樣的問題。 返回null的原因是ChildMap有一個列前綴'c_',ToyMap的前綴是't_',但是ChildMap通過collection屬性包含了ToyMap。所以,實際上在這種情況下,Toy表中的所有列都必須有一個前綴'c_t_'。

<sql id="Toy_Column_List"> 
    t.Id as c_t_Id, t.Name as c_t_Name, t.Color as c_t_Color 
</sql> 
0

這是我的工作示例

<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 
      } 
      ] 
     } 
     ] 
    } 
    ]