mybatis ---- 级联查询 一对多 (集合映射)

本文深入解析MyBatis框架中的关联映射技术,包括一对一和一对多关系的处理,通过具体示例展示了如何使用<association>和<collection>元素进行复杂类型的数据映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

关联有嵌套查询和嵌套结果两种方式,本文是按照嵌套结果这种方式来说明的

上一章介绍了多对一的关系,用到了<association></association>,这是一个复杂类型的关联。我们选择一个示例来回顾下,比如:一个博客有一个用户,关联映射就工作于这种结果之上。首先看下,我们在本文中要用到的表结构字段:

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 博客  
  2. blog :  id  title  author_id   
  3.   
  4. 作者  
  5. author:  id  username password email  bio favourite_section  
  6.   
  7. 文章  
  8. post :id  blog_id  author_id  created_on  section  subject  draft  body    
  9.   
  10. 评论  
  11. comment : id   post_id  name  comment    
  12.   
  13. 标签  
  14. T : id name   
我们把一个博客和一个用户关联在一起,就像:

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <select id="selectBlog" parameterType="int" resultMap="blogResult">  
  2.            select   
  3.                b.id as blog_id,  
  4.                b.title as blog_title,  
  5.                b.author_id as blog_author_id  
  6.                a.id as author_id,  
  7.                a.username as author_username,  
  8.                a.password as author_passowrd,  
  9.                a.email as auhtor_email,  
  10.                a.bio as author_bio  
  11.             from  blog b left outer join author a on b.author_id=a.id  
  12.                  where b.id=#{id}  
  13.     </select>  


注意这个联合查询,以及所有结果被唯一而清晰的名字而重命名。这使得映射非常简单。现在我们可以映射这个结果:

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <resultMap id="blogResult" type="Blog">  
  2.      <id property="id" column="blog_id"/>  
  3.       <result property="title" column="blog_title"/>  
  4.         
  5.       <!-- 和一个用户关联,Blog 类里面属性时author,关联的列是原先的blog.author_id-->  
  6.       <association property="author"  column="blog_author_id" javaType="Author"  resultMap="authorResult"/>  
  7. </resultMap>  
  8.   
  9. <resultMap id="authorResult" type="Author">  
  10.           <id property="id" column="author_id"/>  
  11.           <result property="username" column="author_username"/>  
  12.           <result property="password" column="author_password"/>  
  13.           <result property="email" column="author_email"/>  
  14.           <result property="bio" column="author_bio"/>  
  15. </resultMap>  

在上面的例子中,你可以看到博客的作者关联authorResult 结果映射来加载作者实例。  上面的实例中,用了外部的结果映射元素来映射关联。这使得Author结果映射可以重用。然而,你不需要重用它的话,或者你仅仅引用你所有的结果映射到一个单独描述的结果映射中。你可以嵌套结果映射。这里给出使用这种方式的相同示例:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <resultMap id="blogResult" type="Blog">  
  2.      <id property="id" column="blog_id"/>  
  3.       <result property="title" column="blog_title"/>  
  4.         
  5.       <!-- 和一个用户关联,Blog 类里面属性时author,关联的列是原先的blog.author_id-->  
  6.       <association property="author"  column="blog_author_id" javaType="Author" >  
  7.               <id property="id" column="author_id"/>  
  8.               <result property="username" column="author_username"/>  
  9.               <result property="password" column="author_password"/>  
  10.               <result property="email" column="author_email"/>  
  11.               <result property="bio" column="author_bio"/>  
  12.       </association>  
  13. </resultMap>  

上面你已经看到了如何处理有一个类型的关联.但是“有很多个”是怎样的呢?,也就是集合类型,本文的主要工作是来说这个的。


集合

相对于关联来说,集合映射多了一个属性”ofType“.这个属性用来区分 JavaBean(或字段)属性类型和 集合包含的类型来说是很重要的. ,ofType用来表示集合包含类型。
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <collection property="posts"  ofType="Post">  
  2.        <id property="id" column="post_id"/>  
  3.        <result property="subject" column="post_subject"/>  
  4.        <result property="body" column="post_body"/>  
  5.  </collection>  
集合元素的作用和关联几乎是相同的。我们来继续上面的示例,一个博客只有一个作者。但是博客有很多文章,在博客类中,这可以由下面的写法来表示:
[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private List<Post> posts;   
这一次联合博客表和文章表(一个blog_id可以对应很多的文章)SQL如下:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <select id="selectBlog" parameterType="int" resultMap="blogResult">  
  2.          select   
  3.              b.id as blog_id ,  
  4.              b.title as blog_title,  
  5.              b.author_id as blog_author_id,  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值