oracle批量insert

oracle批量insert

https://blog.csdn.net/ghgzczxcvxv/article/details/51577086

oracle中的批量操作有如下两种情况:

插入的数据集合为一个子查询

如果我们需要插入的数据集合来自数据库,则我们可以采用如下形式来就行批量插入:

insert into order_info
  (o_id, o_name, addtime)
  (select t.username, t.realname, sysdate
     from ITM_USER t
    where t.username is not null);
  • 1
  • 2
  • 3
  • 4
  • 5

把一个list集合的pojo插入数据库

如果我们需要插入的数据集合来自于java中的list集合,则此时我们需要借助于dual和union来拼装出一个数据库中的供插入的数据集合:

insert into order_info t
  (o_id, o_name, addtime)
  ((select 1, 'ykp', sysdate from dual) union
   (select 2, 'ykp', sysdate from dual) union
   (select 3, 'ykp', sysdate from dual) union
   (select 4, 'ykp', sysdate from dual));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

mybatis中如下:

<insert id="addOrder_info" parameterType="java.util.List">
    insert into order_info (o_id, o_name, addtime)
    <foreach collection="list" item="item" index="index" separator="union all" >
      (select #{item.o_id}, #{item.o_name}, sysdate from dual)
    </foreach>  
</insert>
原文地址:https://www.cnblogs.com/handsome1013/p/8931283.html