mybatis) selectKey 함수
- 작업 도중 특정 작업 이전(insert, update) 에 사전에 정의해야 할 데이터가 필요할 수도 있는데,
- mybatis에서는 특별한 쿼리 로직 없이 selectKey로 처리 가능하다
- 보통 인덱스 값 자동 증가할 때 사용한다.
useGeneratedKeys : insert, update시 자동생성 키를 받을때 true로 설정한다. (default: false)
keyProperty : 리턴 받을 key property 설정. 여러개를 사용한다면 ,(콤마)로 구분
: VO에 getter/setter가 존재해야한다.
<insert id="" useGeneratedKeys="true" keyProperty="imgIdx">
<selectKey keyProperty="리턴 받을 프로퍼티명" resultType="반환타입" order="BEFORE/AFTER">
작업 할 쿼리문;
</selectKey>
</insert>
시퀀스.nextval 처리 한 값 -> 'imgIdx' 에 저장
이제 insert 쿼리문에서 nextval 처리 된 'imgIdx' 값을 사용가능
<insert id="imgInsert" useGeneratedKeys="true" keyProperty="imgIdx">
<selectKey keyProperty="imgIdx" resultType="Integer" order="BEFORE">
select img_seq.nextval as imgIdx from dual
</selectKey>
</insert>
반응형
'개발 > Mybatis' 카테고리의 다른 글
Mybatis) ResultMap (0) | 2022.08.12 |
---|---|
Mybatis 조건문 (if , choose/when/otherwise) (0) | 2022.08.09 |
댓글