mybatis 批量修改
一般來説对于批量修改
在service层对List集合进行for循环,每一条记录调用一次MySQL数据库。但是这样对于数据量大的情况下会大大消耗连接数据库的性能。
- 一开始都是对List 进行 for 操作 这个是大忌讳 ❗❗❗❗
- 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
``
虽然我知道很菜很菜!但我会努力改变自己,踏实的学习知识!登山ing
本文来自博客园,作者:元亓,转载请注明原文链接:https://www.cnblogs.com/Lazy-1bird/p/16450028.html