Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
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
Archives
Today
Total
관리 메뉴

괴발개발 개발하다

[ Spring ] 스프링 영속 계층 CRUD 구현 (mapper 테스트) 본문

spring

[ Spring ] 스프링 영속 계층 CRUD 구현 (mapper 테스트)

괴발새발개발자 2022. 7. 11. 01:12

 

프로젝트 구성

 

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));
	}
}