기타
게시판 번호 설정 (SelectKey 지정 에러)
hojomu
2023. 4. 14. 12:08
selecting key or setting result to parameter object
Mapper.xml에서 글을 쓸 때
<insert id="write">
<selectKey keyProperty="board_no" order="BEFORE" resultType="int">
select max(board_no)+1 board_no
from port_board
</selectKey>
insert into port_board(board_no, title, contents, id)
values(#{board_no}, #{title}, #{contents}, #{id})
</insert>
명령어를 실행한다
이 때 table에 데이터가 하나도 없고 board_no가 null 값으로 지정되어 있다면,
selectKey 를 불러올 때 기존에 board_no 가 null이기 때문에 max(board_no)+1 board_no 를 찾을 수가 없어서
에러가 발생한다.
table에 행을 하나 추가해서 max(board_no)가 null이 되지 않게 설정하거나, 열의 초기값을 null이 아닌 다른 수로 지정하자
-- 열의 초기값 설정
alter table port_board alter column board_no set default 0;
** 위의 해결방법을 사용해도 selectkey 에러가 해결되지 않아서 table에 첫 번째 행을 추가했다.