๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

๊ฐœ๋ฐœ/MyBatis2

[MyBatis] ๋ฐ˜๋ณต๋˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑํ•˜๊ธฐ <foreach> collection ์ธ์ž(Map, Array, List, Set ๋“ฑ..) ๊ฐ’์œผ๋กœ ๋ฐ˜๋ณต์ ์ธ SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. (OR ๋˜๋Š” IN ๊ตฌ๋ฌธ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.) foreach๋Š” 6๊ฐœ์˜ ์†์„ฑ์ด ์žˆ๋‹ค. collection - ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž (Map, Array, List, Set๋“ฑ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด) item - collection ์†์„ฑ์—์„œ ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž๊ฐ’์„ alias๋ช…์œผ๋กœ ๋Œ€์ฒด open - ํ•ด๋‹น ๊ตฌ๋ฌธ์ด ์‹œ์ž‘๋  ๋•Œ ์‚ฝ์ž…ํ•  ๋ฌธ์ž์—ด close - ํ•ด๋‹น ๊ตฌ๋ฌธ์ด ์ข…๋ฃŒ๋  ๋•Œ ์‚ฝ์ž…ํ•  ๋ฌธ์ž์—ด separator - ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌ๋ฌธ ์‚ฌ์ด์— ์‚ฝ์ž…ํ•  ๋ฌธ์ž์—ด (๊ตฌ๋ฌธ์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์ž) index - ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌ๋ฌธ ๋ฒˆํ˜ธ. 0๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ€ ์˜ˆ์ œ a, b, c๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋Š” list๊ฐ€ ์žˆ๊ณ , ํ•ด๋‹น list์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’.. 2022. 1. 6.
[MyBatis] ๋ณ€์ˆ˜ ์‚ฌ์šฉ์‹œ #{value}, ${value}์˜ ์ฐจ์ด ์ง€๋‚œ๋ฒˆ์— ํšŒ์‚ฌ์—์„œ ์–˜๊ธฐ๋ฅผ ๋‚˜๋ˆ„๋‹ค๋ณด๋‹ˆ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ๋„ ์žˆ๊ธธ๋ž˜ ์ •๋ฆฌํ•ด๋ณธ๋‹ค! 1. #{} ์‚ฌ์šฉ SELECT * FROM USER WHERE USER_ID = #{user_id} user_id์˜ ๊ฐ’์ด test๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. SELECT * FROM USER WHERE USER_ID = 'test'; 2. ${} ์‚ฌ์šฉ SELECT * FROM USER WHERE USER_ID = ${user_id} user_id์˜ ๊ฐ’์ด test๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. SELECT * FROM USER WHERE USER_ID = test ${}๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ž‘์€๋”ฐ์˜ดํ‘œ๊ฐ€ ์•ˆ๋ถ™๊ธฐ๋•Œ๋ฌธ์— ์˜ˆ์•ฝ์–ด๋‚˜ ์นผ๋Ÿผ๋ช… ๋“ฑ์„ ์‚ฌ์šฉํ•  ๋•Œ ์ ์šฉํ•˜๋ฉด ๋œ๋‹ค ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ–ˆ์„ ๊ฒฝ์šฐ SUM( CASE WHEN .. 2021. 11. 30.
728x90
๋ฐ˜์‘ํ˜•