Mybatis 动态 SQL

1. if 标签

if 标签有很多应用场景, 例如: 在用户进行注册是有些是必填项有些是选填项, 这就会导致前端传入的参数不固定如果还是将参数写死就很难处理, 这时就可以使用 if 标签进行判断

    <insert id="dynamicAdd">
        insert into userinfo (username,
        password
        <if test="photo != null">
            ,photo
        </if>)
        values (#{username},
        #{password}
        <if test="photo != null">
            ,#{photo}
        </if>)
    </insert>

当传入的参数为 null 时就不会进入 if 标签内, photo 就会为默认值而不是 null.

2. trim 标签

之前的插⼊⽤户功能,只是有⼀个 photo 字段可能是选填项,如果所有字段都是⾮必填项,就考虑使⽤< trim >标签结合< if >标签,对多个字段都采取动态⽣成的⽅式.
< trim >标签中有如下属性:

  • prefix:表示整个语句块,以prefix的值作为前缀
  • suffix:表示整个语句块,以suffix的值作为后缀
  • prefixOverrides:表示整个语句块要去除掉的前缀
  • suffixOverrides:表示整个语句块要去除掉的后缀
<insert id="dynamicAdd2">
        insert into userinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
           <if test="username != null">
               username,
           </if>
            <if test="password != null">
                password,
            </if>
            <if test="photo != null">
                photo,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null">
                username = #{username},
            </if>
            <if test="password != null">
                password = #{password},
            </if>
            <if test="photo != null">
                photo = #{photo},
            </if>
        </trim>
    </insert>

在以上 sql 动态解析时,会将第⼀个 部分做如下处理:

  • 基于 prefix 配置,开始部分加上 (
  • 基于 suffix 配置,结束部分加上 )
  • 多个 组织的语句都以 , 结尾,在最后拼接好的字符串还会以 , 结尾,会基于 suffixOverrides 配置去掉最后⼀个 ,

3. where 标签

传⼊的⽤户对象,根据属性做 where 条件查询,⽤户对象中属性不为 null 的,都为查询条件.如:user.username 为 “a”,则查询条件为 where username=“a”;

 <select id="selectByProm" resultType = "com.example.demo.entity.Userinfo">
        select * from userinfo
        <where>
            <if test="username != null">
                username = #{username}
            </if>
            <if test="password != null">
               and password = #{password}
            </if>
            <if test="photo != null">
                and photo = #{photo}
            </if>
        </where>
    </select>

当第一个条件不成立时 where 标签会自动去掉后续成立条件开头的 and
< where >标签也可以使⽤ < trim prefix=“where” prefixOverrides=“and”> 替换.

4. set 标签

根据传⼊的⽤户对象属性来更新⽤户数据,可以使⽤< set >标签来指定动态内容.

<update id="dynamicUpdate">
        update userinfo
        <set>
            <if test="username != null">
                username = #{username},
            </if>
            <if test="password != null">
                password = #{password},
            </if>
            <if test="photo != null">
                photo = #{photo},
            </if>
        </set>
        where id = #{id}
    </update>

set 标签会自动去掉最后一个 ,
< set >标签也可以使⽤ < trim prefix=“set” suffixOverrides=“,”>代替

5. foreach 标签

对集合进⾏遍历时可以使⽤该标签. < foreach >标签有如下属性:

  • collection:绑定⽅法参数中的集合,如 List,Set,Map或数组对象
  • item:遍历时的每⼀个对象
  • open:语句块开头的字符串
  • close:语句块结束的字符串
  • separator:每次遍历之间间隔的字符串

示例:根据多个用户 id 来删除⽂章数据

<delete id="deleteByIds">
        delete from  userinfo
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值