使用mybatis或ibatis的一对多关系

使用mybatis或ibatis的一对多关系,ibatis,mybatis,Ibatis,Mybatis,我有一个数据库,它有两个表 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">

我有一个数据库,它有两个表

  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中进行映射

我有我的豆荚

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>    
如中所示,您必须编写更复杂的查询并更改映射器

请求将是这样的:

<select id="selectPosts" resultType="com.mycom.myproject.bean.postBean">  
    select id, post_name, file_id, file_name as postName from post left outer join files
</select>
以及您的结果图:

<resultMap id="detailedPostResultMap" type="Blog">
  <result property="id" column="id"/>
  <result property="name" column="post_name"/>
  <collection property=filesAttachment" ofType="FileAttachment">
     <id property="fileId" column="file_id"/>
     <result property="fileName" column="file_name"/>
  </collection>
</resultMap>
如中所示,您必须编写更复杂的查询并更改映射器

请求将是这样的:

<select id="selectPosts" resultType="com.mycom.myproject.bean.postBean">  
    select id, post_name, file_id, file_name as postName from post left outer join files
</select>
以及您的结果图:

<resultMap id="detailedPostResultMap" type="Blog">
  <result property="id" column="id"/>
  <result property="name" column="post_name"/>
  <collection property=filesAttachment" ofType="FileAttachment">
     <id property="fileId" column="file_id"/>
     <result property="fileName" column="file_name"/>
  </collection>
</resultMap>

嗨,cporte,我已经这样做了,但是它返回给我两个post对象的列表,只有不同的文件名,但是我想要一个post对象,它应该有两个或更多文件对象的列表shum,在您的查询中,您有WHERE fa.file\u id=pa.post\u id。也许它应该是fa.file\u id=pa.file\u id no?是的,您是对的,但它返回两行,因为我附加了两个文件。这是否正确。它应该返回两行还是一行?映射程序中的另一个错误my bad:应该替换为,因为名称不是id,我想您应该只有一篇文章,其中包含两个文件。尝试使用更正后的映射器和更正后的请求,也许它会工作,然后它仍然会在每个rowHi cporte中为我提供2行和filesAttachment=null。我已经这样做了,但它会返回两个具有不同文件名的post对象的列表,但我希望一个post对象应该具有2个或更多文件对象的列表shum,在您的查询中,您有WHERE fa.file\u id=pa.post\u id。也许它应该是fa.file\u id=pa.file\u id no?是的,您是对的,但它返回两行,因为我附加了两个文件。这是否正确。它应该返回两行还是一行?映射程序中的另一个错误my bad:应该替换为,因为名称不是id,我想您应该只有一篇文章,其中包含两个文件。尝试使用正确的映射器和正确的请求,也许它会工作,然后仍然会给我2行,每行filesAttachment=null