Mybatis 动态SQL的用法

17 篇文章 12 订阅

1.动态SQL(  if  , choose , where ,set , foreach   )

 (1)   if

<select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
      SELECT * FROM BLOG WHERE state = 'ACTIVE'
       <if test="title != null">
              AND title like #{title}
       </if>
       <if test="author != null and author.name != null">
              AND title like #{author.name}
       </if>
</select>

 (2)  choose  :相当于 java中的 switch

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。

当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。

类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

<select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
       SELECT * FROM BLOG WHERE state = 'ACTIVE'
     <choose>
        <when test="title != null">
             AND title like #{title}
        </when>
        <when test="author != null and author.name != null">
             AND title like #{author.name}    
        </when>
        <otherwise>
             AND featured = 1;
        </otherwise>
     </choose>
</select>

(3)  where   : 消除 前置 and 或 or

<select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
       SELECT * FROM BLOG 
     <where>
        <if test="state != null">
             state = #{state}
        </if>
        <if test="title != null">
             AND title like #{title}    
        </if>
        <if test="author != null and author.name != null">
             AND title like #{author.name}
        </if>
     </where>
</select>

 (4)  set   : 动态包含更新的列,而不包含不需要更新的

MyBatis在生成update语句时若使用if标签,如果前面的if没有执行,则可能导致有多余逗号的错误。

使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。 
没有使用if标签时,如果有一个参数为null,都会导致错误 

<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
   update Author
   <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
   </set>
   where id={id}
</update>

  (4)  foreach  :迭代集合

 

<select id="selectPostIn" resultType="domain.blog.Post">
     SELECT *
     FROM POST P
     WHERE ID in
     <foreach item="item" index="index" collection="list"  open="("  separator=","  close=")">
                  #{item}
     </foreach>
</select>

   

foreach标签主要用于构建in条件,他可以在sql中对集合进行迭代。如下:

  

<delete id="deleteBatch"> 

    delete from user where id in

    <foreach collection="array" item="id" index="index" open="(" close=")" separator=",">

      #{id}

    </foreach>

  </delete>

  我们假如说参数为----  int[] ids = {1,2,3,4,5}  ----那么打印之后的SQL如下:

  delete form user where id in (1,2,3,4,5)

  释义:

    collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array

    item : 表示在迭代过程中每一个元素的别名

    index :表示在迭代过程中每次迭代到的位置(下标)

    open :前缀,表示该语句以什么开始

    close :后缀,表示以什么结束

    separator :分隔符,表示迭代时每个元素之间以什么分隔

我们通常可以将之用到批量删除、添加等操作中。

 

foreach标签也可以实现实现批量插入(删除)数据:

<insert id="addEmps">
        INSERT INTO tb1_emplyee(last_name,email,gender,d_id)
        VALUES 
        <foreach collection="emps" item="emp" separator=",">
            (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
        </foreach>
</insert>

对应的接口:

  public void addEmps(@Param("emps")List<Employee> emps);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一彡十

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值