mybatis 一条语句批量保存、更新、删除

批量保存(一条SQL)
批量插入的SQL语句:

insert into table_exam (aaa,bbb,ccc) values(xx,xx,xx),(oo,oo,oo)
<!-- 批量插入数据 -->
    <insert id="insertBatch" parameterType="java.util.List"
        useGeneratedKeys="true">
        <selectKey resultType="long" keyProperty="id" order="AFTER">
            SELECT
            LAST_INSERT_ID()
        </selectKey>
        insert into tablename
        (id, tablename,
        name,logo,description,section_no,look_count,favorite_count,create_uid,create_time,update_time,timestamp)
        values
        <foreach collection="list" item="wdSolr" index="index"
            separator=",">
            (
            #{wdSolr.id},#{wdSolr.tablename},#{wdSolr.name},#{wdSolr.logo},
            #{wdSolr.description},#{wdSolr.sectionNo},#{wdSolr.lookCount},#{wdSolr.favoriteCount},
            #{wdSolr.createUid},#{wdSolr.createTime},#{wdSolr.updateTime},#{wdSolr.timestamp}
            )
        </foreach>
    </insert>

  批量更新(一条SQL)

UPDATE table
    SET username = CASE id 
        WHEN 1 THEN 'name_1'
        WHEN 2 THEN 'name_2'
        WHEN 3 THEN 'name_2'
    END, 
    article_title = CASE id 
        WHEN 1 THEN 'article_title 1'
        WHEN 2 THEN 'article_title 2'
        WHEN 3 THEN 'article_title 3'
    END
WHERE id IN (1,2,3)
<!--
    批量更新  一条SQL搞定
    updateInBatchAndSelectiveById-->
    <update id="updateInBatchAndSelectiveById" parameterType="list">
        update orders
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="username = CASE" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.userId!=null">
                        when id=#{i.id} then #{i.userId}
                    </if>
                </foreach>
            </trim>
            <trim prefix=" article_title =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.number!=null">
                        when id=#{i.id} then #{i.number}
                    </if>
                </foreach>
            </trim>

            <trim prefix="createtime =case" suffix="end," >
                <foreach collection="list" item="i" index="index">
                    <if test="i.createtime!=null">
                        when id=#{i.id} then #{i.createtime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="note =case" suffix="end," >
                <foreach collection="list" item="i" index="index">
                    <if test="i.note!=null">
                        when id=#{i.id} then #{i.note}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" separator="or" item="i" index="index" >
            id=#{i.id}
        </foreach>
    </update>
<delete id="deleteCTQ" parameterType="java.lang.String">


DELETE FROM sqm_product_ctq_current where guid in


<foreach item="idItem" collection="array" open="(" separator="," close=")">

#{idItem}


</foreach>


</delete>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值