The Selectkey element of the Ibatis sqlmap configuration file has a type attribute that allows you to specify pre or post representation before generation (pre) or the epigenetic (post).
Oracle Settings
XML code
- <!--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--
- <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]]>
- <!--This method is unsafe should be used scope_identity () but this function belongs to a domain function and needs to be executed in a block of statements. -
- </selectkey>
- </Insert>
The above MS SQL Server configuration is provided by the official website configuration, but in fact, it is just the hidden dangers! Make sure you have a valid primary key, as configured below.
XML code
- <!--Microsoft SQL Server IDENTITY Column Improved-
- <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--
- <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 () binds to the database connection and is at the same unified session level, without the MS SQL Server function problem. -
- </selectkey>
- </Insert>
In this way, it is possible to ensure that the current self-increment primary key is obtained when inserting data to the maximum extent.
Ibatis Auto-generated primary key (Oracle,ms Sql server,mysql) "Go"