ibatis自动生成的主键 (oracle,ms sql server,mysql)...
时间:2010-08-10 来源:kunshan_shenbin
转自: http://www.javaeye.com/topic/215571
iBatis的sqlMap配置文件的selectKey元素有个type属性,可以指定pre或者post表示前生成(pre)还是后生成(post)。
Oracle设置
- <!-- 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>
<!-- 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>
MS SQL Server配置
- <!-- 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"> <![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values(#description#) ]]> <selectKey resultClass="int" keyProperty="id" type="post"> <![CDATA[SELECT @@IDENTITY AS ID ]]> <!-- 该方法不安全 应当用SCOPE_IDENTITY() 但这个函数属于域函数,需要在一个语句块中执行。 --> </selectKey> </insert>
上述MS SQL Server配置随是官网提供的配置,但实际上却恰恰隐患重重!按下述配置,确保获得有效主键。
- <!-- 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>
<!-- 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配置
- <!-- 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>
<!-- 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>
通过以上方式,可以最大程度上确保插入数据的时候获得当前自增主键。
相关阅读 更多 +