让iBatis中的insert返回主键
<!-- 为了使insert操作能够返回插入记录的id,必须为insert写一个selectKey -->
<!--
下面是针对Oracle的写法,Oracle没有autoincrement,而是用触发器实现的
CURRVAL是在触发器中定义的
-->
<insert id="insertRemark" parameterClass="RemarkInfo">
insert into SGS_REMARK(REMARK) values(#remark#)
<selectKey resultClass="int" keyProperty="id" >
SELECT S_SGS_REMARK.CURRVAL AS ID FROM DUAL
</selectKey>
</insert>
<!-- 下面是针对MySQL的写法 -->
<!--
<selectKey resultClass="int" keyProperty="id" >
SELECT @@IDENTITY AS id
</selectKey>
-->
其中Mysql中也可以这么写(首推方法):
Xml代码
<insert id="tijiaoShensuQiantui" parameterClass="qiandao">
insert into ur_sign_date_shensu ( `auditing_user_id` ,`shensu_time`,`shensu_note`,`shensu_date_time`,`shensu_state`,`sign_date_id`)
values(#auditingUserId#,#shensuTime#,#shensuNote#,#shensuDateTime#,1,#id_PK#);
<selectKey resultClass="String" keyProperty="signId" >
SELECT last_insert_id() as signId ;
</selectKey>
</insert>
<insert id="tijiaoShensuQiantui" parameterClass="qiandao">
insert into ur_sign_date_shensu ( `auditing_user_id` ,`shensu_time`,`shensu_note`,`shensu_date_time`,`shensu_state`,`sign_date_id`)
values(#auditingUserId#,#shensuTime#,#shensuNote#,#shensuDateTime#,1,#id_PK#);
<selectKey resultClass="String" keyProperty="signId" >
SELECT last_insert_id() as signId ;
</selectKey>
</insert>
signId必须是qiandao这个bean里的属性