我有一个有两个表的数据库
post:
id
post_name
post_desc
files:
file_id
file_name
post_attachments
post_id
file_id
在我的 xml 映射中我已经有了
<select id="selectPosts" resultType="com.mycom.myproject.bean.postBean">
select id, post_name as postName from post
</select>
所以它是一对多的关系。我想要将所有文件附加到帖子中。我不明白如何在 mybatis 中进行映射。
我有我的 PostBean 就像
public class PostBean extends Post {
private List<FileAttachment> filesAttachment;
//getter setter
}
我使用 mybatis 与 spring 和 mysql 作为数据库
如果您还需要什么,请告诉我。
- 更新 -
我已将我的mapper.xml修改为
<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>
--- 更新2 ---
实际上我也有一个在上面创建的映射表,所以我的查询就像
<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>