selectKey用法

<!-- My sql SEQUENCE -->
<insert id="insertUser-MY-SQL" parameterClass="user">
	INSERT INTO T_USER (name, sex) VALUES (#name#,#sex#)
	<selectKey resultClass="int" keyProperty="id">
		SELECT LAST_INSERT_ID() as id  
      </selectKey>
</insert>

<!-- Oracle SEQUENCE -->
<insert id="insertUser-ORACLE" parameterClass="user">
	<selectKey resultClass="int" keyProperty="id" type="pre">    
		<![CDATA[
		SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL
		]]>
	</selectKey> 
	<![CDATA[
	INSERT INTO t_user(id,name,sex)VALUES(#id#,#name#,#sex#)
	]]>
</insert>

<!-- Microsoft SQL Server IDENTITY Column -->
<insert id="insertUser-MS-SQL" parameterClass="user ">    
	<![CDATA[
	INSERT   INTO   T_USER   (name, sex) VALUES   (#name#,#sex#)  
	]]>
	<selectKey resultClass="int" keyProperty="id" type="post">    
		<![CDATA[
		SELECT @@IDENTITY AS ID 
		]]>
		<!-- 该方法不安全 应当用SCOPE_IDENTITY() 但这个函数属于域函数,需要在一个语句块中执行。 -->
	</selectKey>
</insert>

<!-- Microsoft SQL Server IDENTITY Column 改进 -->
<insert id="insertUser-MS-SQL" parameterClass="user">
	<selectKey resultClass="int" keyProperty="id">    
		<![CDATA[
		INSERT   INTO   T_USER   (name, sex) VALUES   (#name#,#sex#)   
		   SELECT SCOPE_IDENTITY() AS ID 
		]]>
	</selectKey>
</insert>

 

你可能感兴趣的:(select)