public interface ReplyMapper {
public int insert(ReplyVO vo);
public ReplyVO read(Long rno); // 특정 댓글 읽기
}
2. ReplyMapper.xml 내용 추가
<select id="read" resultType="org.codehows.domain.ReplyVO">
select * from tbl_reply where rno = #{rno}
</select>
3. ReplyMapperTests 클래스
@Test
public void testRead() {
Long targetRno = 5L;
ReplyVO vo = mapper.read(targetRno);
log.info(vo);
}
실행시 아래와 같은 결과가 나타나는 것을 볼 수 있습니다.
◎ 삭제(delete)
▷ 특정 댓글의 삭제는 댓글의 번호(rno)만으로 처리가 가능
1. ReplyMapper 메소드 내용 추가
public interface ReplyMapper {
public int insert(ReplyVO vo);
public ReplyVO read(Long rno); // 특정 댓글 읽기
public int delete (Long rno);
}
2. ReplyMapper.xml 내용 추가
<delete id="delete">
delete from tbl_reply where rno = #{rno}
</delete>
3. ReplyMapperTests 클래스 메서드 추가
@Test
public void testDelete() {
Long targetRno = 1L;
mapper.delete(targetRno);
}
테이블의 1번 행이 삭제되는 것을 확인할 수 있습니다.
◎ 수정(update)
▷ 댓글의 수정은 현재의 tbl_reply 테이블의 구조에서는 댓글의 내용(reply)과 최종 수정 시잔(updatedate)을 수정
1. ReplyMapper 메소드 내용 추가
public interface ReplyMapper {
public int insert(ReplyVO vo);
public ReplyVO read(Long rno); // 특정 댓글 읽기
public int delete(Long targetRno);
public int update(ReplyVO reply);
}
2. ReplyMapper.xml 내용 추가
<update id="update">
update tbl_reply set reply = #{reply}, updatedate = sysdate where rno = #{rno}
</update>
3. ReplyMapperTests 클래스 메서드 추가
@Test
public void testUpdate() {
Long targetRno = 10L;
ReplyVO vo = mapper.read(targetRno);
vo.setReply("Update Reply ");
int count = mapper.update(vo);
log.info("UPDATE COUNT : " + count);
}
해당 게시물의 날짜와 시간이 업데이트 된 것을 확인할 수 있습니다.
(4) @Param 어노테이션과 댓글 목록
▷ 댓글 목록과 페이징 처리는 기존 게시물 페이징 처리와 유사
▷ 추가적으로 특정한 게시물의 댓글들만을 대상으로 하기 때문에 추가로 게시물의 번호가 필요하게 됨
▷ MyBatis가 두 개 이상의 데이터를 파라미터로 전달하는 방법
▶ 별도의 객체로 구성
▶ Map을 이용하는 방식
▶ @Param을 이용하는 방식(가장 간단함, '#{ }'의 이름으로 사용 가능)
◎ ReplyMapper 인터페이스 수정
package org.codehows.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.codehows.domain.Criteria;
import org.codehows.domain.ReplyVO;
public interface ReplyMapper {
public int insert(ReplyVO vo);
public ReplyVO read(Long rno); // 특정 댓글 읽기
public int delete(Long targetRno);
public int update(ReplyVO reply);
public List<ReplyVO> getListWithPaging(
@Param("cri") Criteria cri,
@Param("bno") Long bno);
}
XML로 처리할 때는 지정된 'cri'와 'bno'를 모두 사용할 수 있음
◎ ReplyMapper.xml 수정
▷ 특정 게시물의 댓글을 가져옴
<select id="getListWithPaging" resultType="org.codehows.domain.ReplyVO">
select rno, bno, reply, replyer, replyDate, updatedate
from tbl_reply
where bno = #{bno}
order by rno asc
</select>