Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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
yang1005
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした