一般主键生成无需人为来控制 ibatis之selectkey 而由数据库自动处理。但执行 insert 操作以后,程序没有返回本次操作插入记录的主键值。以下是 Oracle 和 SQL Server 主键生成方法:
< !- Oracle ->
<insert id="insertProduct-ORACLE" parameterClass="com.domain.demo">
<selectKey resultClass="int" keyProperty="id" >
SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL --dual是个零时表
</selectKey>
insert into demo(demo_id,demo_text) values (#id#,#text#)
</insert>
<!- Microsoft SQL Server ->
<selectKey resultClass="int" keyProperty="id" >
SELECT @@IDENTITY AS ID
</selectKey>
<!- My Sql ->
<selectKey resultClass="int" keyProperty="id">
SELECT LAST_INSERT_ID() AS ID
</selectKey>