민서네집

MySQL - selectKey 사용하기 본문

Database/MySQL

MySQL - selectKey 사용하기

브라이언7 2013. 5. 9. 08:31

[참고] http://www.okjsp.net/bbs?seq=137293


AUTO_INCREMENT 속성의 PK 컬럼값을 다른 컬럼에도 쓰고 싶은 경우 - 더구나 그 컬럼이 PK 컬럼을 참조하는 foreign key 인 경우, 그러면서 NOT NULL 속성으로 되어 있는 경우


<insert id="insert" parameterClass="Bean"> 

<selectKey resultClass="Integer" keyProperty="seq"> 

select ifnull(max(a),0)+1 from table 

</selectKey> 

INSERT INTO table (a, b, c) VALUES (#seq#, #b#, #seq#) 

</insert>


이런 방법이 가장 맞는것 같다.



Comments