<choose> ํ๊ทธ๋ switch๋ฌธ๊ณผ ๋น์ทํ ์ญํ ์ ํฉ๋๋ค.
l <choose> ํ๊ทธ
<select id="selectCHOOSE" parameterType="memberVO" resultType="memberVO">
SELECT
*
FROM TEST_TABLE
WHERE 1=1
<choose>
<when test="id !='' and id !=null and name!='' and name!=null">
AND NAME=#{name} AND ID=#{id}
</when>
<when test="name!='' and name!=null">
AND NAME='ํ์ง์'
</when>
<when test="id!='' and id!=null">
AND ID='ํ์ง์'
</when>
</choose>
</select>
switch์ case ๋ฌธ์ฒ๋ผ choose๋ฌธ์์๋ when ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์กฐ๊ฑด์์๋ ํญ์ test๋ฅผ ๋ถ์ฌ์ค์ผ ํฉ๋๋ค. ์ ์ฝ๋์์๋ ๋ง์ฝ name, id๊ฐ ์์ผ๋ฉด ์ฒซ๋ฒ์งธ when์ด, name๋ง ์์ผ๋ฉด ๋๋ฒ์งธ when์ด, id๋ง ์์ผ๋ฉด ์ธ๋ฒ์งธ when ์คํ๋๋ ์ฝ๋์ ๋๋ค.
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.selectCHOOSE", memberVo);
return result;
}
setId๊ฐ ์์ผ๋ฉด ๋๋ฒ์งธ ์กฐ๊ฑด์ธ AND NAME='ํ์ง์'์ด ์คํ๋ ๊ฑธ ๋ณด์ค์ ์์ต๋๋ค.
์ฝ์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
์ง๋ฌธ์ ์ธ์ ๋ ํ์ํฉ๋๋ค.
"๋ ๋ฐ๋์ ๋ฐฑ์๋ ์์ด ๋ ๊ฑฐ์ผ"
'...' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MyBatis] ๋ง์ด๋ฐํฐ์ค include ๊ตฌ๋ฌธ ์ฌ์ฉํ๊ธฐ (0) | 2021.01.10 |
---|---|
[MyBatis] ๋ง์ด๋ฐํฐ์ค foreach ๊ตฌ๋ฌธ ์ฌ์ฉํ๊ธฐ (0) | 2021.01.10 |
[MyBatis] ๋ง์ด๋ฐํฐ์ค if ๋ฌธ ์ฌ์ฉํ๊ธฐ (0) | 2021.01.10 |
[Java] ์๋ฐ List ๋ฐ๋ณต๋ฌธ (ํฅ์๋ ๋ฐ๋ณต๋ฌธ) (0) | 2021.01.10 |
[Oracle] ์ค๋ผํด seq ์ต๋๊ฐ ๊ตฌํ๊ธฐ (0) | 2021.01.08 |