mybatis实现一条SQL批量保存和更新

关键是mapper.xml的SQL语句映射配置

批量保存(一条SQL)

批量插入的SQL语句:

insert into table (aa,bb,cc) 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 wd_solr
        (fayu_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.fayuId},#{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)

 <!--
    批量更新  一条SQL搞定
    updateInBatchAndSelectiveById-->
    <update id="updateInBatchAndSelectiveById" parameterType="list">
        update orders
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="user_id =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=" number =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>

一条记录update一次,性能比较差,容易造成阻塞。

MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。

UPDATE course
    SET name = CASE id 
        WHEN 1 THEN 'name1'
        WHEN 2 THEN 'name2'
        WHEN 3 THEN 'name3'
    END, 
    title = CASE id 
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
    END
WHERE id IN (1,2,3)
    原文作者:Nisus_Liu
    原文地址: https://www.jianshu.com/p/424c150813d2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞