상세페이지 쿼리문 변경(JOIN)
SELECT A.*,
M.nickname AS extra__writer,
IFNULL(SUM(RP.point),0) AS extra__sumReactionPoint,
IFNULL(SUM(IF(RP.point > 0, RP.point, 0)),0) AS extra__goodReactionPoint,
IFNULL(SUM(IF(RP.point < 0, RP.point, 0)),0) AS extra__badReactionPoint
FROM article AS A
LEFT JOIN `member` AS M
ON A.memberId = M.id
LEFT JOIN reactionPoint AS RP
ON RP.relTypeCode = 'article'
AND A.id = RP.relId
WHERE A.id = #{id}
GROUP BY A.id
서브쿼리
대신 조인
을 이용해 3개의 테이블을 합침1. 게시물에 좋아요/싫어요 선택했는지 찾는 쿼리문
<select id="getSumReactionPointByMemberId" resultType="int">
SELECT IFNULL(SUM(`point`),0) FROM reactionPoint
WHERE relTypeCode = 'article'
AND relId = #{id}
AND memberId =#{actorId}
</select>
한번만
할수있기 때문에 선택을 했다면, reactionPoint테이블에 데이터가 하나
남겨져 있다.point
컬럼만 조회시 데이터가 없다면 로우가 나타나지 않는다. 집계함수를 쓰면 값이 없어도 0을 리턴
2. 게시물에 좋아요/싫어요 선택했는지 여부 model객체에 추가
@RequestMapping("/usr/article/detail")
public String showDetail(Model model, int id) {
Article article = articleService.getForPrintArticle(rq.getLoginedMemberId(), id);
boolean actorCanMakeReaction = articleService.actorCanMakeReaction(rq.getLoginedMemberId(),id);
model.addAttribute("actorCanMakeReaction",actorCanMakeReaction);
~~~ 생략 ~~~
}