From: http://www.javaeye.com/topic/215571
In the sqlmap configuration file of ibatisSelectkeyElement hasTypeAttribute, which can be specifiedPreOrPostIndicates that (Pre) Or generate later (Post).
Oracle settings
XML Code
- <! -- Oracle sequence -->
- <InsertId = "insertproduct-Oracle" parameterclass = "com. domain. Product">
- <SelectkeyResultclass = "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 Configuration
XML Code
- <! -- Microsoft SQL Server identity column -->
- <InsertId = "insertproduct-MS-SQL" parameterclass = "com. domain. Product">
- <! [CDATA [insert into product (prd_description) values (# description #)]>
- <SelectkeyResultclass = "int" keyproperty = "ID" type = "Post">
- <! [CDATA [select @ identity as ID]>
- <! -- Scope_identity () should be used for this method, but this function belongs to a domain function and needs to be executed in a statement block. -->
- </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 () should be used for this method, but this function belongs to a domain function and needs to be executed in a statement block. --> </Selectkey> </Insert>
The above ms SQL Server configuration is provided on the official website, but in fact it is quite risky! Use the following configurations to ensure that a valid primary key is obtained.
XML Code
- <! -- Microsoft SQL Server identity column Improvement -->
- <InsertId = "insertproduct-MS-SQL" parameterclass = "com. domain. Product">
- <SelectkeyResultclass = "int" keyproperty = "ID">
- <! [CDATA [insert into product (prd_description) values (# description #)
- Select scope_identity () as ID]>
- </Selectkey>
- </Insert>
<! -- Microsoft SQL Server identity column Improvement --> <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 Configuration
XML Code
- <! -- MySQL last insert id -->
- <InsertId = "insertproduct-mysql" parameterclass = "com. domain. Product">
- <! [CDATA [insert into product (prd_description) values (# description #)]>
- <SelectkeyResultclass = "int" keyproperty = "ID">
- <! [CDATA [select last_insert_id () as ID]>
- <! -- The method last_insert_id () is bound to a database connection and belongs to the same unified session level. The preceding function problem of ms SQL Server will not occur. -->
- </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]> <! -- The method last_insert_id () is bound to a database connection and belongs to the same unified session level. The preceding function problem of ms SQL Server will not occur. --> </Selectkey> </Insert>
The above method ensures that the current auto-increment primary key is obtained when data is inserted to the maximum extent.