美文网首页
MyBatis:动态 SQL

MyBatis:动态 SQL

作者: hemiao3000 | 来源:发表于2021-10-27 17:01 被阅读0次

1. 动态 SQL

简而言之,动态 SQL 就是在 Mapper 中使用分支、循环等逻辑。常见的动态 SQL 元素包括:

  • if 元素
  • choose-when-otherwise 元素
  • where 元素
  • set 元素
  • foreach 元素

if 元素

if 元素是我们最常见的元素判断语句,相当于 Java 中的 if 语句。它的 test 属性是它的必要属性。

<select id="select" parameterType="int" resultType="Department">
    SELECT * FROM dept
    <if test="deptno != null">
        WHERE deptno = #{deptno}
    </if>
</select>

choose-when-otherwise 元素

MyBatis 并未提供类似 if-else 元素来处理分支情况,if 元素可出现多次,但它们是并列的判断,而非互斥的判断。

choose-when-otherwise 元素类似于 Java 中的 switch-case,用于处理多个条件间的互斥判断。

<select id="selectBySallary" resultType="Employee">
    SELECT * FROM emp
    <choose>
        <when test="min != null and max != null">
            WHERE sal >= #{min} AND sal <= #{max}
        </when>
        <when test="min != null">
            WHERE sal >= #{min} 
        </when>
        <when test="max != null">
            WHERE sal <= #{max}
        </when>
        <otherwise></otherwise>
    </choose>
</select>

where 元素

如果我们强行规定,上述 choose-when-otherwise 所实现的功能必须使用 if 实现,那么将会写成如下形式:

<select id="selectBySallary" resultType="Employee">
    SELECT * FROM emp
    WHERE 1 = 1
    <if test="min != null">
        AND sal >= #{min}
    </if>
    <if test="max != null">
        AND sal <= #{max}
    </if>
</select>

注意体会上面 WHERE 1 = 1 的位置及其作用。

由于判断条件有可能有,也可能没有,所有在 if 元素中,WHERE 关键字出现的地方就有些“尴尬”。WHERE 1 = 1 就是此问题的 非典型 解决方案。

MyBatis 提供了 Where 元素以解决上述尴尬问题。

<select id="selectBySallary" resultType="Employee">
    SELECT * FROM emp
    <where>
        <if test="min != null">
            AND sal >= #{min}
        </if>
        <if test="max != null">
            AND sal <= #{max}
        </if>
    </where>
</select>

set 元素

类似于 where 的元素,set 元素对应于 SQL 语句中的 SET 子句。它专用于 update 语句,用于包含所需更新的列。

set 元素常常和 if 元素联合使用。因为在“选择性更新”功能中,有一个最后一个逗号问题。

注意,<small>更新行为务必要保证更新至少一个属性,否则 MyBatis 更新语句提示 update 语句错误</small>

<update id="updateByPrimaryKeySelective" parameterType="Department">
    UPDATE dept
    <set>
        <if test="dname != null"> dname = #{dname}, </if>
        <if test="loc != null"> loc = #{loc}, </if>
    </set>
    WHERE deptno = #{deptno}
</update>

foreach 元素

foreach 元素使用不多,但是当需要构建包含 IN 子句的查询时,则必用到。

<select id="selectInDeptnos" resultType="Employee">
    SELECT * FROM emp 
    WHERE deptno IN 
    <foreach collection="list" item="deptno" open="(" separator="," close=")">
        #{deptno}
    </foreach>
</select>

collection 属性表示集合类型,其属性值可以是 list 或 array,对应参数类型为 List 或 数组。

相关文章

  • MyBatis动态SQL

    MyBatis 动态SQL 内容 Mybatis动态SQL在XML中支持的几种标签: if chose trim、...

  • MyBatis核心知识点

    (1)Mybatis动态sql是做什么的?都有哪些动态sql?能简述一下动态sql的执行原理不? Mybatis动...

  • MyBatis 动态SQL(*.xml)

    原文参考MyBatis 动态SQL MyBatis的动态SQL大大减少了拼接SQL语句时候的各种格式问题,这里摘录...

  • Mybatis动态SQL

    MyBatis Mybatis笔记连载上篇连接MyBatis缓存Mybatis笔记连载下篇连接 动态SQL 动态S...

  • MyBatis的动态SQL与日志Log4J、SQL语句构造器

    一、MyBatis动态SQL 动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似...

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • mybatis的xml文件的标签详解

    Mybatis #{}和${}和区别 mybatis获取方法参数 动态SQL

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • JavaEE基础知识学习----MyBatis(四)动态SQL

    MyBatis的动态SQL MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似...

  • IT 每日一结

    mybatis动态sql 动态sql绝对是mybatis排列前几的闪光点之一。传统代码中的sql语句需要经过多个字...

网友评论

      本文标题:MyBatis:动态 SQL

      本文链接:https://www.haomeiwen.com/subject/paspaltx.html