Ibatis中insert用法(轉)
阿新 • • 發佈:2019-02-03
在Ibatis中,insert()的返回值為一個Object的主鍵,其實這個Object的主鍵是這樣的來的:如果在bean的xml檔案中設定了插入的keyProperty,則insert()方法返回的就是這個主鍵的值。
例如,所以我們想要在插入時想要插入一個sequence值到資料庫的某個欄位(當然,這個欄位的型別為Number的),我們可以在xml檔案中做如下配置(以下為在DB伺服器是Oracle的前提下):
userId" >
SELECT user_account_s.nextval AS userid FROM
dual
insert into
user_account(userid, username, password, groupname)
values(#userId #,
#userName#, #password#, #groupName#)
此時插入到資料庫中的某表userid欄位的值即使sequence的值。但要注意的是,配置中出現的紅色加粗字型一定要對應到bean中的屬性欄位,也即要與bean中的屬性欄位名稱相同,否則則會丟擲異常。
如果是SQL SERVER資料庫,則進行如下配置:
insert
into user_account(userid, username, password, groupname)
values(#userId#,
#userName#, #password#, #groupName#)
userId" >
SELECT @@IDENTITY
as userid