๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/MyBatis

[MyBatis] ๋ณ€์ˆ˜ ์‚ฌ์šฉ์‹œ #{value}, ${value}์˜ ์ฐจ์ด

by ynzu๐Ÿค 2021. 11. 30.
๋ฐ˜์‘ํ˜•

 

์ง€๋‚œ๋ฒˆ์— ํšŒ์‚ฌ์—์„œ ์–˜๊ธฐ๋ฅผ ๋‚˜๋ˆ„๋‹ค๋ณด๋‹ˆ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ๋„ ์žˆ๊ธธ๋ž˜ ์ •๋ฆฌํ•ด๋ณธ๋‹ค!

 

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 policy=#{policy} THEN 1 ELSE 0 END ) as POL${policy}

 

์ด๋ ‡๊ฒŒ ์‹คํ–‰๋œ๋‹ค.

SUM( CASE WHEN u.policy='01' THEN 1 ELSE 0 END ) as POL01

 

 

 

728x90
๋ฐ˜์‘ํ˜•

'๊ฐœ๋ฐœ > MyBatis' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[MyBatis] ๋ฐ˜๋ณต๋˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑํ•˜๊ธฐ <foreach>  (0) 2022.01.06

๋Œ“๊ธ€