java
[Mybatis] selectKey
kimxavi
2018. 11. 17. 13:22
반응형
데이터를 넣을 때, 기본키를 생성하고 insert 를 하도록 하고 싶을 때가 있다.
이럴 때, 사용할 수 있는 것이 <selectKey> 태그 이다
<insert id="insertUser">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
insert into User
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
- #{id} 같은 경우엔 넘어오는 인자에 dummy로 라도 넣어주어야 한다!
- ex) object 필드 내에 선언
- selectKey구문이 먼저 실행되고 User id프로퍼티에 셋팅된다. 그리고 나서 insert 구문이 실행된다
반응형