多条数据,需要条件筛选之后插入到数据表:
<insert id="insertExpectCardLabelInfo" parameterType="java.util.List">
//插入表字段
INSERT INTO expect_know_label (
expect_know_label_id,
user_id,
expect_know_label_data_id,
del_flag,
create_time,
create_user,
update_time,
update_user)
//满足条件的数据表 重命名
select
expect_know_label_id,
user_id,
expect_know_label_data_id,
del_flag,
create_time,
create_user,
update_time,
update_user
from (
//满足条件的数据,传入集合bean,遍历数据
<foreach collection="list" item="item" index="index" separator="UNION ALL">
select
#{item.expectId:VARCHAR} as expect_know_label_id,
#{item.userId:VARCHAR} as user_id,
#{item.expectDataId:VARCHAR} as expect_know_label_data_id,
0 as del_flag,
now() as create_time,
#{item.userId:VARCHAR} as create_user,
now() as update_time,
#{item.userId:VARCHAR} as update_user
from dual
</foreach>
) A
//根据需求筛选出需要的数据
where A.user_id=#{userId:VARCHAR} and A.del_flag=0
and A.expect_know_label_data_id not in
(select expect_know_label_data_id from expect_know_label where user_id=#{userId:VARCHAR} and del_flag=0)
</insert>
//数据插入成功。
如何只是需要插入数据,而不对数据进行筛选的,可以直接进行<foreach>遍历,例如
select
expect_know_label_id,
user_id,
expect_know_label_data_id,
del_flag,
create_time,
create_user,
update_time,
update_user
<foreach collection="list" item="item" index="index" separator="UNION ALL">
#{item.expectId:VARCHAR},
#{item.userId:VARCHAR},
#{item.expectDataId:VARCHAR},
0,
now(),
#{item.userId:VARCHAR},
now(),
#{item.userId:VARCHAR}
</foreach>
/////
原文地址:https://www.cnblogs.com/mustanglqt/p/10594211.html