pgsql配合<foreach>批量插入,更新

foreach 简单了解

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item集合中每一个元素进行迭代时的别名,
index表示在迭代过程中,每次迭代到的位置,
open该语句以什么开始,
separator在每行数据之间以什么符号作为分隔符,有 union、union all、 逗号
close以什么结束
在使用foreach的时候最关键的也是最容易出错的就是collection属性,
该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,

1、批量插入

批量插入例子:假如传入的list只有两条数据

<insert id="insertNaRulePara" parameterType="ArrayList" >
	   insert into NA_RULE_PARA(
	              RULE_ID,
	              PARA_ID,
	              PARA_TYPE_ID
	   		)
	   		VALUES
	    <foreach collection="list" item="item" index="index" separator="," >
            (
                #{item.ruleId},
                #{item.paraId},
                1
            )
        </foreach>
	</insert>
	

separator="," 这里以逗号作为分隔符,

注意,不同数据库适应的分隔符不同。比如:postgresql 不适应 union,nuion all 分隔符。

后台实际执行的sql如下,同时插入两条(如果list多条,values后面继续拼接上数据)

insert into NA_RULE_PARA( RULE_ID, PARA_ID, PARA_TYPE_ID ) VALUES ( 4, 2022, 1 ) , ( 5, 2023, 1 ) 

2、批量更新

注意,如果不加where id in(1,2) ,case id 是默认去遍历表的所有id。加了之后只从where中的id拿,所有一定要加where id in(1,2), 否则数据表中除了1,2 的其他行数据,该字段被设置为null,如果该列不能为null,则报错

1)更新单个列

update user 
set phone = case id
when 1 then '13128387651' 
when 2 then '13128387652'
end
where id in(1,2)

含义:
当id=1时, phone=‘13128387651’ ;
当id=2时, phone=‘13128387652’

2)更新多个列

update user 
set phone = case id
when 1 then '13128387651' 
when 2 then '13128387652'
end,
name= case id
when 1 then '陈维' 
when 2 then '黄日'
end
where id in(1,2)

3)实战例子,根据id更新单列

该函数需传入ArrayList
在这里插入图片描述

 <update id="updateBasInfoW" parameterType="ArrayList">
        update bas_info
        set user_mod_w = case id
        <foreach collection="list" item="item" index="index" separator=" " >
            when #{item.id} then #{item.num}
        </foreach>
        end
        <where>
            id in
            <foreach collection="list"  item="item" open="(" separator="," close=")" >
                #{item.id}
            </foreach>
        </where>
    </update>

对应sql

update bas_info 
set user_mod_m = case id
when 1447762 then 5 
when 1447773 then 4
end
where id in(1447762,1447773)

4)实战例子,根据id更新多列

<update id="updateOltInfo" parameterType="ArrayList">
        update bas_info
        set user_mod_w = case id
        <foreach collection="list" item="item" index="index" separator=" " >
            when #{item.id} then #{item.num}
        </foreach>
        end,
        user_mod_m= case id
        <foreach collection="list" item="item" index="index" separator=" " >
            when #{item.id} then #{item.numM}
        </foreach>
        end
        where id in
        <foreach collection="list"  item="item" open="(" separator="," close=")" >
            #{item.id}
        </foreach>
        
    </update>

对应sql

update bas_info 
set user_mod_m = case id
when 1447762 then 5 
when 1447773 then 4
end,
user_mod_m = case id
when 1447762 then 109
when 1447773 then 110
end
where id in(1447762,1447773)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值