【MyBatis】---- 动态SQL

本文深入解析MyBatis框架中的动态SQL元素,包括if、choose、when、otherwise、trim、where、set和foreach,通过具体示例展示了如何在SQL语句中灵活使用这些元素进行条件判断和循环操作,有效提升SQL的灵活性和可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 概述

元素作用备注
if判断语句单条件分支判断
choose(when,otherwise)相当于java的switch和case语句多条件分支判断
trim(where,set)辅助元素,用于处理特定的SQL拼装问题,比如去掉多余的and、or等用于处理SQL拼装的问题
foreach循环语句在in语句等列举条件常用

1. if元素

相当于Java中的if语句,常常与test属性联合使用
场景例子:

<!--根据角色名称(roleName)查找角色, 角色名称是一个选填条件-->
    <select id="findRoles" parameterType="string" resultMap="roleResultMap">
        select role_no,role_name,note from t_role where 1 = 1
        <if test="roleName != null and roleName != ''">
            and role_name like concat('%', #{roleName}, '%')
        </if>
    </select>

2. choose、when、otherwise元素

相当于Java中的switch…case…default功能语句

<select id="findRoles" parameterType="role" resultMap="roleResultMap">
        select role_no,role_name,note from t_role where 1 = 1
        <choose>
            <when test="roleNo != null and roleNo != ''">
                AND role_no = #{roleNo}
            </when>
            <when test="roleName != null and roleName != ''">
                AND role_name like concat('%', #{roleName}, '%')
            </when>
            <otherwise>
                AND note is not null
            </otherwise>
        </choose>
    </select>

3. trim、where、set元素

3.1 使用where元素,去掉了条件1=1

<select id="findRoles" parameterType="role" resultMap="roleResultMap">
        select role_no,role_name,note from t_role
        <where>
            <if test="roleName != null and roleName != ''">
                and role_name like concat('%', #{roleName}, '%')
            </if>
            <if test="note != null and note !=''">
                and note like concat('%', #{note}, '%')
            </if>
        </where>
    </select>

3.2 使用trim元素 去掉常见的and or

<!--prefix:代表语句的前缀;   prefixOverrides:代表需要去掉哪种字符串-->
    <select id="findRoles" parameterType="string" resultMap="roleResultMap">
        select role_no,role_name,note from t_role
        <trim prefix="where" prefixOverrides="and">
            <if test="roleName != null and roleName !=''">
                and role_name like concat('%', #{roleName}, '%')
            </if>
        </trim>
    </select>

3.3 使用set

<!--set元素遇到了逗号,它会吧对应的逗号去掉-->
    <select id="findRoles" parameterType="role">
        update t_role
        <set>
            <if test="roleName != null and roleName != ''">
                role_name = #{roleName},
            </if>
            <if test="note != null and note != ''">
                note = #{note}
            </if>
        </set>
        where role_no = #{roleNo}
    </select>

4. foreach元素

是一个循环语句,作用是遍历集合,能够很好的支持数组和List、Set接口的集合,往往用于SQL中的in关键字

例子: List < String > 的角色编号的集合roleNoList, 可以使用foreach元素找到集合中角色的详细信息

<select id="findUserBySex" resultType="user">
        select * from t_role where role_no in
        <foreach collection="roleNoList" index="index" item="roleNo" open="(" separator="," close=")">
            #{roleNo}
        </foreach>
</select>
  • collection配置的roleNoList是传递进来的参数名称,可以是一个数组、List、Set等集合
  • item配置的是循环中当前的元素
  • index配置的是当前元素在集合的位置下标
  • open和close配置的是以什么符号将这些集合元素包装起来
  • separator是各个元素的间隔符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值