LAST_INSERT_ID()
last_insert_id 함수는 테이블의 마지막 auto_increment 값을 리턴한다.
이걸 사용하면 insert한 뒤에, 자동 증가된 pk값을 가져오는 쿼리를 한번 더 날려줄 필요없다.
mybatis 사용 예시
<insert id="insertRegDataList" parameterType="Popup">
/* PopupMapper.insertRegDataList */
INSERT INTO POPUP (
TYPE
, TITLE
, CONTENTS
, RELATION_URL
, RELATION_URL_TYPE
, START_DAY
, END_DAY
, REG_DT
, SHOW_YN
, ORDER_NO
)
VALUES
(
#{type}
, #{title}
, #{contents}
, #{relationUrl}
, #{relationUrlType}
, #{startDay}
, #{endDay}
, CURRENT_TIMESTAMP()
, #{showYn}
, #{orderNo}
)
<selectKey keyProperty="no" resultType="String" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
'DB > MySQL' 카테고리의 다른 글
[MySQL, MariaDB] csv 파일을 DB 테이블로 Import (0) | 2022.01.24 |
---|
댓글