批量插入在oracle和mysql中配置几乎一致,但是sql语句不同。
oracle配置如下
<insert id="setRoles" parameterType="java.util.List"> insert into role <foreach collection="list" item="role" index="index" separator="union all"> select #{role.roleID},#{role.moduleID},#{role.roleName},#{role.grade},#{role.roleID} from dual </foreach> </insert>
mysql配置如下
<insert id="insertByBatch" parameterType="java.util.List"> insert into attachment_table (name, logID,url) values <foreach collection="list" item="item" index="index" separator=","> (#{item.name,jdbcType=VARCHAR}, #{item.logid,jdbcType=INTEGER},#{item.url,jdbcType=LONGVARCHAR}) </foreach> </insert>
注意其中
1、oracle的插入语句里没有values
2、oracle的foreach循环中使用的是select **** from dual
这两种不同的方式,输入的日志格式也不同。
oracle :
insert into role select ?,?,?,?,? from dual union all select ?,?,?,?,? from dual union all select ?,?,?,?,? from dual
mysql :
insert into role values(?,?,?,?,?),(?,?,?,?,?)
由于oracle中没有mysql的这种插入方式,才导致两种不同数据库需要两种不同的批量插入方式。
具体的配置当中,foreach是一个循环标签,由于我们的传入参数是list,所以collection的值为list;item代表list中每一个对象的名称