我正在研究 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。如果有人可以帮助我或将我指向可以提供帮助的来源,我将不胜感激。
提前感谢所有帮助。