똑같은 삽질은 2번 하지 말자
방금 INSERT 된 Primary Key 가져오기(MyBatis) 본문
insert 된 그 PK를 다른테이블에서 써야할 때
<insert id="insertData" parameterType="DataClass" useGeneratedKeys="true" keyProperty="id">
/* query */
<selectKey keyProperty="id" resultClass="int">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
id 값을 리턴 받는 곳은
DAO 클래스에서 insert문을 호출하는 구문에서 값을 리턴 받으면 된다.
int id = commonDB.insert(NAMESPACE + "insertData", dataClss);
ex)
<mapper namespace="com.gwak.mapper.PayMapper">
<insert id="save" parameterType="map">
INSERT INTO RYOKAN_PAYMENT(MBER_ID,P_PRICE) VALUES(#{mber_id},#{p_price})
<selectKey keyProperty="p_code" resultType="Integer">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
</mapper>
'DB' 카테고리의 다른 글
MySQL>Client does not support authentication protocol requested by server 에러 (0) | 2021.03.21 |
---|---|
SQL 실행순서 / 쿼리 실행 과정 (0) | 2020.07.13 |
JDBC & Mybatis (0) | 2020.02.09 |
MariaDB 와 MySQL (0) | 2020.01.02 |
CREATE TABLE ~ (0) | 2019.07.05 |
Comments