Spring_FrameWork

Ajax 댓글 처리

tmxhsk99 2018. 11. 29. 17:26

댓글 처리를 위한 테이블 생성과 처리 

create table tbl_reply(

rno number(10,0),

bno number(10,0) not null,

reply varchar2(1000) not null,

replyer varchar2 (50) not null,

replyDate date default sysdate,

updateDate date default sysdate

);

create sequence seq_reply;


alter table tbl_reply add constraint pk_reply primary key (rno);


alter table tbl_reply add constraint fk_reply_board

foreign key (bno) references tbl_board (bno);



bno칼럼을 이용해서 해당 댓글이 어떤 게시물의 댓글이니를 명시하도록합니다. 댓글 자체는 단독으로 

CRUD가 가능하므로 별도의 PK를 부여하도록 하고 외래키 FK 설정을 통해서 tbl_board 테이블을 참조하도록 설정한다.



tbl_reply 테이블을 참고해서 org.zerock.domain 패키지 아래 ReplyVO클래스를 추가 



package org.zerock.domain;


import java.util.Date;


import lombok.Data;


@Data

public class ReplyVO {

private Long rno;

private Long bno;

private String reply;

private String replyer;

private Date replyDate;

private Date updateDate;

}


ReplyMapper 클래스 XML 처리 


mapper 에 ReplyMapper 인터페이스를 처리하고, XML 파일을 생성 시켜준다.


댓글에 대한처리 역시 페이지처리가 필요할수 있으므로 Criteria를 이용해서 처리하도록 한다.


ReplyMapper.XML


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.zerock.mapper.ReplyMapper">


</mapper>


tbl_reply가 tbl_board 테이블의 bno 값과 정확히 일치해야 하므로 테스트를 진행하기전에 최신 bno 번호 몇개를 예제로 확신해 한다

replymapper.xm설정



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.zerock.mapper.ReplyMapper">
    <insert id="insert">
 
        insert into tbl_reply (rno, bno, reply, replyer)
        values (seq_reply.nextval, #{bno}, #{reply}, #{replyer})
 
    </insert>
    
    <select id="read" resultType ="org.zerock.domain.ReplyVO">
        select * from tbl_reply where rno = #{rno}
    </select>
    
    <delete id = "delete">
        delete from tbl_reply where rno =#{rno}
    </delete>
    
    <update id="update">
        update tbl_reply set reply = #{reply}, updatedate = sysdate where rno = #{rno}
    </update>
</mapper>
 
cs

mapper interface 설정


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.zerock.mapper;
 
import org.zerock.domain.ReplyVO;
 
public interface ReplyMapper {
    //리플 추가
    void insert(ReplyVO vo);
    //리플 조회 처리
    public ReplyVO read(Long bno);
    //삭제
    public int delete(Long bno);
    //수정 
    public int update (ReplyVO reply);
    
 
}
 
cs


 

ReplyMapperTests.java  (log가 안뜰때는 log4.property와 log4.xml의 로그 범위 설정을 체크)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.zerock.mapper;
 
import java.util.stream.IntStream;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.zerock.domain.ReplyVO;
 
import lombok.Setter;
import lombok.extern.log4j.Log4j;
 
@RunWith(SpringRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
// Java Config
// @ContextConfiguration(classes = { org.zerock.config.PersistenceConfig.class
// })
@Log4j
public class ReplyMapperTests {
 
    // 테스트 전에 해당 번호의 게시물이 존재하는지 반드시 확인할 것
    private Long[] bnoArr = { 16899L, 16898L, 16897L, 16896L, 16895L };
 
    @Setter(onMethod_ = @Autowired)
    private ReplyMapper mapper;
 
    //생성
    @Test
    public void testCreate() {
        IntStream.rangeClosed(110).forEach(i -> {
            // 1~10반복
            ReplyVO vo = new ReplyVO();
 
            vo.setBno(bnoArr[i % 5]);
            vo.setReply("댓글 테스트 " + i);
            vo.setReplyer("replyer" + i);
 
            mapper.insert(vo);
        });
 
    }
 
    // 조회
    @Test
    public void testRead() {
        Long targetRno = 5L;
 
        ReplyVO vo = mapper.read(targetRno);
 
        log.info(vo);
    }
    
    //삭제
    @Test
    public void testDelete() {
        Long targetRno = 1L;
        
        mapper.delete(targetRno);
    }
    //수정 
    @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);
    }
    
    //연동 확인
    @Test
    public void testMapper() {
 
        log.info(mapper);
    }
 
}
 
cs