본문 바로가기

수업 & 공부/수업 내용

27- 게시판 리뷰 (댓글 출력&카운트)

 

1. 댓글 출력기능

 내가만든 방법은
 comment에 있는 모든 값을 가져와서
 boardRead.jsp 에서 forEach문으로  
 현제 읽고있는 글의 id랑 일치하는 댓글만 뿌려줬는데

 (그러고보니 댓글이 많아질수록 느려지지 않을까..?)

 강사님은 comment에 있는 값을 출력해서 받을때 id값을 넣어줘서
 id에 맞는 댓글만 받아왔다. 이 방법이 훨씬 좋다

 

 

 

2. 댓글 수 출력기능

 내가 만든 방법은
 board의 commentCount에 댓글을 작성하면 
 댓글 수를 다 집계해서 board 테이블 commentCount 에
 업데이트 하는 쿼리를 썼다.

for(int i= 1; i<=maxId;i++) {
            pstmt = conn.prepareStatement("update board set commentCount=(select count(id) from comment where id=?) where id=?");
            pstmt.setInt(1, i);
            pstmt.setInt(2, i);
            pstmt.executeQuery();
        }

 (for문을 통해서 id값마다 해당 id의 댓글수를 계산해서 업데이트 해준다. maxId는 board글의 맨 마지막 글까지)

 리뷰에서 나온 방법은 해당 id에 하나 달았을때 그 id에 댓글 수를+1 하는 방법. 이런 방법도 있었구나

 

 

3. 답글달기 기능

 

create table repltest (
  cid int primary key,
  refid int not null default repltest.cid,
  depthnum int not null default 0,
  ordernum int not null default 0
)
-- 전체 글 목록 뽑을 때
select * from repltest order by refid desc, ordernum asc
-- 답글 쓸 때 ordernum를 정하기 위한 해당 글(답글)의 최고 번호를 가져옴.
select depthnum, max(ordernum) from repltest group by refid, depthnum having refid =1  and depthnum = 1
insert into repltest values (1, 1, 0, 0) -- cid, refid, depthnum, ordernum
insert into repltest values (2, 2, 0, 0) -- cid, refid, depthnum, ordernum
insert into repltest values (3, 3, 0, 0) -- cid, refid, depthnum, ordernum
insert into repltest values (4, 1, 1, 1) -- cid, refid, depthnum, ordernum
insert into repltest values (5, 1, 1, 2) -- cid, refid, depthnum, ordernum

수업에서 알게된 쿼리

 

전체적으로 연산 방법은 작성한 코드랑 비슷하지만 나는 코드를 BoardDAO.java에서 구현했다.

어떻게 쓸지


1. 답글insert.jsp 에서 ordernum과 depthnum, refid(상위글num) 받아서
   insert할 내용을 적어서 위 값과 함께 Cmd로 같이 보내면

2. 답글insertCmd 에서 정보들을 받아서 위 select쿼리를 실행
   =위 select쿼리에서 rifid와 depth를 넣으면 해당 깊이 orderNum의 최댓값을 구해준다
   따라서 orderNum의 최댓값에 +1을 하면 자기가 들어갈 위치
   >> 해당 rifid 나머지 자기자신과 같거나 큰 orderNum 은 +1 해줘야함. 왜냐하면 orderNum이 중복되기 때문

   >> 답글의 답글을 달때는 순서를 어떻게 정해줘야하지??

3. 2번에서 구한 orderNum을(나머지 정보도) 가져와서 insert문 쿼리를 통해 입력한다

 

0904 위 쿼리 사용방법 수정

 

-- 원본글에서 글 넣을때는 > refid는 해당 원본글의 id , depthnum 에 1 더해서 넣기 > 나온 값에서 ordernum + 1


-- 답글에서 (이단)답글을 넣을때는 해당 답글의 refid, 해당 답글의 depthnum에 1 더해서 넣기 > 해당 refid 에서 자기보다 크거나 같은 ordernum이 있으면 그 글들을 모두 +1

(원본글인지 답글인지 ... 는 depthNum을 보고 확인가능하다)

 

 

  

당연하지만 칼럼의 이름만 바꿔주면 내 table에서도 잘 동작하는걸 볼수있다.