iBatis selectKey > INSERT할때 selectKey태그를 활용하여 키값 반환받기
프로그래밍/Database 2013. 4. 11. 11:43 |Many database systems support auto-generation of primary key fields, as a vendor extension. Some vendors pre-generate keys (e.g. Oracle), some vendors post-generate keys (e.g. MS-SQL Server and MySQL). In either case, you can obtain a pre-generated key using a < selectKey> stanza within an <insert> element. Example 3.7 shows an <insert> statement for either approach.
Example�3.9.�<insert> statements using <selectKey> stanzas
<!—Oracle SEQUENCE Example using .NET 1.1 System.Data.OracleClient --> <insert id="insertProduct-ORACLE" parameterClass="product"> <selectKey resultClass="int" type="pre" property="Id" > SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL </selectKey> insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values (#id#,#description#) </insert> <!— Microsoft SQL Server IDENTITY Column Example --> <insert id="insertProduct-MS-SQL" parameterClass="product"> insert into PRODUCT (PRD_DESCRIPTION) values (#description#) <selectKey resultClass="int" type="post" property="id" > select @@IDENTITY as value </selectKey> </insert> <!-- MySQL Example --> <insert id="insertProduct-MYSQL" parameterClass="product"> insert into PRODUCT (PRD_DESCRIPTION) values (#description#) <selectKey resultClass="int" type="post" property="id" > select LAST_INSERT_ID() as value </selectKey> </insert>
관련 URL
- http://whiteship.tistory.com/207
- http://ibatis.apache.org/docs/dotnet/datamapper/ch03s03.html