MyBatis的动态SQL

    技术2022-07-10  142

    一、标签

    if 标签

    if 标签通常用于 WHERE 语句、UPDATE 语句、INSERT 语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值。

    <if test="name != null and name != ''"> and NAME = #{name} </if>

    choose 标签

    有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis 提供了 choose 元素,按顺序判断 when 中的条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的 sql。类似于 Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

    if 是与(and)的关系,而 choose 是或(or)的关系。

    <select id="getStudentListChoose" parameterType="Student" resultMap="BaseResultMap"> SELECT * from STUDENT WHERE 1=1 <where> <choose> <when test="Name!=null and student!='' "> AND name LIKE CONCAT(CONCAT('%', #{student}),'%') </when> <when test="hobby!= null and hobby!= '' "> AND hobby = #{hobby} </when> <otherwise> AND AGE = 15 </otherwise> </choose> </where> </select>

    trim标签

    trim标记是一个格式化的标记,主要用于拼接sql的条件语句(前缀或后缀的添加或忽略),可以完成set或者是where标记的功能。

    trim属性主要有以下四个

    prefix:在trim标签内sql语句加上前缀suffix:在trim标签内sql语句加上后缀prefixOverrides:指定去除多余的前缀内容,如:prefixOverrides=“AND | OR”,去除trim标签内sql语句多余的前缀"and"或者"or"。suffixOverrides:指定去除多余的后缀内容。

    例如在update中

    <update id="updateByPrimaryKey" parameterType="Object"> update student set <trim suffixOverrides=","> <if test="name != null"> NAME=#{name}, </if> <if test="hobby != null"> HOBBY=#{hobby}, </if> </trim> where id=#{id} </update>

    如果name和hobby的值都不为空的话,会执行如下语句

    update student set NAME='XX',HOBBY='XX' /*,*/ where id='XX'

    会忽略最后一个“,” ;

    在select中

    <select id="selectByNameOrHobby" resultMap="BaseResultMap"> select * from student <trim prefix="WHERE" prefixOverrides="AND | OR"> <if test="name != null and name.length()>0"> AND name=#{name} </if> <if test="hobby != null and hobby.length()>0"> AND hobby=#{hobby} </if> </trim> </select>

    如果name和hobby的值都不为空的话,会执行如下语句

    select*fromuserWHERE /*and*/ name = ‘xx’ and hobby= ‘xx’

    会为片段添加 “WHERE” 前缀,并忽略第一个 “and” ;

    当然,避免出现“WHERE AND”还有其他方法,如下

    <!--将where提取出来,并加上“1=1”的查询条件 --> select * from student where 1=1 <trim suffixOverrides=","> <if test="name != null and name != ''"> and NAME = #{name} </if> <if test="hobby != null and hobby != ''"> and HOBBY = #{hobby} </if> </trim>

    用在insert中

    <insert id="insert" parameterType="Object"> insert into student <trim prefix="(" suffix=")" suffixOverrides=","> <if test="name != null"> NAME, </if> <if test="hobby != null "> HOBBY, </if> </trim> <trim prefix="values(" suffix=")" suffixOverrides=","> <if test="name != null "> #{name}, </if> <if test="hobby != null "> #{hobby}, </if> </trim> </insert>

    可以为生成格式正确的insert语句。

    foreach 标签

    foreach 标签主要用于构建 in 条件,可在 sql 中对集合进行迭代。也常用到批量删除、添加等操作中。

    <!-- in查询所有,不分页 --> <select id="selectIn" resultMap="BaseResultMap"> select name,hobby from student where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>

    属性介绍:

    collection:collection 属性的值有三个分别是 list、array、map 三种,分别对应的参数类型为:List、数组、map 集合。item :表示在迭代过程中每一个元素的别名index :表示在迭代过程中每次迭代到的位置(下标)open :前缀close :后缀separator :分隔符,表示迭代时每个元素之间以什么分隔

    二、格式化输出

    where 标签

    当 if 标签较多时,这样的组合可能会导致错误。 如下:

    <select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap"> SELECT * from STUDENT WHERE <if test="name!=null and name!='' "> NAME LIKE CONCAT(CONCAT('%', #{name}),'%') </if> <if test="hobby!= null and hobby!= '' "> AND hobby = #{hobby} </if> </select>

    当 name 值为 null 时,查询语句会出现 “WHERE AND” 的情况,解决该情况除了将"WHERE"改为“WHERE 1=1”之外,还可以利用 where 标签。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以 AND 或 OR 开头的,则它会剔除掉。

    <select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap"> SELECT * from STUDENT <where> <if test="name!=null and name!='' "> NAME LIKE CONCAT(CONCAT('%', #{name}),'%') </if> <if test="hobby!= null and hobby!= '' "> AND hobby = #{hobby} </if> </where> </select>

    set 标签

    没有使用 if 标签时,如果有一个参数为 null,都会导致错误。当在 update 语句中使用 if 标签时,如果最后的 if 没有执行,则或导致逗号多余错误。使用 set 标签可以将动态的配置 set 关键字,和剔除追加到条件末尾的任何不相关的逗号。

    <update id="updateStudent" parameterType="Object"> UPDATE STUDENT SET NAME = #{name}, MAJOR = #{major}, HOBBY = #{hobby} WHERE ID = #{id}; </update> <update id="updateStudent" parameterType="Object"> UPDATE STUDENT SET <if test="name!=null and name!='' "> NAME = #{name}, </if> <if test="hobby!=null and hobby!='' "> MAJOR = #{major}, </if> <if test="hobby!=null and hobby!='' "> HOBBY = #{hobby} </if> WHERE ID = #{id}; </update>

    使用 set+if 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值。

    <update id="updateStudent" parameterType="Object"> UPDATE STUDENT <set> <if test="name!=null and name!='' "> NAME = #{name}, </if> <if test="hobby!=null and hobby!='' "> MAJOR = #{major}, </if> <if test="hobby!=null and hobby!='' "> HOBBY = #{hobby} </if> </set> WHERE ID = #{id}; </update>
    Processed: 0.010, SQL: 9