Ibatis Auto-generated primary key (Oracle,ms Sql server,mysql) "Go"

Source: Internet
Author: User
Tags cdata

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
  1. <!--Oracle SEQUENCE--
  2. <insert id="insertproduct-oracle" parameterclass="com.domain.Product">
  3. <selectkey resultclass="int" keyproperty="id" type="pre">
  4. <! [Cdata[select stockidsequence. Nextval as ID from Dual]]>
  5. </selectkey>
  6. <! [Cdata[insert into PRODUCT (prd_id,prd_description) VALUES (#id #, #description #)]]>
  7. </Insert>


MS SQL Server Configuration

XML code
  1. <!--Microsoft SQL Server IDENTITY Column--
  2. <insert id="Insertproduct-ms-sql" parameterclass="com.domain.Product">
  3. <! [Cdata[insert into PRODUCT (prd_description) VALUES (#description #)]]>
  4. <selectkey resultclass="int" keyproperty="id" type="POST">
  5. <! [Cdata[select @ @IDENTITY as ID]]>
  6. <!--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. -
  7. </selectkey>
  8. </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
  1. <!--Microsoft SQL Server IDENTITY Column Improved-
  2. <insert id="Insertproduct-ms-sql" parameterclass="com.domain.Product">
  3. <selectkey resultclass="int" keyproperty="id">
  4. <! [Cdata[insert into PRODUCT (prd_description) VALUES (#description #)
  5. SELECT scope_identity () as ID]]>
  6. </selectkey>
  7. </Insert>


MySQL Configuration

XML code
  1. <!--MySQL last Insert Id--
  2. <insert id="Insertproduct-mysql" parameterclass="com.domain.Product">
  3. <! [Cdata[insert into PRODUCT (prd_description) VALUES (#description #)]]>
  4. <selectkey resultclass="int" keyproperty="id">
  5. <! [Cdata[select last_insert_id () as ID]]>
  6. <!--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. -
  7. </selectkey>
  8. </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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.