Mybatis步步进阶(三)——1:N关系配置 && 动态SQL拼接

在hibernate学习中,实体映射关系配置一直都让人比较头疼;一对多、多对一、创建主外键,多的一端负责维护管理外键关系 。。Balabala。。。另外面向对象进行操作的hql语句,当涉及多表主外键关联查询时,hql的书写也是让人比较头疼的。

  Mybatis在这两点上都有自己独到的处理方式,下面以【先实例,后总结】的方式为大家逐一道来。

一、1:N实体关系配置

1、实体关系


一个command指令ID对应多条content实体 

[java]  view plain copy
  1. public class Command {  
  2.       
  3.     private int Id;  
  4.     private String name;  
  5.     private String description;  
  6.     private List<Content> content;  
  7.   
  8. }  
[java]  view plain copy
  1. public class Content {  
  2.     private int Id;  
  3.     private String commandID;  
  4.     private String content;  
  5. }  

2、实体关系配置

[html]  view plain copy
  1. <mapper namespace="Command">  
  2.   
  3.   <resultMap type="cn.max.domain.Command" id="Command">  
  4.     <id column="Id" jdbcType="INTEGER" property="id"/>  
  5.     <result column="name" jdbcType="VARCHAR" property="name"/>  
  6.     <result column="description" jdbcType="VARCHAR" property="description"/>  
  7.     <collection property="content" resultMap="Command"/>         <!-通过<span style="font-family: SimSun;">collection标签表示command中content实体集合属性</span><span style="font-family: SimSun;">-></span>  
  8.   </resultMap>  

[html]  view plain copy
  1. <mapper namespace="Content">  
  2.   
  3. <!-- TYPE message类路径 -->  
  4.   <resultMap type="cn.max.domain.Content" id="MsgResult">  
  5.     <id column="Id" jdbcType="INTEGER" property="id"/>  
  6.     <result column="commandId" jdbcType="VARCHAR" property="commandId"/>  
  7.     <result column="content" jdbcType="VARCHAR" property="content"/>  
  8.   </resultMap>  
  9. </mapper>  

  根据两个实体1:N的实体关系,command实体中自然加入一个List或者set类型content集合。对应command配置文件中,使用[collection]标签配置属性为content即可。

  这样根据实体所有属性进行xml文件配置,并且就一[collection]标签即可表示两者间1:n的关联关系,对比于hibernate各种丰富的尸体关联关系标签,显得简洁明了许多。

二、动态SQL拼接实现表关联查询

  通过上述步骤便配置好了command和content实体的对应关系,接下来就是在command.xml中使用sql标签,通过command的name属性关联查询出对应content集合了。

[html]  view plain copy
  1. <select id="queryCommandList" parameterType="cn.max.domain.Command" resultMap="Command">  
  2.     select a.Id C_ID,a.name,a.description,b.content,b.Id,b.commandId from command  a left join content b on a.Id=b.commandId where 1=1  
  3.       
  4.         <if test="name !=null and !"".equals(name.trim())">   
  5.             and a.name=#{name}  
  6.         </if>  
  7.         <if test="description !=null and !"".equals(command.trim())">  
  8.         <span style="white-space:pre">    </span>and a.description  like '%' #{description} '%'  
  9.     <span style="white-space:pre">    </span></if>      
  10.         
  11. </select>  
  12. </mapper>  

  同样是结合OGNL和EL表达式,传入command实体作为参数,获取command中name和description属性,联合查询。

  标题中动态也就是通过OGNL标签进行判断,传入参数属性是否为空,拼接sql进行查询。这与我们平时拼写组合查询sql类似,更多的也就是考验大家的sql掌握的程度了。

  回到开篇,对比于hibernate封装的实体关系标签和hql语句,mybatis映照实体属性,利用特有的标签对应实体关系进行配置,跟hibernate其实本质上并无区别,但由于标签、标签属性都比较简单容易理解,所以相对也让人容易接受。而查询sql也是我们最熟悉的原生sql,然而应对传递参数的问题,mybatis利用OGNL标签进行判断拼接,熟悉OGNL和mybatis定义的获值方式也是很容易实现的。

  怎么样,四不四很简单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值