這裏是對象模型,我建議:
class Member {
Integer id;
String name;
List<ClubRegistration> clubRegistrations;
}
class Club {
Integer id;
String name;
List<MemberRegistration> memberRegistrations;
}
class ClubRegistration {
Club club;
Date date;
}
class MemberRegistration {
Member member;
Date date;
}
所以,你可以得到在這兩個方面的數據:成員有俱樂部或與其會員俱樂部。
SQL查詢:
SELECT m.id AS memberId, m.name AS memberName, c.id AS clubId, c.name AS clubName, register_Date
FROM member m
INNER JOIN member_club mc ON m.id=mc.member_id
INNER JOIN club c ON mc.club_id=c.id
WHERE {what you want}
的ResultMap爲你問問情況:
<resultMap id="memberRM" type="Member">
<id column="memberId" property="id"/>
<result column="memberName" property="name"/>
<collection property="clubRegistrations" ofType="ClubRegistration">
<result column="register_Date" property="date" />
<association property="club" javaType="Club">
<id column="clubId" property="id"/>
<result column="clubName" property="name"/>
</association>
</collection>
</resultMap>
只是交換會員/俱樂部另一種情況。
你應該那麼即使引用的結果映射,而不是窩的定義:<association property="club" resultMap="clubRM" />
雖然我還沒有檢查它自己,the circular reference issue that would arise is managed bay Mybatis。
Woww ..我認爲你的解決方案是輝煌的。真的很感謝你的建議! –
只是更新的答案走得更遠。讓我知道它是否運作良好。 – blackwizard