动态SQL

if标签

  通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件

<select id="getUserByName" parameterType="string" resultType="User">
     select * from account where 1=1
     <if test="_parameter!=null and _parameter!=''">
         and name like CONCAT('%',#{_parameter},'%');
     </if>
</select>

choose标签

  它无法实现if....else、if....else.....的逻辑,

  要想实现这样的逻辑,就需要使用到choose when otherwise标签。

  choose元素中包含when和otherwise两个标签,一个choose中至少包含一个when,有0个或1个otherwise。

  (只进一个when,当所有的when不满足时,将走otherwise)

<select id="getUserByNameOrId" resultType="User">
     select * from account where 1=1
     <choose>
         <when test="name!=null and name!=''">
             and name=#{name}
         </when>
         <when test="id!=-1">
             and id=#{id}
         </when>
         <otherwise>
             and money=1000
         </otherwise>
     </choose>
</select>

where标签

  如果该标签包含的元素有返回值,就插入一个where;如果where后面的字符串是以and和or开头,就将它剔除。

<select id="getUserByNameOrId" resultType="User">
     select * from account
    <!--使用了where标签,无需手动编写where条件-->
    <where>
         <choose>
             <when test="name!=null and name!=''">
                 and name=#{name}
             </when>
             <when test="id!=-1">
                 and id=#{id}
             </when>
             <otherwise>
                 and money=1000
             </otherwise>
         </choose>
    </where>
</select>

set标签

  如果该标签包含的元素中有返回值,就插入一个set;如果set后面的字符串是以逗号结尾的,就把这个逗号剔除。

  (经常用于修改)

<update id="updateUser" parameterType="User">
 <!--如果参数类型是一个对象,那么sql语句中#{对象的属性名}-->
    update account
   <!--使用<set>标签替代set语句-->
   <set>
       <if test="name!=null and name !=''">
           name=#{name},
       </if>
       <if test="money!=0">
           money=#{money},
       </if>
   </set>
where id=#{id}
</update>

注意点:

  本案例在set标签中存在值时没有问题,但如果都不满足其中的if条件。

  Set标签中没有返回值的情况下 语句 将变成 update account where id=#{id},所以使用时,仍需小心

trim标签

  Where和set标签的功能都可以使用trim标签来实现

Where标签对应trim的用法如下

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

Set标签对应trim的用法如下

<trim prefix="SET" suffixOverrides=",">

</trim>

trim标签有如下属性:

  prefix:当trim元素中包含内容时,会给内容增加prefix指定的前缀

  prefixOverrides:当trim元素中包含内容时,会把内容中匹配的前缀去掉

  suffix:当trim元素中包含内容时,会给内容增加suffix指定的后缀

  suffixOverrides:当trim元素中包含内容时,会把内容中匹配的后缀去掉

foreach标签

  可以生成一系列的值,这个标签主要用于SQL的in语句后面

  foreach元素的属性主要有 item,index,collection,open,separator,close。

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

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

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

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

  close:表示以什么结束

<select id="findUserByIds" resultType="User">
    select * from account
    <!--此处必须使用list 不能使用参数名-->
    <if test="list.size>0">
        where id IN
        <foreach collection="list" open="(" close=")" separator="," item="item">
            #{item}<!--item此时表示集合中的元素-->
        </foreach>
    </if>
</select>
<select id="findUserByIds" resultType="User">
    select * from account
    <!--此处必须使用list 不能使用参数名-->
    <if test="list.size>0">
        where id IN
        <foreach collection="list" open="(" close=")" separator="," item="item">
            #{item.id}<!--item此时表示集合中的元素User对象-->
        </foreach>
    </if>
</select>
<insert id="addUserList">
    insert into account values
    <foreach collection="list" item="user" separator=",">
        (DEFAULT,#{user.name},#{user.money})
    </foreach>
</insert>
原文地址:https://www.cnblogs.com/whtt/p/11651965.html