eine bis viele Beziehung mit Mybatis oder Ibatis
Ich habe eine Datenbank mit zwei Tabellen
post:
id
post_name
post_desc
files:
file_id
file_name
post_attachments
post_id
file_id
In meinem XML-Mapping habe ich schon
<select id="selectPosts" resultType="com.mycom.myproject.bean.postBean">
select id, post_name as postName from post
</select>
Es ist also eine Eins-zu-Viele-Beziehung. Ich möchte, dass alle Dateien an einen Beitrag angehängt werden. Ich verstehe nicht, wie ich das Mapping in mybatis machen kann.
Ich habe mein PostBean gern
public class PostBean extends Post {
private List<FileAttachment> filesAttachment;
//getter setter
}
Ich benutze mybatis mit spring und mysql als Datenbank. Bitte lassen Sie mich wissen, wenn Sie etwas anderes benötigen.
--aktualisieren--
Ich habe meine mapper.xml als geändert
<resultMap id="BaseResultMap" type="com.mycom.myproject.db.mybatis.model.post">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Sep 11 10:14:08 BST 2012.
-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="post_name" jdbcType="VARCHAR" property="postName" />
<collection property="filesAttachment" ofType="com.mycom.myproject.db.mybatis.model.FileAttachment">
<id property="fileId" column="file_id" />
<id property="fileName" column="file_name" />
</collection>
--- update2 ---
Tatsächlich habe ich auch eine Zuordnungstabelle, die ich oben erstellt habe, damit meine Abfrage wie folgt aussieht
<select id="selectPosts" parameterType="string" resultType="com.mycom.myproject.bean.postBean">
select p.id, p.post_name, fa.file_id as fileId, fa.file_name as fileName from post p
left outer join files f on f.post_id = p.id join post_attachments pa on pa.file_id = f.post_id
where p.id = 371
</select>