这是我为 MyBatis 配置的 XML 配置的一部分。
<sql id="selectElementWithAttributes">
SELECT
e.id as id,
e.title as title,
e.type as type,
e.date as date,
e.ownerid as ownerid,
a.attributeid as attributeid,
a.value as value,
a.type as a_type
FROM
test_element as a
LEFT JOIN
test_elementattributes as a
ON
e.id
= a.parentid
</sql>
现在我使用 resultMap 将列映射到我的对象。
<resultMap type="Element" id="rmElement">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="type" column="type" />
<result property="date" column="date" />
<result property="ownerid" column="ownerid" />
<collection property="attributes" ofType="Attribute">
<id property="{id=parentid,attributeid=attributeid}" />
<result property="key" column="attributeid" />
<result property="value" column="value" />
</collection>
</resultMap>
问题是,我必须在我的 Element 对象中进行集合。两个集合都包含属性。区别在于属性的类型(映射到 a_type)。根据这种类型,我喜欢在集合 1 或集合 2 中使用属性。我玩弄了鉴别器,但并不真正理解我在做什么,它不起作用......
这是想法,但是如何根据 a_type 切换集合?
<resultMap type="Element" id="rmElement">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="type" column="type" />
<result property="date" column="date" />
<result property="ownerid" column="ownerid" />
<collection property="attributes" ofType="Attribute">
<id property="{id=parentid,attributeid=attributeid}" />
<result property="key" column="attributeid" />
<result property="value" column="value" />
</collection>
<collection property="attributesOther" ofType="Attribute">
<id property="{id=parentid,attributeid=attributeid}" />
<result property="key" column="attributeid" />
<result property="value" column="value" />
</collection>
</resultMap>
提前致谢