ibatis自动生成主键

   很多数据库支持自动生成主键的数据类型如(mysql)。不过这通常(并不总是)是个私有的特性。SQL Map通过<insert>的元素<selectKey>来支持自动生成的键值。它同时支持预生成(如Oracle)和后生成两种类型(如MS-SQL Server)。
Oracle生成自动主键的配置如下:
<insert id="insertProduct-Oracle" parameterClass="com.domain.Product">
		<selectKey resultClass="int" keyProperty="id">
			select stockidsequence.nextval as id 
			from dual
		</selectKey>
		insert into product(prd_id,prd_description)
		values(#id#,#description#)
	</insert>


MS-SQL Server生成主键的配置如下:
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
		insert into product(prd_description) values(#description#)
		<selectKey resultClass="int" keyProperty="id">
			select @@identity as id
		</selectKey>
	</insert>

你可能感兴趣的:(ibatis)