MyBatis动态sql详解(一)

 MyBatis动态SQL非常简单,即是根据条件动态的变化sql语句,就不用自己去拼写了,MyBatis中用于实现动态SQL的元素主要有:
  if  
  choose(when,otherwise)  
  trim  
  where  
  set  
  foreach  


 1、if判断语句:

 if判断语句即是当满足条件时,sql将会加上此条语句,比如我想根据传入的user动态的添加判断语句:

 <select id="dynamicIfGetUser" parameterType="com.happyheng.entity.User"
  resultType="com.happyheng.entity.User">
  select * from users where age > 0
  <if test="id!=null">
   and id = #{id}
  </if>
  <if test="name!=null">
   and name = #{name}
  </if>
 </select>

可以看出,我前面已经加上了where语句,那么在后面,只要传入的user的id不为空,就加上id判断语句,只要name不为空,就加上name判断语句,非常方便。

2、where+if判断语句:
如果我想全部在后面加上where的判断,那么只需要写在where嵌套里面即可,MyBatis会智能的把首个and 或 or 给忽略,而后面的and、or都不会忽略。

 <select id="dynamicWhereIfGetUser" parameterType="com.happyheng.entity.User"
  resultType="com.happyheng.entity.User">
  select * from users
  <where>
   <if test="id!=null">
    and id = #{id}
   </if>
   <if test="name!=null">
    and name = #{name}
   </if>
   <if test="age!=null">
    and age = #{age}
   </if>
  </where>
 </select>


比如下面这条语句,如果把name中的改为or,那么当输入的user的id不为空,name也不为空时,那么sql语句为
 select * from users where id = 7 or name = 'happyheng'

 <select id="dynamicWhereIfGetUser" parameterType="com.happyheng.entity.User"
  resultType="com.happyheng.entity.User">
  select * from users
  <where>
   <if test="id!=null">
    and id = #{id}
   </if>
   <if test="name!=null">
   or name = #{name}
   </if>
   <if test="age!=null">
    and age = #{age}
   </if>
  </where>
 </select>

3、choose(when,otherwise)  语句:
choose语句跟java中的switch语句相同,when相当于case,when中一个判断语句,只要判断语句成立,那么就跳出choose语句,如果所有的choose都不成立,那么就执行otherwise中的sql。

为了方便,在前面加上了where 1=1,这样1=1肯定成立,也加上了where:
 <select id="dynamicChooseGetUser" parameterType="com.happyheng.entity.User"
  resultType="com.happyheng.entity.User">
  select * from users where 1=1
  <choose>
   <when test="id!=null">
    and id = #{id}
   </when>
   <when test="name!=null">
    and name = #{name}
   </when>
   <otherwise>
    and name = 'happyheng'
   </otherwise>
  </choose>
 </select>

那么,当传入的id为空,name为zhangsan时,执行的是
  select * from users where 1=1 and name = 'zhangsan'
当传入的id为空,name也为空时,执行的是
   select * from users where 1=1 and name = 'happyheng'

4、trim语句:
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容忽略,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,

 <select id="dynamicTrimGetUser" parameterType="com.happyheng.entity.User"
  resultType="com.happyheng.entity.User">
  select * from users
  <trim prefix="where" prefixOverrides="and|or">
   <if test="id!=null">
    and id = #{id}
   </if>
   <if test="name!=null">
    or name = #{name}
   </if>
   <if test="age!=null">
    and age = #{age}
   </if>
  </trim>
 </select>

这样,当我们传入的id不为空时,那么sql语句即为:
select * from users where id = 7;

  5、set语句:
  set语句主要是在更新时进行使用,其会自动把最后一个逗号给删除,比如我想更新一个user,把user的id为7的name改为libenben,但是其age我不想改,那么就可以这样:
<update id="dynamicSetUser" parameterType="com.happyheng.entity.User">
  update users
  <set>
   <if test="name!=null">
    name = #{name},  
   </if>
   <if test="age!=null">
    age = #{age},   
   </if>
  </set>
  where id = #{id}
 </update>


那么我这样使用即可:
  SqlSession session = MyBatisUtil.getSqlSession(true);
  User user = new User();
  user.setId(7l);
  user.setName("libenben");
  
  List<User> users = session.selectList("dynamicSetUser",user);
  for(User getUser: users){
   System.out.println("结果为"+getUser);
  }
  
  session.close();

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值