• Mybatis 批量修改数据,,并判断非空数据插入


    方法一:

    <update id="updateListPO">       
        <foreach collection="list" separator=";" item="item">
            UPDATE project_quotation_item
            <SET>
            	product_num = #{item.productNum},
            	product_price_total = #{item.productPriceTotal},
           		product_price_wttax = #{item.productPriceWttax},
            	certificate = #{item.certificate},
            	deliver_date = #{item.deliverDate},
            	product_brand = #{item.productBrand},
            	producer = #{item.producer},
            	exp = #{item.exp},
            	is_deleted = #{item.isDeleted},
            	remark = #{item.remark},
            	substitute_type = #{item.substituteType},
            	<if test='null != item.quotationStatus and "" != item.quotationStatus and "null" != item.quotationStatus'>
                    quotation_status = #{item.quotationStatus},
            	</if>
            </set>
            WHERE
            id = #{item.id}
        </foreach>
    </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    方法二:(优化后的)

    <update id="updateListPO">
            update project_quotation_item
            <set>
                <trim prefix="product_num = case" suffix="end,">
                    <foreach collection="list" item="item">
                    	<if test='null != item.productNum and "" != item.productNum and "null" != item.productNum'>
                        	when id=#{item.id} then #{item.productNum}
                        </if>
                    </foreach>
                </trim>
                <trim prefix="product_price_total = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.productPriceTotal}
                    </foreach>
                </trim>
                <trim prefix="product_price_wttax = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.productPriceWttax}
                    </foreach>
                </trim>
                <trim prefix="certificate = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.certificate}
                    </foreach>
                </trim>
                <trim prefix="deliver_date = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.deliverDate}
                    </foreach>
                </trim>
                <trim prefix="product_brand = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.productBrand}
                    </foreach>
                </trim>
                <trim prefix="producer = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.producer}
                    </foreach>
                </trim>
                <trim prefix="exp = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.exp}
                    </foreach>
                </trim>
                <trim prefix="is_deleted = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.isDeleted}
                    </foreach>
                </trim>
                <trim prefix="remark = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.remark}
                    </foreach>
                </trim>
                <trim prefix="substitute_type = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.substituteType}
                    </foreach>
                </trim>
                <trim prefix="quotation_status = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id=#{item.id} then #{item.quotationStatus}
                    </foreach>
                </trim>
            </set>
            <where>
                 id in
                <foreach collection="list" separator="," item="item" open="(" close=")">
                    #{item.id}
                </foreach>
            </where>
        </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    方法三:

    <update id="updateBatch">
    	update 
    		T t
    	<trim prefix="set" suffixOverrides=",">
    	  <trim prefix="t.updateTime = case" suffix="end,">
    		 <foreach collection="list" item="item" index="index">
    			 <if test="item.updateTime != null">
    			  	when t.id = #{item.id} then #{item.updateTime}
    			 </if>
    		 </foreach>
    	  </trim>
    	  <trim prefix="t.updateBy = case" suffix="end,">
    		 <foreach collection="list" item="item" index="index">
    			 <if test="item.updateBy != null">
    			  	when t.id = #{item.id} then #{item.updateBy}
    			 </if>
    		 </foreach>
    	  </trim>
    	  <trim prefix="t.remark = case" suffix="end,">
    		 <foreach collection="list" item="item" index="index">
    			 <if test="item.remark != null">
    			  	when t.id = #{item.id} then #{item.remark}
    			 </if>
    		 </foreach>
    	  </trim>
    	 </trim>
    	where
    	<foreach collection="list" item="item" index="index" open="t.id in (" separator="," close=")">
    		#{item.id}
    	</foreach>
    </update>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    越跌越买!192亿大举抄底
    [oeasy]python0022_ python虚拟机_反编译_cpu架构_二进制字节码_汇编语言
    zookper安装和群起脚本
    72-Java的选择排序、二分查找、Lambda表达式
    HCIP(第十四天)
    linux下使用crontab定时器,并且设置定时不执行的情况,附:项目启动遇到的一些问题和命令
    【数据结构】链表经典oj
    全国职业技能大赛云计算--高职组赛题卷②(私有云)
    Xuperchain竞赛环境安装与工作环境搭建
    常见的一些威胁情报分析平台
  • 原文地址:https://blog.csdn.net/qq_49641620/article/details/133386998