๋กœ์ผ“๐Ÿพ
article thumbnail

 

๋งˆ์ด๋ฐ”ํ‹ฐ์Šค์—์„œ๋Š” ๋™์  ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€์˜ ํƒœ๊ทธ๋“ค์ด ์žˆ๋Š”๋ฐ, ์ด๋ฒˆ ๊ธ€์—์„œ <if test=""> ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

l <if> 

 

if๋ฌธ์€ ๋‹ค๋ฅธ ์ฝ”๋“œ์—์„œ์™€์˜ if๋ฌธ๊ณผ ์‚ฌ์šฉ ์šฉ๋„๊ฐ€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ๋˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•๋„ ์•„์ฃผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์˜ํ• ์ ์€ ๋ฐ˜๋“œ์‹œ ์กฐ๊ฑด๋ฌธ์•ž์— test=""๋ฅผ ์ ์–ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

	<select id="selectIF" parameterType="memberVO" resultType="memberVO">
		SELECT
			*
		FROM TEST_TABLE
		WHERE 1=1
			<if test="name != '' and name != null">
				AND name=#{name}
			</if>
	</select>

 

๋งŒ์•ฝ name์ด๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ์žˆ์œผ๋ฉด ํ•˜์œ„ ํƒœ๊ทธ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—†๋‹ค๋ฉด if ํƒœ๊ทธ์˜ ํ•˜์œ„ ํƒœ๊ทธ๋Š” ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค.

 

public MemberVO selectIF() {
		sqlMapper = getInstance();
		SqlSession session = sqlMapper.openSession();
		MemberVO memberVo = new MemberVO();
		MemberVO result = new MemberVO();
		
		memberVo.setId("๊น€๋ณ‘์ค€");
		memberVo.setName("๊น€๋ณ‘์ค€");
		
		
		result = session.selectOne("mapper.member.selectIF", memberVo);
		
		return result;
	}

 

setName์„ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์ง€์ •ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์— if ํƒœ๊ทธ์˜ ํ•˜์œ„ํƒœ๊ทธ๋Š” ์ž‘๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ ๊ฒฐ๊ณผ

 

๋งŒ์•ฝ setName์„ ์•ˆํ•ด์ฃผ๋ฉด ๊ฐ์ฒด๊ฐ€ ์—†์–ด์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—๋Ÿฌ ์ด์œ ๋Š” select * from table๋กœ ์ฟผ๋ฆฌ๋ฌธ์ด ์ž‘๋™ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด ๊ฒฝ์šฐ selectOne์ด ์•„๋‹Œ selectList๋กœ ๋ฐ›์•„์•ผํ•ด์„œ ์—๋Ÿฌ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค. ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค๋Š” ์ด์œ ๋งŒ์œผ๋กœ if ํƒœ๊ทธ๋ฌธ์ด ๋ฌด์‹œ๋˜์—ˆ๋‹ค๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.

 

"๋‚œ ๋ฐ˜๋“œ์‹œ ๋ฐฑ์—”๋“œ ์™•์ด ๋ ๊ฑฐ์•ผ"

profile on loading

Loading...