各种数据库插入数据时获得当前自增主键的方法

1. oracle数据库

    

<insert id="addRecord" parameterClass="Record">		
<selectKey keyProperty="id" resultClass="int" type="pre">
			select
			seq_record.nextval as id from dual
		</selectKey>
		insert into record(id, title, creator,create_time)
		values(#id#,#title#,#creator#,#create_time#)
</insert>
  

2. mysql数据库

 

<insert id="saveWidget" parameterClass="WidgetModel">		
    insert into widget(name, type, description)
    values(#name#, #type#, #description#)
    <selectKey resultClass="int" keyProperty="id">  
        SELECT LAST_INSERT_ID() AS ID 
    </selectKey>
</insert>

 

3. ms sql server

    

<insert id="saveWidget" parameterClass="WidgetModel">  
  <selectKey resultClass="int" keyProperty="id">    
        insert into widget(name, type, description)
        values(#name#, #type#, #description#)  
        SELECT SCOPE_IDENTITY() AS ID
    </selectKey>    
</insert>
 

你可能感兴趣的:(oracle,mysql,插入数据,自增主键,ms sql server)