批量插入
批量插入mysql和oracle的sql会有些差别,要注意!
mysql:1
2
3
4
5
6
7
8<insert id="batchInsert" parameterType="java.util.List">
insert into user(username, password) values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.username},
#{item.password} )
</foreach>
</insert>
oracle:1
2
3
4
5
6
7
8
9 <insert id="batchInsert" parameterType="java.util.List">
insert into user(username, password)
<foreach close=")" collection="list" item="item" index="index" open="(" separator="union">
select
#{item.username,jdbcType=VARCHAR},
#{item.password,jdbcType=VARCHAR}
from dual
</foreach>
</insert>
下面的是Oracle使用 Sequence来生成主键的批量插入写法
1 | <insert id="batchInsert" parameterType="java.util.List"> |
批量删除
1 | <delete id="batchDeleteByIdList" parameterType="java.util.List"> |
当然,也可以使用Array来替代List,修改parameterType=“array”和collection=”array”即可