Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
5
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@yang1005

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

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
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
5
Help us understand the problem. What are the problem?