db에 테이블 생성

create table tbl_board (
    bno     number(10)      not null,
    title   varchar2(200)   not null,
    content varchar2(1000)  null,
    writer  varchar2(50)    not null,
    regdate date            default sysdate,
    viewcount number        default 0
);

create unique index idx_tbl_board_pk on tbl_board
(bno ASC);

alter table tbl_board
add constraint tbl_board_bno_pk primary key(bno);

create sequence board_seq
increment by 1
start with 1
nomaxvalue;

 

src/main/java 경로에 있는 domain 폴더에 BoardDTO 파일 생성 후 작성

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BoardDTO {
	private Integer bno;
	private String title;
	private String content;
	private String writer;
	private Date regdate;
	private int viewcount;
}

 

src/main/java 경로에 있는 persistence 패키지에 IBoardDAO 인터페이스 작성

public interface IBoardDAO {
	
	public void create(BoardDTO bDto) throws Exception;
	public BoardDTO read(Integer bno) throws Exception;
	public int update(BoardDTO bDto) throws Exception;
	public int delete(Integer bno) throws Exception;
	public List<BoardDTO> listAll() throws Exception;
	
}

 

src/main/resources경로에 있는 mappers 폴더에 boardMapper.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="BoardMapper">
	<insert id="create">
		INSERT INTO tbl_board (bno, title, content, writer)
		VALUES (board_seq.nextval, #{title}, #{content}, #{writer})
	</insert>
	
	<select id="read" resultType="com.hanul.springstudent.domain.BoardDTO">
		SELECT 	bno, 
				title, 
				content, 
				writer,
				regdate,
				viewcount
		FROM	tbl_board
		WHERE	bno = #{bno}
	</select>
	
	<update id="update">
		UPDATE	tbl_board
		SET		title = #{title}, content = #{content}
		WHERE	bno = #{bno}
	</update>
	
	<delete id="delete">
		DELETE
		FROM	tbl_board
		WHERE	bno = #{bno}
	</delete>
	
	<select id="listAll" resultType="com.hanul.springstudent.domain.BoardDTO">
		<![CDATA[
			SELECT	bno,
					title,
					content,
					writer,
					regdate,
					viewcount
			FROM	tbl_board	
			WHERE	bno > 0
			ORDER BY bno DESC, regdate DESC
		]]>
	</select>
</mapper>

 

persistence 패키지 안에 있는 impl 패키지에 BoardDAOImpl 클래스 생성 후 작성

@Repository
public class BoardDAOImpl implements IBoardDAO {
	
	@Autowired
	private SqlSession session;

	@Override
	public void create(BoardDTO bDto) throws Exception {
		session.insert("BoardMapper.create", bDto);
	}

	@Override
	public BoardDTO read(Integer bno) throws Exception {
		return session.selectOne("BoardMapper.read", bno);
	}

	@Override
	public int update(BoardDTO bDto) throws Exception {
		return session.update("BoardMapper.update", bDto);
	}

	@Override
	public int delete(Integer bno) throws Exception {
		return session.delete("BoardMapper.delete", bno);
	}

	@Override
	public List<BoardDTO> listAll() throws Exception {
		return session.selectList("BoardMapper.listAll");
	}
	
}

 

src/test/java 경로에 JUnit Test Case 생성 후 등록 메소드 작성

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class IBoardDAOtest {
	@Autowired
	private IBoardDAO bDao;
	
	@Test
	public void testCreate() throws Exception {
		BoardDTO bDto = new BoardDTO();
		bDto.setTitle("새로운 글을 등록합니다.");
		bDto.setContent("새로운 글을 등록합니다.");
		bDto.setWriter("user03");
		
		bDao.create(bDto);
	}

}

 

빈공간 우클릭하여 JUnit Test 실행

 

우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!

 

여러번 실행해서 db에 테스트 게시물 15개 등록

 

조회 메소드 추가

@Test
public void testRead() throws Exception {
    log.info(bDao.read(1).toString());
}

 

빈공간 우클릭하여 JUnit Test 실행

 

우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!

 

수정 메소드 추가

@Test
public void testUpdate() throws Exception {
    BoardDTO bDto = new BoardDTO();
    bDto.setBno(1);
    bDto.setTitle("수정된 글입니다.");
    bDto.setContent("수정된 글입니다.");

    bDao.update(bDto);
}

 

빈공간 우클릭하여 JUnit Test 실행

 

우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!

 

삭제 메소드 추가

@Test
public void testDelete() throws Exception {
    bDao.delete(1);
}

 

빈공간 우클릭하여 JUnit Test 실행

 

우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!