๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐Ÿƒ Spring

Spring/Mybatis | foreach(bulk insert)

  • -

์—ฌ๋Ÿฌ๊ฐœ๋ฅผ insert ํ•˜๊ฑฐ๋‚˜ delete ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ.

์ฟผ๋ฆฌ๋ฅผ ๋‹จ์ผ๋กœ ๋ณด๋‚ด์ง€ ๋ง๊ณ  ํ•œ ๋ฒˆ์— ๋™์ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

(Bulk Insert ์ฟผ๋ฆฌ ์‚ฌ์šฉ)

ํ˜•ํƒœ

<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
	#{item}
</foreach>
  • collection : ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž๊ฐ’
  • item : ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž ๊ฐ’์„ ์‚ฌ์šฉํ•  alias๋ช…
  • open : ํ•ด๋‹น ๊ตฌ๋ฌธ์ด ์‹œ์ž‘๋  ๋•Œ ์‚ฝ์ž…ํ•  ๋ฌธ์ž์—ด
  • close : ํ•ด๋‹น ๊ตฌ๋ฌธ์ด ์ข…๋ฃŒ๋  ๋•Œ ์‚ฝ์ž…ํ•  ๋ฌธ์ž์—ด
  • separator : ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌ๋ฌธ์‚ฌ์ด์— ๊ตฌ๋ถ„๊ฐ’
  • index : ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌ๋ฌธ ๋ฒˆํ˜ธ (0๋ถ€ํ„ฐ์‹œ์ž‘)

 

์‚ฌ์šฉ์˜ˆ์‹œ

<insert id="insertItem" parameterType="List">
	INSERT INTO TABLE_NAME
    VALUES
    <foreach collection="list" item="item" index="i" open="(" separator="," close=")">
    (
      #{item.number}
      #{item.name}
      #{item.age}
      #{item.addr}
      #{item.desc}
      #{i}
      ...
  	)
    </foreach>
</insert>

 

'๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก > ๐Ÿƒ Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Spring MVC | Web Application  (0) 2023.01.13
Spring Boot/VSC | New Project  (0) 2023.01.10
Spring/Mybatis | keyProperty  (0) 2023.01.09
Spring/Mybatis | ๊ณ ์ฐฐ #{}  (0) 2023.01.03
Spring | @RequestMapping  (0) 2023.01.02
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.