mybatis foreach批量插入数据:Oracle与MySQL不同点:
- 主要不同点在于foreach标签内separator属性的设置问题:
- separator设置为","分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) ,(v4,v5,v6) ,...
- separator设置为"union all"分割时,最终拼接的代码形式为:insert into table_name (a,b,c) values (v1,v2,v3) union all (v4,v5,v6) union all...
- 详情请见示例代码:
Oracle:
<insert id="inserData" parameterType="com.test.aaa.Bac">
insert into table_name (name, adress, age)
values
<foreach collection="list" item="item" index="index" separator="union all">
(select #{item.name},
#{item.adress},
#{item.age}
from dual )
<foreach>
</insert>
MySQL:
<insert id="inserData" parameterType="com.test.aaa.Bac">
insert into table_name (name, adress, age)
values
<foreach collection="list" item="item" index="index" separator=",">
( #{item.name}, #{item.adress}, #{item.age} )
<foreach>
</insert>