MyBatis中的动态SQL(sql标签、where标签、set标签、批量增加与批量删除)

 

目录

sql标签

​编辑

where标签

set标签

foreach标签

批量增加

批量删除


将基础SQL语句中重复性高的增加它的复用性,使得sql语句的灵活性更强

sql标签<sql>

<sql id="text">
        select * from user
    </sql>
    <select id="selectAll" resultType="user">
        <include refid="text"></include>
    </select>

where标签

注意事项:where会自动忽略前后缀比如and与or

<select id="selectByIdAndPwd" resultType="user">
        <include refid="text"></include>
         <where>
             <if test="id!=null">
                 id=#{id}
             </if>
             <if test="pwd!=null">
                 and password=#{pwd}
             </if>
         </where>
​
    </select>

set标签

注意事项:set中满足条件的if会自动忽略后缀,比如“,”

<update id="updateUser" parameterType="user">
        update user
        <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="password!=null">
                password=#{password}
            </if>
        </set>
         where id=#{id}
    </update>

foreach标签

批量增加
<!--collection传入的容器类型;open以什么为开始,close以什么为结束,separator分割
    item传入的容器中的元素-->
<insert id="addForeach" parameterType="user">
        insert into user(name,password,sex)  values
        <foreach collection="list" item="user" separator=",">
           (#{user.name},#{user.password},#{user.sex})
        </foreach>
    </insert>

批量删除
<delete id="delForeach" parameterType="int">
        delete from user where id in
        <foreach collection="array" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </delete>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值