Mabatis <where> 의미
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
<where> 을 쓰지 않고 where로 쓴다면
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
where
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
위와 같이 될 겁니다. 이렇게 되면 어떤 문제가 발생하냐..
if 절이 모두 실행 되지 않는다면 sql 문장이
SELECT * FROM BLOG
where
위와 같이 되서 에러가 발생합니다..ㅠㅠ
그리고 첫번쨰 if 절이 실행이 되지 않는다면..
SELECT * FROM BLOG
where
AND title like #{title}
where 후 AND 로 시작하게 됩니다.
이러한 부분을 방지하여 주는 것이 <where> 입니다 AND 나 OR 로 시작하면 지워주고 where 뒤에 아무것도 없다면 where자체를 만들어 주지 않는 역할을 한다네요~