Mybatis动态sql

目录

if

where

trim

choose、when、otherwise

foreach

特殊符号处理

使用转义字符

使用来<![CDATA[]]>包裹特殊字符


动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

if

在查询时,我们可以将条件动态传入sql语句中,将if关键字当作一个标签处理

XML 复制代码
<select id="find" resultType="Student">
        select * from Student where id>0
            <if test="num!=0">
                and num = #{num}
            </if>
</select>

如果if的条件不成立,就会返回所有id>0的数据

where

对于查询条件个数不确定的情况,可使用元素。如下

XML 复制代码
<select id="id" parameterType="" resultType="">
        SELECT xxx... FROM table t
    <where>
        <if test="name != null &amp; name!='' ">
            name like #{name}
        </if>
        <if test="age!=null &amp; age>0">
            AND age> #{value}
        </if>
    </where>
</select>

元素会进行判断,如果它包含的标签中有返回值的话,它就插入一个 'where'。 此外,如果标签返回的内容是以 AND 或 OR 开头,它会剔除掉 AND 或 OR。

trim

where 标签,其实用 trim 也可以表示,当 WHERE 后紧随 AND 或则 OR 的 时候,就去除 AND 或者 OR。

XML 复制代码
<select id="find" resultType="Student">
        select * from Student
        <trim prefix="where" prefixOverrides="and">
            <if test="num!=0">
                and num = #{num}
            </if>
            <if test="name!=null">
                and name = #{name}
            </if>
        </trim>
</select>

prefix="where" 有条件成立时,添加where关键字

prefixOverrides="and" 覆盖and关键字

choose、when、otherwise

有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

XML 复制代码
<select id="findActiveBlogLike" resultType="Blog">
  SELECT * FROM student WHERE id>0
  <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>

foreach

对集合进行遍历主要用在构建 in 条件中,它可以在 SQL 语句中进行迭代一个集合。foreach 元素的属性主要有 item,index,collection,open,separator,close。

item:表示集合中每一个元素进行迭代时的别名

index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置

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

separator:表示在每次进行迭代之间以什么符号作为分隔符

close:表示以什么结束

foreach可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

XML 复制代码
<select id="selectPostIn" resultType="Student">
  SELECT * FROM student
  <where>
    <foreach item="item" index="index" collection="list"
        open="ID in (" separator="," close=")" nullable="true">
          #{item}
    </foreach>
  </where>
</select>

特殊符号处理

使用转义字符

|----|---------|
| < | &lt; |
| > | &gt; |
| " | &quto; |
| ' | &apos |
| & | &amp |

使用来<![CDATA[]]>包裹特殊字符

XML 复制代码
<if test="id != null">
    AND <![CDATA[ id <> #{id} ]]>
</if>
相关推荐
考虑考虑24 分钟前
JDK25模块导入声明
java·后端·java ee
_小马快跑_2 小时前
Java 的 8 大基本数据类型:为何是不可或缺的设计?
java
Re_zero4 小时前
线上日志被清空?这段仅10行的 IO 代码里竟然藏着3个毒瘤
java·后端
洋洋技术笔记4 小时前
Spring Boot条件注解详解
java·spring boot
程序员清风1 天前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林5511 天前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊1 天前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing1 天前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠2 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840822 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot