1、批量更新
oracle 和 Mysql的语法有一些,差异,使用时注意
<!--oracle 11g 语法-->
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
update tableName
set yqb = #{item.yqb}
where id = ${item.id}
</foreach>
</update>
<!--mysql语法-->
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update tableName
set yqb = #{item.yqb}
where id = ${item.id}
</foreach>
</update>
2、批量删除
注:oracle数据库in查询数量最多支持1000个,超过会报错;使用需谨慎
<delete id="batchDelete" parameterType="java.util.List">
delete from tableName
where id in (
<foreach collection="list" item="item" index="index" open="" close="" separator=",">
#{item,jdbcType = VARCHAR}
</foreach>
)
</delete>
3、批量新增
注:foreach标签底层使用的递归的方式实现,如果插入数量过多有可能导致栈溢出错误;需谨慎使用
<insert id="batchInsert" parameterType="java.util.List">
insert into tableName(id,name,age)
<foreach collection="list" item="item" index="index" open="" close="" separator="union all">
select #{item.id,jdbcType = VARCHAR},#{item.name,jdbcType = VARCHAR},#{item.age,jdbcType = VARCHAR}
from dual
</foreach>
</insert>