Coin163

mybatis批量插入insert

2015-01-13by admin, 次阅读
MyBatis中批量插入insert的方法:

方法一:
<insert id="insertBatch" parameterType="java.util.List">
  insert into t_user(id,name) values
  <foreach collection="list" item="user" index="index" separator=",">
  ( #{user.id}, #{user.name})
  </foreach>
  </insert>


方法二:
<insert id="batchInsert2" parameterType="ArrayList">
     insert into t_user(id,name)
    <foreach collection="list" item="user" index="index" separator="union all">
      select #{user.id,jdbcType=VARCHAR},#{user.name,jdbcType=VARCHAR} from dual
    </foreach>
</insert>


可以考虑用union all来实现批量插入。
例如:
insert into XX_TABLE(XX,XX,XX)select 'xx','xx','xx' union all select 'xx','xx','xx' union all select 'xx','xx','xx' ...
先拼装好语句再动态传入insert into XX_TABLE(XX,XX,XX)后面部分
------分隔线----------------------------