db에 테이블 생성
create table tbl_reply(
rno number(10) not null,
bno number(10) not null,
replytext varchar2(1000) not null,
replyer varchar2(50) not null,
regdate date default sysdate,
updatedate date default sysdate
);
create unique index IDX_tbl_reply_PK on tbl_reply
(bno desc, rno asc);
alter table tbl_reply
add constraint tbl_reply_rno_PK primary key(rno);
alter table tbl_reply
add constraint tbl_reply_bno_FK foreign key(bno) REFERENCES tbl_board(bno);
create sequence reply_seq
increment by 1
start with 1
nomaxvalue;
src/main/java 경로에 있는 domain 폴더에 ReplyDTO 파일 생성 후 작성
@Data
public class ReplyDTO {
private int rno;
private int bno;
private String replytext;
private String replyer;
private Date regdate;
private Date updatedate;
}
src/main/java 경로에 mapper 패키지 생성 후 ReplyMapper 인터페이스를 생성 후 추상메소드 작성
public interface ReplyMapper {
public String getTime2();
}
src/main/resources경로에 있는 mappers 패키지에 ReplyMapper.xml 파일 생성 후 작성
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanul.springstudent.mapper.ReplyMapper">
<select id="getTime2" resultType="string">
SELECT sysdate
FROM dual
</select>
</mapper>
그리고 root-context.xml 파일 가장 하단에 코드 추가
<mybatis-spring:scan base-package="com.hanul.springstudent.mapper"/>
src/test/java 경로에 JUnit Test Case 생성 후 테스트 케이스 작성
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class ReplyMapperTests {
@Autowired
private ReplyMapper mapper;
@Test
public void testMapper() {
log.info("mapper ===> " + mapper.getTime2());
}
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 매퍼 인터 페이스를 잘 불러오고 있음!
ReplyMapper 인터페이스에 등록 추상메소드 추가
public int insert(ReplyDTO replDto);
ReplyMapper.xml에 insert문 추가
<insert id="insert">
INSERT INTO tbl_reply(rno, bno, replytext, replyer)
VALUES (reply_seq.nextval, #{bno}, #{replytext}, #{replyer})
</insert>
ReplyMapperTests 파일에 내 게시물 번호 5개 배열로 선언 후 등록 테스트케이스 추가
private int[] bnoArr = {2506722, 2506721, 2506720, 2506719, 2506718};
@Test
public void testInsert() {
IntStream.rangeClosed(1, 10).forEach(i -> {
ReplyDTO rDto = new ReplyDTO();
rDto.setBno(bnoArr[i%5]);
rDto.setReplytext("댓글 테스트" + i);
rDto.setReplyer("replyer" + i);
mapper.insert(rDto);
});
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
DB에 잘 등록되었나 확인
ReplyMapper 인터페이스에 조회 추상메소드 추가
public ReplyDTO read(int rno);
ReplyMapper.xml에 select문 추가
<select id="read" resultType="com.hanul.springstudent.domain.ReplyDTO">
SELECT *
FROM tbl_reply
WHERE rno = #{rno}
</select>
ReplyMapperTests 파일에 조회 테스트케이스 추가
@Test
public void testRead() {
int targetRno = 1;
ReplyDTO rDto = mapper.read(targetRno);
log.info(rDto);
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
ReplyMapper 인터페이스에 삭제 추상메소드 추가
public int delete(int rno);
ReplyMapper.xml에 delete문 추가
<delete id="delete">
DELETE
FROM tbl_reply
WHERE rno = #{rno}
</delete>
ReplyMapperTests 파일에 삭제 테스트케이스 추가
@Test
public void testDelete() {
int targetRno = 1;
mapper.delete(targetRno);
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
ReplyMapper 인터페이스에 수정 추상메소드 추가
public int update(ReplyDTO replDto);
ReplyMapper.xml에 update문 추가
<update id="update">
UPDATE tbl_reply
SET replytext = #{replytext}, updatedate = sysdate
WHERE rno = #{rno}
</update>
ReplyMapperTests 파일에 수정 테스트케이스 추가
@Test
public void testUpdate() {
int targetRno = 2;
ReplyDTO rDto = mapper.read(targetRno);
rDto.setReplytext("댓글 수정테스트");
int result = mapper.update(rDto);
if (result > 0) {
log.info("정상적으로 수정이 되었습니다.");
} else {
log.info("데이터 수정에 실패하였습니다.");
}
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
ReplyMapper 인터페이스에 전체조회 추상메소드 추가
public List<ReplyDTO> getListWithPaging(
@Param("cri") Criteria cri,
@Param("bno") int bno);
ReplyMapper.xml에 select문 추가
<select id="getListWithPaging" resultType="com.hanul.springstudent.domain.ReplyDTO">
SELECT rno,
bno,
replytext,
replyer,
regdate,
updatedate
FROM tbl_reply
WHERE bno = #{bno}
ORDER BY rno asc
</select>
ReplyMapperTests 파일에 전체조회 테스트케이스 추가
@Test
public void testList() {
Criteria cri = new Criteria();
List<ReplyDTO> replies = mapper.getListWithPaging(cri, bnoArr[0]);
replies.forEach(reply -> log.info(reply));
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
'Programming > Spring' 카테고리의 다른 글
[STS3] 댓글 기능(3) (0) | 2024.04.23 |
---|---|
[STS3] 댓글 기능(2) (0) | 2024.04.22 |
[STS3] 파일 업로드(1) (0) | 2024.04.22 |
[STS3] Mapper를 활용하여 게시판 구현(7) (2) | 2024.04.18 |
[STS3] Mapper를 활용하여 게시판 구현(6) (2) | 2024.04.18 |