mybatis-动态sql

动态sql的定义

定义:动态SQL就是指根据不同的条件生成不同的SQL语句

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach
    :::success
    实际运用的场景:
    在用户查询中,我们经常会遇到多查询的方式,比如说我们查询用户,查询用户可以用用户名、年龄、邮箱等等去查询或者是去筛选用户。但是我们匹配的用户不是每个条件都会满足的,这时候我们可以使用动态sql,把我们满足条件的语句拼接到where 后面,这样我们就可以实现动态查询了。
    :::

if标签

//如果传入的值为null,或只有title或只有author,会产生不同的结果
<select id="queryBlogIF" parameterType="map" resultType="Blog" resultMap="blog">
    select * from mybatis.blog where 1=1
    <if test="title!=null">
        and title=#{title}
    </if>
    <if test="author!=null">
        and author =#{author}
    </if>
  <if test="price!=null">
        and price =#{price}
    </if>
</select>

上面的例子就是我们查询书籍,我们可以根据书名、作者、价格查询。以上符合其一或以上的都会查询出来。

if:根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到SQL语句中。

where标签

<select id="findActiveBlogLike"
     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 author_name like #{author.name}
    </if>
  </where>
</select>

where:

  • 当where标签里面有内容的时候,会自动生成where关键字,并且将内容前多余的or或and删除。
  • 当where没有内容的时候,此时where标签没有任何效果
  • 注意:where标签不能将其中内容后面后面多余的and或or删除。

trim标签

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

3,trim:

  • prefix|suffix:将trim标签中内容前面或后面添加指定内容
  • suffixoverrides|prefixOverrides:将trim标签中内容前面或后面去掉指定内容

choose、when、otherwise

相当于if…else… if…else

<select id="findActiveBlogLike"
     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 author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

假如第一个第一个when成立以后,后面的when便不会去执行。

foreach

动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)。比如:

<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>

示例:
Mapper接口

int deleteMoreByArray(@Param("eids") Integer [] eids);

mapper.xml

<select id="deleteMoreByArray" >
  SELECT *
  FROM t_emp
  WHERE eid in
  <foreach  collection="eids" item="eid"
       separator="," ">
        #{eid}
  </foreach>
</select>

Test
![image.png](https://img-blog.csdnimg.cn/img_convert/91039c888c21acc25444f52086106a2a.png#averageHue=#efebdc&clientId=u8a97b148-f559-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=190&id=ue964c489&margin=[object Object]&name=image.png&originHeight=238&originWidth=983&originalType=binary&ratio=1&rotation=0&showTitle=false&size=151600&status=done&style=none&taskId=u40363624-c4fe-4e7b-9df1-8ae8bcca059&title=&width=786.4)

用集合插入数据
Mapper接口
![image.png](https://img-blog.csdnimg.cn/img_convert/f8a2ac4d4e26d0b92d2f8d09aa060138.png#averageHue=#fdfcf9&clientId=u8a97b148-f559-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=178&id=uffaba559&margin=[object Object]&name=image.png&originHeight=223&originWidth=633&originalType=binary&ratio=1&rotation=0&showTitle=false&size=46996&status=done&style=none&taskId=u5145e410-0bd0-46b8-ab3a-be72bc483e7&title=&width=506.4)
mapper.xml
![image.png](https://img-blog.csdnimg.cn/img_convert/d758c48be92131f6d7f3dcd63b9390f4.png#averageHue=#f0e7bf&clientId=u8a97b148-f559-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=199&id=u5f0bc6fb&margin=[object Object]&name=image.png&originHeight=249&originWidth=1067&originalType=binary&ratio=1&rotation=0&showTitle=false&size=159578&status=done&style=none&taskId=u36ea5504-ef6a-4626-bc1b-35edf9cf82f&title=&width=853.6)
Test
![image.png](https://img-blog.csdnimg.cn/img_convert/c017c5e336b474cb219af45b55945d7c.png#averageHue=#f4f4e6&clientId=u8a97b148-f559-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=266&id=u625f5e0d&margin=[object Object]&name=image.png&originHeight=332&originWidth=1134&originalType=binary&ratio=1&rotation=0&showTitle=false&size=270933&status=done&style=none&taskId=u2388cd35-9435-4c9e-aaa0-cba268ffdc7&title=&width=907.2)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

没有梦想的java菜鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值