<!--批量插入,重复则更新--> <update id="insertAndUpdateOnDutyPerson" parameterType="com.hikvision.js.ispsstatistics.entity.OnDutyPerson"> insert into <include refid="TableName"/> ( <include refid="Base_Column_List"/> ) values <foreach collection="list" item="item" index="index" separator=","> (#{item.id,jdbcType=VARCHAR}, #{item.onDutyDate,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, #{item.idCard,jdbcType=VARCHAR}, #{item.policeCode,jdbcType=VARCHAR}, #{item.orgCode,jdbcType=VARCHAR}, #{item.orgName,jdbcType=VARCHAR}, #{item.phoneNum,jdbcType=VARCHAR}, #{item.updateTime,jdbcType=VARCHAR} ) </foreach> on conflict(on_duty_date,id_card,org_code) do update set ( <include refid="Base_Column_List"/> )= (excluded.id,excluded.on_duty_date,excluded.name,excluded.id_card,excluded.police_code,excluded.org_code,excluded.org_name,excluded.phone_num,excluded.update_time) </update>
这样的查询 do update set只能设置一条数据
插入和更新并不是批量数据一一对应操作的 而是批量insert然后再判断是否更新 所以下面只能写死一条数据 多了就报错..遂百度
发现pgsql在这种情况下会提供一个EXCLUDED临时表把之前插入的值储存起来,然后就可以