본문 바로가기
목차
고민, 나중에..

마이바티스 SQL 고찰..(개인 학습)<where><if></if></where>

by 지각생 2022. 2. 12.
728x90

 

<select id="getWritingList" resultType="com.MyBlog.Dto.Board">
		select * from board
		<where>
			<if test="query !=null and query != ''">
				${field} like '%${query}%'
			</if>
		and
			<if test="categoryName !=null and categoryName != ''">
			categoryName = #{categoryName}
			</if>
			
			<if test="categoryName ==''">
			categoryName like '%${categoryName}%'
			</if>
		and
			<if test="nickName !=null and nickName != ''">
			nickName = #{nickName}
			</if>
		
			<if test="nickName ==''">
			nickName like '%${nickName}%'
			</if>
			and pub = #{pub}
			order by ${order} ${desc}
		limit #{offset}, #{rowNum}
		</where>
	</select>

categoryName, nickName은 not null값이니 (디폴트 값 "" 갖도록 코딩해 둠)

if절 안에

categoryName !=null and 

nickName !=null and 

은 지워도 문제 없으니 지우자.

<select id="getWritingList" resultType="com.MyBlog.Dto.Board">
		select * from board
		<where>
			<if test="query !=null and query != ''">
				${field} like '%${query}%'
			</if>
		and
			<if test="categoryName != ''">
			categoryName = #{categoryName}
			</if>
			
			<if test="categoryName ==''">
			categoryName like '%${categoryName}%'
			</if>
		and
			<if test="nickName != ''">
			nickName = #{nickName}
			</if>
		
			<if test="nickName ==''">
			nickName like '%${nickName}%'
			</if>
			and pub = #{pub}
			order by ${order} ${desc}
		limit #{offset}, #{rowNum}
		</where>
	</select>

query도 기본값이 ""이라서 null이 아니니깐.

query !=null and 

을 지워보자

 

<select id="getWritingList" resultType="com.MyBlog.Dto.Board">
		select * from board
		<where>
			<if test="query != ''">
				${field} like '%${query}%'
			</if>
		and
			<if test="categoryName != ''">
			categoryName = #{categoryName}
			</if>
			
			<if test="categoryName ==''">
			categoryName like '%${categoryName}%'
			</if>
		and
			<if test="nickName != ''">
			nickName = #{nickName}
			</if>
		
			<if test="nickName ==''">
			nickName like '%${nickName}%'
			</if>
			and pub = #{pub}
			order by ${order} ${desc}
		limit #{offset}, #{rowNum}
		</where>
	</select>

여기까진 내 생각대로 잘 작동한다.

내가 코딩해준 기본값대로 작동하면

select * from board where

                                  title like '%%'

                            and categoryName like '%%'

                            and nickName like '%%' 

                            and pub =1

                                  order by data desc

                                  limit 0, 15;

이렇게 움직이겠지

그런데 여기서

<if test="categoryName ==''">
			categoryName like '%${categoryName}%'
</if>
<if test="nickName ==''">
			nickName like '%${nickName}%'
</if>

를 빼면?

select * from board where

                                  title like '%%'

                            and pub =1

                                  order by data desc

                                  limit 0, 15;

<select id="getWritingList" resultType="com.MyBlog.Dto.Board">
		select * from board
		<where>
			<if test="query != ''">
				${field} like '%${query}%'
			</if>

			<if test="categoryName != ''">
			categoryName = #{categoryName}
			</if>
			
	
			<if test="nickName != ''">
			nickName = #{nickName}
			</if>
		
			and pub = #{pub}
			order by ${order} ${desc}
		limit #{offset}, #{rowNum}
		</where>
	</select>

이렇게 압축할 수 있겠지

그런데 작동시 categoryName에 디폴트 값이 아닌 "없음" 값이 들어간다면

 

select * from board where

                                  title like '%%'

                           categoryName like '%없음%'

                           nickName like '%1212%' 

                            and pub =1

                                  order by data desc

                                  limit 0, 15;

이렇게 and값이 없어서 디폴트 값을 벗어나면 에러가 뜨는구나!

그럼 

<select id="getWritingList" resultType="com.MyBlog.Dto.Board">
		select * from board
		<where>
			<if test="query != ''">
				${field} like '%${query}%'
			</if>
		and
			<if test="categoryName != ''">
			categoryName = #{categoryName}
			</if>
			
			<if test="categoryName ==''">
			categoryName like '%${categoryName}%'
			</if>
		and
			<if test="nickName != ''">
			nickName = #{nickName}
			</if>
		
			<if test="nickName ==''">
			nickName like '%${nickName}%'
			</if>
			and pub = #{pub}
			order by ${order} ${desc}
		limit #{offset}, #{rowNum}
		</where>
	</select>

이렇게 확정 지으면 내가 원하는대로 잘 작동할 것 같다.

 

그런데 하나만 더 생각해보자

<select id="getWritingList" resultType="com.MyBlog.Dto.Board">
		select * from board
		<where>
			<if test="query !=null and query != ''">
				${field} like '%${query}%'
			</if>
		and
			<if test="categoryName != ''">
			categoryName = #{categoryName}
			</if>
			
			<if test="categoryName ==''">
			categoryName like '%${categoryName}%'
			</if>
		and
			<if test="nickName != ''">
			nickName = #{nickName}
			</if>
		
			<if test="nickName ==''">
			nickName like '%${nickName}%'
			</if>
			and pub = #{pub}
			order by ${order} ${desc}
		limit #{offset}, #{rowNum}
		</where>
	</select>

query !=null and 값만 주고 작성한다면

query =""; 값일때 입력될때

if문은 

query !=null and query != ""에 해당되지 않으므로 해당 if문은 실행되지 않는다.

 

그럼

select * from board where

                                  title like '%%'

                            and categoryName like '%%'

                            and nickName like '%%' 

                            and pub =1

                                  order by data desc

                                  limit 0, 15;

and 

는 <where> 태그 절 안이라 마이바티스가 삭제 처리를 해줘서 정상작동하는 것이다.

 

결론

1. <where>태그 절 안에 if절이 실행되지 않아 첫문장에 오는 and 는 마이바티스가 처리해주나보다.

  (그렇다고 듣긴 했는데 살짝 아리쏭한 부분이 아직 있긴하다.)

2.  태그 절 안에 if조건문에 의해 실행되지 않았을때 and가 없어 문장이 어색하다면 오류가 발생한다.

3. 인자로 query ="";값을 줬는데 SQL내에서 if절에 해당되지 않아 인자가 쓰이는 곳이 없어도 문제 되지 않는 듯하다.

 

728x90

댓글