iBatis Auto-Generated Keys

用了selectKey就不用通过trigger向新插入输入设置sequence值了。

还有个相关的:

http://hi.baidu.com/openj/blog/item/2ce013091c915cca3ac763f4.html

Auto-Generated Keys

Many relational database systems support auto-generation of primary key fields. This feature of the

RDBMS is often (if not always) proprietary. Data Mapper supports auto-generated keys via the

<selectKey> stanza of the <insert> element. Both pre-generated keys (e.g. Oracle) and post-generated

(MS-SQL Server) keys are supported. Here are a couple of examples:

<!—Oracle SEQUENCE Example –>

<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">

<selectKey resultClass="int" >

SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL

</selectKey>

insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)

values (#id#,#description#)

</insert>

<!— Microsoft SQL Server IDENTITY Column Example –>

<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">

insert into PRODUCT (PRD_DESCRIPTION)

values (#description#)

<selectKey resultClass="int" >

SELECT @@IDENTITY AS ID

</selectKey>

</insert>

The selectKey statement is executed before the insert statement if it is placed before the insert SQL,

otherwise the selectKey statement is executed after the insert statement. In the previous examples, the

Oracle example shows that the selectKey will be executed before the insert statement (as is appropriate for a sequence). The SQL Server example shows that the selectKey statement will be executed after the insert statement (as is appropriate for an identity column).

With iBATIS versions 2.2.0 and later, you can explicitly state the order of execution of the statements if

you wish. The selectKey element supports an attribute type that can be used to explicitly set the execution order. The value of the type attribute can be either “pre” or “post” - meaning that the statement will be executed before or after the insert statement. If you specify the type attribute, then the value you specify will be used regardless of the position of the selectKey element. For example, in the following statement the selectKey statement will be executed before the insert statement, even though the element is placed after the insert statement.

加载评论框需要翻墙