3
2

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にList<Object>を一括update

Posted at

mybatisを利用して、OracleDBにListを一括update方法を紹介します。

mapperメソッド定義

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

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

mapper.xmlのSQL定義

NGパターン

<update id ="updateBatch" parameterType= "java.util.List" >
	 <foreach collection ="itemList" item="item" separator= ";">
		  update TABLEAAA
		   <set >
				C1 = #{item.c1},
				C2 = #{item.c2}
		   </set >
		  WHERE ID = #{item.id}
	 </foreach >
</update >

書き方は大丈夫そうな感じですが、実はエラーが発生します。
Oracleの場合は特別です。

OKパターン

<update id ="updateBatch" parameterType= "java.util.List" >
	begin
	 <foreach collection ="itemList" item="item" separator= ";">
		  update TABLEAAA
		   <set >
				C1 = #{item.c1},
				C2 = #{item.c2}
		   </set >
		  WHERE ID = #{item.id}
	 </foreach >
	;end;
</update >
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?