이번 프로젝트때 사용한 selectKey에 대해서 글을 작성한다.
...
글을 작성하고, 작성된 글 번호를 가지고 그 다음 작업을 해야할때,
아래와 같이 selectKey태그를 사용하게 되면,,,
1.seq_board 시퀀스의 다음 nextval를 구해온다.
2. 구해온 nextval를 getBno() 메서드에 값을 담아준다.
3.getBno()에 저장된 값과 title, writer등 게터 메서드의 값들을 가져와 db테이블에 insert해준다.
<insert id="insertSelectKey">
<selectKey order="BEFORE" keyProperty="bno" resultType="long">
select seq_board.nextval from dual
</selectKey>
insert into
tbl_board(bno, title, writer, content)
values
(#{bno}, #{title}, #{writer}, #{content})
</insert>
order : 명령 처리 선처리, 후처리
keyProperty : 처리할 필드명
resultType : 처리하 필드 명의 타입
insert쿼리에 keyProperty를 #{프리스테이트먼트} 안에 넣어 준다.
==서버단==
@Test
public void insert() {
BoardVO board = new BoardVO();
board.setWriter("jiseong");
board.setTitle("title...");
board.setContent("content........");
int result = mapper.insertSelectKey(board);
System.out.println("글작성 성공 (1) == "+result);
System.out.println("select key bno >> "+ board.getBno());
}
//글작성 성공 (1) == 1
//select key bno >> 22
서버단에서는 게터 메소드로 값을 읽어올 수 도 있다!