괴발개발 개발하다
[ Spring ] 스프링 영속 계층 CRUD 구현 (mapper 테스트) 본문
1. BoardVO.java (클래스)
package org.pky.domain;
import java.util.Date;
import lombok.Data;
@Data
public class BoardVO {
private int board_no;
private String title, content, writer;
private Date regdate, updateDate;
private int viewCnt;
}
2. BoardMapper.java (인터페이스)
package org.pky.mapper;
import java.util.List;
import org.pky.domain.BoardVO;
public interface BoardMapper {
public List<BoardVO> getList();
public void insert(BoardVO board);
public void insertSelectKey(BoardVO board);
public BoardVO read(int board_no);
public int delete(int board_no);
public int update(BoardVO board);
}
3. BoardMapper.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.pky.mapper.BoardMapper">
<select id="getList" resultType="org.pky.domain.BoardVO">
select * from tbl_board order by board_no desc
</select>
<insert id="insert">
insert into tbl_board(board_no, title, content, writer)
values(seq_board.nextval, #{title}, #{content}, #{writer})
</insert>
<insert id="insertSelectKey">
<selectKey order="BEFORE" keyProperty="board_no" resultType="int">
select seq_board.nextval from dual
</selectKey>
insert into tbl_board(board_no, title, content, writer)
values(#{board_no}, #{title}, #{content}, #{writer})
</insert>
<select id="read" resultType="org.pky.domain.BoardVO">
select * from tbl_board where board_no = #{board_no}
</select>
<delete id="delete">
delete from tbl_board where board_no = #{board_no}
</delete>
<update id="update">
update tbl_board
set title = #{title},
content = #{content},
writer = #{writer},
updatedate = sysdate
where board_no = #{board_no}
</update>
</mapper>
4. BoardMapperTests.java (테스트 코드)
package org.pky.mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.pky.domain.BoardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class BoardMapperTests {
@Autowired
private BoardMapper boardMapper;
@Test
public void testGetList() {
log.info("====================");
boardMapper.getList();
}
@Test
public void testInsert() {
BoardVO vo = new BoardVO();
vo.setTitle("remember Title");
vo.setContent("rememberContent");
vo.setWriter("rememberWriter");
boardMapper.insert(vo);
log.info("---------------");
log.info("after insert " + vo.getBoard_no());
}
@Test
public void testInsertSelectKey() {
BoardVO vo = new BoardVO();
vo.setTitle("AAremember Title");
vo.setContent("AArememberContent");
vo.setWriter("AArememberWriter");
boardMapper.insertSelectKey(vo);
log.info("-------------------");
log.info("after insertSelectKey " + vo.getBoard_no());
}
@Test
public void testRead() {
BoardVO vo = boardMapper.read(282);
log.info(vo);
}
@Test
public void testDelete() {
int count = boardMapper.delete(282);
log.info("count ==>> " + count);
}
@Test
public void testUpdate() {
BoardVO vo = new BoardVO();
vo.setBoard_no(281);
vo.setTitle("Update Title");
vo.setContent("update content");
vo.setWriter("update Writer");
log.info("count: " + boardMapper.update(vo));
}
}
'spring' 카테고리의 다른 글
[ Spring ] 스프링 서비스 계층 설정/구현/테스트 (0) | 2022.07.11 |
---|---|
[ Spring ] 스프링 프로젝트 생성 후 pom.xml 설정 (0) | 2022.07.10 |
[ Spring ] 스프링 게시판 만들기 테이블 생성(오라클) (0) | 2022.07.10 |
[ Spring ] 스프링 Error Page (에러 페이지) 설정 (0) | 2022.07.10 |
[ Spring ] 스프링 log 레벨 설정 (0) | 2022.07.09 |