Trouble_Shooting

mybatis에서 if and 쓸 때 + jstl에서 if and 쓸 때

산타는 뽀미 2023. 6. 12. 15:39

mapper에서 mybatis 문법 쓸 때 if 조건문으로 쿼리문 조금씩 다르게 날릴때는

if 에서 || && 이런거 말고 and 랑 or 로 써야함!!

<select id="getArticles" resultType="Article">
		<if test="boardId == 2 or boardId == 5">
		WITH AMG AS (
		</if>
			SELECT A.*, M.name AS writerName, IFNULL(G.groupName,'전체') AS groupName
			FROM article AS A
			INNER JOIN `member` AS M
			ON A.memberId = M.id
			LEFT JOIN `group` AS G
			ON A.classId = G.id
			WHERE boardId = #{boardId}
			<if test="boardId != 2 and boardId != 5">
				ORDER BY A.id DESC;
			</if>
		<if test="boardId == 2">
		)
		SELECT AMG.*, IF(COUNT(H.hwMsg) = 0, '미검사','검사') AS 'hwChk'
		FROM AMG
		LEFT JOIN `homework` AS H
		ON AMG.id = H.relId
		GROUP BY AMG.id
		ORDER BY AMG.id DESC;
		</if>
		<if test="boardId == 5">
		)
		SELECT AMG.*, F.id AS fileId
		FROM AMG
		LEFT JOIN `file` AS F
		ON AMG.id = F.relId
		GROUP BY AMG.id
		ORDER BY AMG.id DESC;
		</if>
	</select>

예를 들어 이렇게 말이다!!

나는 하나의 list로 네개의 보드를 구분해서 관리하고 있어서 쿼리문을 다르게 날릴 필요가 있다.

각자 성격이 달라서 list 페이지를 나누고 싶었으나 그러면 수정 삭제 목록 버튼 링크도 다 수정해줘야하기에 저렇게 나눠서 했다. (성격이 너무 다른 하나의 보드만 따로 뺐다.)

 

 

참고한 블로그

https://java119.tistory.com/42

 

[MyBatis] 동적 쿼리 if문 문법 총 정리

시작하기에앞서 참고 자료 *ibatis 비교문 지원 태그 isNull : "널일경우" isNotNull : "널이아닐경우" isEmpty : "공백일경우" isNotEmpty : "공백이아닐경우" isGreaterTan : ">" isGreaterEqual : ">=" isLessThan : "" 괄호를

java119.tistory.com

 

또한 html에서 c태그중 <c:if test="~"> 이거 쓸 때는 and랑 or 어떻게 쓰냐?!

이때는 그냥 || 랑 && 써도 된다고 한다.

<c:if test="${article.boardId == 2 && rq.getLoginedMember().getAuthLevel() == 1}">
    <c:choose>
        <c:when test="${homeworks.isEmpty() }">
            <div>
                <a class="homeworkChk btn btn-success mr-2" 
                onclick="getStudentsByClass(${article.id},${article.classId});">과제검사</a>
            </div>
        </c:when>
        <c:otherwise>
            <div>
                <a class="getHws btn btn-success btn-outline mr-2">과제확인</a>
            </div>
        </c:otherwise>
    </c:choose>
</c:if>

요렇겡!!!

참고한 블로그

https://kmhan.tistory.com/390

 

[Jsp] jstl if문에서 and와 or문 사용하는 방법

jstl if문에서 and와 or문 사용하는 방법 and ... or ... 출처: https://yangyag.tistory.com/112 [Hello Brother!]

kmhan.tistory.com