iBatis中的insert如何返回主键ibatis insert返回
Xml代码
<!-- Oracle SEQUENCE -->
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">
<selectKey resultClass="int" keyProperty="id" type="pre">
<![CDATA[SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL]]>
</selectKey>
<![CDATA[insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values #id#,#description#)]]>
</insert>
<!-- Microsoft SQL Server IDENTITY Column -->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
<![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values (#description#) ]]>
<selectKey resultClass="int" keyProperty="id" type="post">
<![CDATA[SELECT @@IDENTITY AS ID ]]> <!-- 该方法不安全 应当用SCOPE_IDENTITY() 但这个函数属于域函数,需要在一个语句块中执行。 -->
</selectKey>
</insert>
<!-- Microsoft SQL Server IDENTITY Column 改进-->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
<selectKey resultClass="int" keyProperty="id">
<![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values (#description#)
SELECT SCOPE_IDENTITY() AS ID ]]>
</selectKey>
</insert>
<!-- Mysql Last Insert Id -->
<insert id="insertProduct-Mysql" parameterClass="com.domain.Product">
<![CDATA[insert into PRODUCT(PRD_DESCRIPTION) values (#description#)]]>
<selectKey resultClass="int" keyProperty="id">
<![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> <!-- 该方法LAST_INSERT_ID()与数据库连接绑定,不会发生上述MS SQL Server的函数问题。 -->
</selectKey>
</insert>
或推荐方式
<insert id="add" parameterType="vo.Category">
<selectKey resultType="java.lang.Short" type="POST" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
insert into category (name_zh, parent_id,
show_order, delete_status, description
)
values (#{nameZh,jdbcType=VARCHAR},
#{parentId,jdbcType=SMALLINT},
#{showOrder,jdbcType=SMALLINT},
#{deleteStatus,jdbcType=BIT},
#{description,jdbcType=VARCHAR}
)
</insert>
<!-- Oracle SEQUENCE -->
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">
<selectKey resultClass="int" keyProperty="id" type="pre">
<![CDATA[SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL]]>
</selectKey>
<![CDATA[insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values #id#,#description#)]]>
</insert>
<!-- Microsoft SQL Server IDENTITY Column -->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
<![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values (#description#) ]]>
<selectKey resultClass="int" keyProperty="id" type="post">
<![CDATA[SELECT @@IDENTITY AS ID ]]> <!-- 该方法不安全 应当用SCOPE_IDENTITY() 但这个函数属于域函数,需要在一个语句块中执行。 -->
</selectKey>
</insert>
<!-- Microsoft SQL Server IDENTITY Column 改进-->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
<selectKey resultClass="int" keyProperty="id">
<![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values (#description#)
SELECT SCOPE_IDENTITY() AS ID ]]>
</selectKey>
</insert>
<!-- Mysql Last Insert Id -->
<insert id="insertProduct-Mysql" parameterClass="com.domain.Product">
<![CDATA[insert into PRODUCT(PRD_DESCRIPTION) values (#description#)]]>
<selectKey resultClass="int" keyProperty="id">
<![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> <!-- 该方法LAST_INSERT_ID()与数据库连接绑定,不会发生上述MS SQL Server的函数问题。 -->
</selectKey>
</insert>