iBatis自动生成的主键 很多数据库支持自动生成主键的数据类型不过这通常(并不总是)是个私有的特性 SQL Map 通过<insert>的子元素<selectKey>来支持自动生成的键值它同时支持预生成(如 Oracle)和后生成两种类型(如 MSSQL Server)下面是两个例子 < !—Oracle SEQUENCE Example > <insert id=insertProductORACLE parameterClass=comdomainProduct> <selectKey resultClass=int keyProperty=id > SELECT STOCKIDSEQUENCENEXTVAL AS ID FROM DUAL </selectKey> insert into PRODUCT (PRD_IDPRD_DESCRIPTION) values (#id##description#) </insert> <!— Microsoft SQL Server IDENTITY Column Example > <insert id=insertProductMSSQL parameterClass=comdomainProduct> insert into PRODUCT (PRD_DESCRIPTION) values (#description#) <selectKey resultClass=int keyProperty=id > SELECT @@IDENTITY AS ID </selectKey> </insert> <! Mysql 这个例子是我自己加上去的> <insert id=insertProductMysql parameterClass=comdomainProduct> insert into PRODUCT(PRD_DESCRIPTION) values (#description#) <selectKey resultClass=int keyProperty=id> SELECT LAST_INSERT_ID() </selectKey> </insert> |