2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MybatisでOracleに一括insert(id自動増加)

Last updated at Posted at 2016-03-17

mybatisを利用して、OracleDBに一括insert方法を紹介します。
id自動増加パターン
OracleではID自動増加できないので、シーケンスを利用して、ID自動増加を実現できます。

ID自動増加ではないパターン

mapperメソッド定義

引数:insert対象リスト
itemListを一括DBにinsertします。

int insertBatch(@Param ("itemList" ) List<ObjectClass> itemList);

mapping.xmlのSQL定義

シーケンスを利用する

< insert id= "insertList" parameterType = "java.util.List">
      insert into TABLE_AAA (ID, C2, C3,C4
      ) select TABLE_AAA_SEQ.NEXTVAL, A.* from(
       <foreach collection = "itemList" item= "item" index= "index" separator = "UNION ALL">
          SELECT
            #{item.c2}as C2, #{item.c3}as C3, #{item.c4}as C4
          from dual
       </foreach >
      ) A
</ insert>

作成されるSQL

insert into TABLE_AAA(ID, C2, C3,C4)
select TABLE_AAA_SEQ.nextval, A.* from(
   select 22 as C2,33 as C3,44 as C4 from dual
   union all
   select 222 as C2,333 as C3,444 as C4 from dual
)A
2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?