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

Spring/Mybatis | keyProperty

  • -

ํ•˜๋‚˜์˜ Unique Value ๋ฅผ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์— INSERT ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด ๊ฒŒ์‹œ๊ธ€์„ ๋“ฑ๋กํ•˜๊ณ , ๊ฒŒ์‹œ๊ธ€๊ณผ ํ•จ๊ป˜ ์ฒจ๋ถ€๋  ํŒŒ์ผ๋„ ๋“ฑ๋กํ•œ๋‹ค.

๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ๊ณผ์ •์—์„œ ๊ฒŒ์‹œ๊ธ€์ด ์ €์žฅ๋˜๋Š” table(๊ฒŒ์‹œ๊ธ€1) ๊ณผ ์ฒจ๋ถ€๋˜๋Š” ํŒŒ์ผ์˜ table(์ฒจ๋ถ€ํŒŒ์ผ1) ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ,

๊ฒŒ์‹œ๊ธ€1์˜ Unique key ๋ฅผ ์ฒจ๋ถ€ํŒŒ์ผ1๊ณผ ํ•จ๊ป˜ ์ €์žฅํ•ด์•ผ ์œ ์ผํ•œ ์ฒจ๋ถ€ํŒŒ์ผ์ด ๋œ๋‹ค.

(์ ์ ˆํ•œ ์˜ˆ์‹œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์„ธ์š” :3)

 

๋•Œ๋ฌธ์— key ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” Mybatis ์˜ keyProperty ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

์‚ฌ์šฉ๋ฐฉ๋ฒ•

<insert>
	table insert
    ...
    ...
    ...
    <selectKey keyProperty="key๊ฐ’" resultType="ํƒ€์ž…" order="์‹คํ–‰ํŒ๋‹จ">
            SELECT
                Table colum ์ด๋ฆ„
            FROM
                Table ์ด๋ฆ„ (+ order by reg_date desc limit 1)
    </selectKey>
</insert>
keyProperty selectKey๊ฐ€ ์ˆ˜ํ–‰๋œ ์ดํ›„์— ๊ฒฐ๊ณผ๊ฐ’์ด ์„ธํŒ…๋˜๋Š” key๊ฐ’. 
resultType selectKey๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๊ฐ’์˜ type. string ์ด๋ผ๊ฑฐ๋‚˜ int ๋ผ๊ฑฐ๋‚˜...
order BEFORE ํ˜น์€ AFTER๋กœ ์„ค์ • ๊ฐ€๋Šฅ. selectKey ๊ตฌ๋ฌธ์„ ๋จผ์ €(BEFORE) ์‹คํ–‰ ํ•  ๊ฒƒ์ธ์ง€ ํ˜น์€ ๋‚˜์ค‘์—(AFTER) ์‹คํ–‰ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ง€์ •ํ•œ๋‹ค.
(SQL๋ฌธ๋ฒ•) order by reg_date desc limit 1 ์™€ ๊ฐ™์€ ์กฐ๊ฑด์„ ๊ฑธ์–ด์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ๋ฝ‘์•„๋‚ธ๋‹ค.

 

board table insert ๋ฅผ ์ˆ˜ํ–‰ํ•œ ์ดํ›„, ํ•ด๋‹น Unique key ๋ฅผ ๋ฆฌํ„ด๋ฐ›์•„ file table insert ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

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

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

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