insert 전/후에 PK 값(시퀀스, AUTO_INCREMENT 등)을 가져오는 보조쿼리
selectKeyinsert 실행할 때, DB에 자동 증가 키(PK) 값이 생기는 경우
예) 오라클의 시퀀스, MySQL의 AUTO_INCREMENT, 혹은 방금처럼 MAX값을 이용해서 직접 가져올 때
그런데, 방금 넣은 행의 PK 값을 Java 객체에도 세팅해주고 싶을 때 씀
즉, insert 한 뒤 → 추가 쿼리(selectKey) 실행 → 결과를 자바 객체 속성에 넣어줌
<mapper namespace="com.haenin.transactional.section01.annotation.OrderMapper">
<insert id="insertOrder"
**parameterType="com.haenin.transactional.section01.annotation.Order"**>
INSERT
INTO TBL_ORDER
(
ORDER_DATE
, ORDER_TIME
, TOTAL_ORDER_PRICE
)
VALUES
(
#{orderDate}
, #{orderTime}
, #{totalOrderPrice}
)
**<selectKey keyProperty="orderCode"
order="AFTER"
resultType="_int">
// SELECT MAX(ORDER_CODE) FROM TBL_ORDER
SELECT LAST_INSERT_ID()
</selectKey>**
</insert>
</mapper>
keyPropertyOrder 클래스)에서 어떤 속성에 결과값을 넣을지 지정Order.orderCode 에 넣겠다는 것orderBEFORE : insert 실행 전에 selectKey 쿼리 실행
→ 주로 오라클 SEQUENCE.NEXTVAL 같은 경우
AFTER : insert 실행한 후에 selectKey 쿼리 실행
→ 방금 넣은 행의 PK를 가져오는 경우 (MySQL LAST_INSERT_ID(), MAX(), etc)
resultTypeselectKey 실행 결과의 타입
_int 는 int 타입을 의미

결과값을 orderCode 속성에 그대로 대입

→ selectKey는 insert와 붙여서 처리하므로 불필요한 I/O 줄임