mybatis 批量修改

一般來説对于批量修改

在service层对List集合进行for循环,每一条记录调用一次MySQL数据库。但是这样对于数据量大的情况下会大大消耗连接数据库的性能。

  1. 一开始都是对List 进行 for 操作 这个是大忌讳 ❗❗❗❗
  2. MyBatis的xml文件中使用循环,每条语句用 " ;"号隔开,使用事务监控语句执行的情况。但这样会直接报错,因为MyBatis默认不支持批量update语句的,要在连接JDBC的连接信息中加入一下字段:&allowMultiQueries=true。
 <update id="updateAAAAAAAABatch">
        update ord_order_detail
        <set>
            row_state=11,relation_id = id,
            sample_no= case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.sampleNo}
            </foreach>
            end,
            send_box_time = case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.sendBoxTime}
            </foreach>
            end,
            wms_ship_time= case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.wmsShipTime}
            </foreach>
            end,
            dead_time =case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.deadTime}
            </foreach>
            end,
            manufacture_time =case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.manufactureTime}
            </foreach>
            end,
            batch_no = case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.batchNo}
            </foreach>
            end,
            send_box_laboratory= case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.sendBoxLaboratory}
            </foreach>
            end,
            AAAA_express= case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.AAAAExpress}
            </foreach>
            end,
            AAAA_express_no= case id
            <foreach collection="AAAABatchDetailList" item="item" index="index" open="" close="" separator="">
                when #{item.id} then #{item.AAAAExpressNo}
            </foreach>
            end
        </set>
        where id in
        <foreach collection="AAAABatchDetailList" item="item" separator="," open="(" close=")">
            #{item.id}
        </foreach>
        and row_state=10 and object_type = 4
    </update>
    ``
用法 可以了解一下 case  when then end

 ```sql
UPDATE organizational_hierarchy 
    SET
    is_deleted=
        CASE
	is_deleted 
	WHEN '0' THEN
	'NORMAL' 
	WHEN '1' THEN 
	'DELETED'
	end
	``
posted @ 2022-07-06 11:08  元亓  阅读(2196)  评论(0编辑  收藏  举报