본문 바로가기
개발노트

MyBatis insert 후 auto_increment값 바로 가져오기

by 별토끼. 2018. 2. 4.
반응형

MyBatis insert 후 PK(auto_increment)값 바로 가져오기

 

 

 

게시판을 만들면서 auto_increment값을 바로 가져와야 자식 테이블에 key를 등록해줄 수 있었다.

 

그렇지만 일일이 구현하려 하니 foreign Key가 일치하지 않는다는 오류만 줄줄이 떴고 

구글링을 하자 굉장히 편리한 기능이 있었다! 

<insert id="insert" parameterType="projTimelineDto" useGeneratedKeys="true" keyProperty="post_num"> 
        INSERT INTO proj_post_board 
        (post_num, post_title, post_filePath, post_content, post_regr_id, post_modr_id, post_proj_num) 
        VALUES(#{post_num}, #{post_title},#{post_filePath},#{post_content}, #{post_regr_id}, #{post_modr_id}, #{post_proj_num}) 
 </insert>  

 

 

useGeneratedKeys="true" keyProperty="post_num"

 

이것을  Mapper에 추가해주면 자동으로 값이 들어간다.

 

int post_num = dto.getPost_num();

 

그 후, getter를 이용해서 값을 가져오면 된다!

반응형

댓글