3

我正在研究 MyBatis 3.0.5 的映射工具。该数据库是运行嵌入式模式的 H2 (1.3.160)。在用户手册的帮助下,我得到了简单的部分。但是我很难映射Set使用 aHashMap作为后备存储的 a。

这是自定义集合的 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。不过我认为,您的 CustomSet 应该可以工作。我将进一步调查,如果我发现了什么,我会更新。

于 2011-10-05T15:28:18.697 回答
0

看起来ResultHandler可以解决问题,但这让我编写了大量代码,留​​下了 XML 映射工厂的好处。此外,每个返回的行都会调用一次处理程序这一事实让我担心。

在 mybatis-user 组的一条消息中发现,Mybatis 实际上不会做我想做的事情(即使我摆脱了中介CustomSet,看起来 Mybatis 也无法按照我需要的方式填充 hashmap)所以我决定采用该主题中的 OP 正在做的事情:取回一个列表并填充HashMap我自己。这将允许我通过映射获取对象,并且只需少量代码,即可HashMap填充必要的内容。

于 2011-10-08T18:20:05.577 回答
0

我曾经遇到过同样的问题。正如@Andy Pryor 建议的那样,您可以使用ResultHandler. 但正如我所做的那样,只是“拆分”了您的数据。

我的意思是将你的数据移动Map到另一个类中。然后将你的Set与这个新类相关联。

<resultMap type="com.example.CustomCollection" id="CustomCollectionMapping">
    <collection property="items" javaType="java.util.TreeSet"
         ofType="com.example.NewCustomItem" select="selectCustomItems"/>
</resultMap>

请注意,这com.example.NewCustomItem是新课程。你只需要为这个类再写一个映射器就可以了。

但是如果你真的需要Map用的ResultHandler话。

于 2015-11-08T18:30:18.820 回答