ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Mybatis 批量更新

2020-06-05 21:01:48  阅读:274  来源: 互联网

标签:批量 when 更新 item VALUES jdbcType Mybatis relation id


    <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql 后两种写法效率以此降低耗时长 -->
    <update id="updateBatchStock" >
        update oms_good_stock set bindcode=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.bindcode}
        </foreach>,
        innum=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.innum}
        </foreach>,
        sunit=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.sunit}
        </foreach>,
        pname=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.pname}
        </foreach>,
        pspec=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.pspec}
        </foreach>,
        pstyle=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.pstyle}
        </foreach>,
        proddate=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.proddate}
        </foreach>,
        ratio=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.ratio}
        </foreach>,
        goname=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.goname}
        </foreach>,
        whname=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.whname}
        </foreach>,
        expidate=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.expidate}
        </foreach>,
        create_by=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.createBy}
        </foreach>,
        create_time=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.createTime}
        </foreach>,
        update_by=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.updateBy}
        </foreach>,
        update_time=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.updateTime}
        </foreach>,
        is_push_wms=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.isPushWms}
        </foreach>,
        gocode=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.gocode}
        </foreach>,
        is_delete=
        <foreach collection="list" index="index"  separator=" " open="case id"  close="end" item="item">
            when #{item.id} then #{item.isDelete}
        </foreach>
        where id in
        <foreach collection="list" index="index" separator="," open="(" close=")" item="item">
            #{item.id}
        </foreach>
    </update>
    <!-- 批量更新第二种方法,通过接收传进来的参数list进行循环着组装sql 后两种写法效率以此降低耗时长 -->
    <update id="updateBatch" parameterType="java.util.List" >
        <foreach collection="list"  index="index"  separator=" " item="item">
            update oms_good_stock
            <set >
                <if test="item.bindcode != null  and item.bindcode != ''">
                   bindcode = #{item.bindcode},</if>
                <if test="item.innum != null ">innum = #{item.innum},</if>
                <if test="item.sunit != null  and item.sunit != ''">item.sunit = #{item.sunit},</if>
                <if test="item.pname != null  and item.pname != ''">item.pname = #{item.pname},</if>
                <if test="item.pspec != null  and item.pspec != ''">item.pspec = #{item.pspec},</if>
                <if test="item.pstyle != null  and item.pstyle != ''">item.pstyle = #{item.pstyle},</if>
                <if test="item.ratio != null and item.ratio != ''">item.ratio = #{item.ratio},</if>
                <if test="item.prodDate != null  and item.prodDate != ''">item.prod_date = #{item.prodDate},</if>
                <if test="item.goname != null  and item.goname != ''">item.goname = #{item.goname},</if>
                <if test="item.whname != null and item.whname != ''">item.whname = #{item.whname},</if>
                <if test="item.expiDate != null and item.expiDate != '' ">item.expi_date = #{item.expiDate},</if>
                <if test="item.createBy != null  and item.createBy != ''">item.create_by = #{item.createBy},</if>
                <if test="item.createTime != null ">item.create_time = #{item.createTime},</if>
                <if test="item.updateBy != null  and item.updateBy != ''">item.update_by = #{item.updateBy},</if>
                <if test="item.updateTime != null ">item.update_time = #{item.updateTime},</if>
                <if test="item.isPushWms != null  ">item.isPushWms = #{item.isPushWms},</if>
                <if test="item.isDelete != null  and item.isDelete != ''">item.is_delete = #{item.isDelete},</if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <!-- 批量更新第三种方法,通过 case when语句变相的进行批量更新 -->
    <update id="updateBatch1" parameterType="java.util.List" >
        update standard_relation
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="standard_from_uuid =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.standardFromUuid!=null">
                        when id=#{i.id} then #{i.standardFromUuid}
                    </if>
                </foreach>
            </trim>
            <trim prefix="standard_to_uuid =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.standardToUuid!=null">
                        when id=#{i.id} then #{i.standardToUuid}
                    </if>
                </foreach>
            </trim>
            <trim prefix="gmt_modified =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.gmtModified!=null">
                        when id=#{i.id} then #{i.gmtModified}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" separator="or" item="i" index="index" >
            id=#{i.id}
        </foreach>
    </update>
    <!--批量更新第四种方法,用ON DUPLICATE KEY UPDATE-->
    <insert id="updateBatch2" parameterType="java.util.List">
        insert into standard_relation(id,relation_type, standard_from_uuid,
        standard_to_uuid, relation_score, stat,
        last_process_id, is_deleted, gmt_created,
        gmt_modified,relation_desc)VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id,jdbcType=BIGINT},#{item.relationType,jdbcType=VARCHAR}, #{item.standardFromUuid,jdbcType=VARCHAR},
            #{item.standardToUuid,jdbcType=VARCHAR}, #{item.relationScore,jdbcType=DECIMAL}, #{item.stat,jdbcType=TINYINT},
            #{item.lastProcessId,jdbcType=BIGINT}, #{item.isDeleted,jdbcType=TINYINT}, #{item.gmtCreated,jdbcType=TIMESTAMP},
            #{item.gmtModified,jdbcType=TIMESTAMP},#{item.relationDesc,jdbcType=VARCHAR})
        </foreach>
        ON DUPLICATE KEY UPDATE
        id=VALUES(id),relation_type = VALUES(relation_type),standard_from_uuid = VALUES(standard_from_uuid),standard_to_uuid = VALUES(standard_to_uuid),
        relation_score = VALUES(relation_score),stat = VALUES(stat),last_process_id = VALUES(last_process_id),
        is_deleted = VALUES(is_deleted),gmt_created = VALUES(gmt_created),
        gmt_modified = VALUES(gmt_modified),relation_desc = VALUES(relation_desc)
    </insert>

 

标签:批量,when,更新,item,VALUES,jdbcType,Mybatis,relation,id
来源: https://www.cnblogs.com/wf-zhang/p/13052296.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有