iBATIS自動生成主鍵
我們在數(shù)據(jù)庫插入一條數(shù)據(jù)的時候,經(jīng)常是需要返回插入這條數(shù)據(jù)的主鍵。但是數(shù)據(jù)庫供應(yīng)商之間生成主鍵的方式都不一樣。
有些是預(yù)先生成(pre-generate)主鍵的,如Oracle和PostgreSQL;有些是事后生成(post-generate)主鍵的,如MySQL和SQL Server。但是不管是哪種方式,我們都可以用iBATIS的節(jié)點來獲取語句所產(chǎn)生的主鍵。
例子如下:
xml 代碼
<insert id="insertProduct-ORACLE" parameterClass="product">
<selectKey resultClass="int" type="pre" keyProperty="Id" >
SELECT YOURPKSEQUENCE.NEXTVAL AS VALUE FROM DUAL
<selectKey>
insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values (#id#,#description#)
<insert>
<insert id="insertProduct-MS-SQL" parameterClass="product">
insert into PRODUCT (PRD_DESCRIPTION) values (#description#)
<selectKey resultClass="int" type="post" keyProperty="id" >
select @@IDENTITY as value
<selectKey>
<insert>
<insert id="insertProduct-MYSQL" parameterClass="product">
insert into PRODUCT (PRD_DESCRIPTION) values (#description#)
<selectKey resultClass="int" type="post" keyProperty="id" >
select LAST_INSERT_ID() as value
<selectKey>
<insert>
posted on 2009-06-20 13:03 suplayer 閱讀(190) 評論(0) 編輯 收藏 所屬分類: IBatis