---
title: "ibatis自动生成主键"
author: "chenqian"
date: "2009-03-04T16:45:54Z"
tags: []
slug: "ibatis"
draft: false
lastmod: "2009-03-04T16:45:54Z"
id: 343049
url: "https://chenqian.iteye.com/blog/343049"
---

转自互联网：

ibatis自动生成主键：


```xml
    <!-- Oracle SEQUENCE Example using .NET 1.1 System.Data.OracleClient -->  
   <insert id="insertProduct-ORACLE" parameterClass="product">  
        <selectKey resultClass="int" type="pre" keyProperty="id" >  
            SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE 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="product">  
       insert into PRODUCT (PRD_DESCRIPTION)  
       values (#description#)  
       <selectKey resultClass="int" type="post" keyProperty="id" >  
           select @@IDENTITY as value  
       </selectKey>  
   </insert>  
     
   <!-- MySQL Example -->  
   <insert id="insertProduct-MYSQL" parameterClass="product">  
       insert into PRODUCT (PRD_DESCRIPTION)  
       values (#description#)  
       <selectKey resultClass="int" type="post" keyProperty="id" >  
           select LAST_INSERT_ID() as value  
       </selectKey>  
   </insert>  
```