日期:2014-05-16  浏览次数:20408 次

oracle中主键自增设置

create table dectuser(

?????????? userid integer primary key,? /*主键,自动增加*/

?????????? name varchar2(20),

????????? ?sex varchar2(2)

?????????? );2、创建自动增长序列

????? drop sequence dectuser_tb_seq;

????? create sequence dectuser_tb_seq minvalue 1 maxvalue 99999999

?????????????? increment by 1

?????????????? start with 1;?? /*步长为1*/

?

<insert id="insert" parameterClass="cn.javass.spring.chapter7.UserModel">?
??? <selectKey resultClass="int" type="pre" keyProperty="id" >?
???????? SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL?
??? </selectKey>?
???? insert into test (id,name) values (#irrd#,#myName#)?
</insert>

?

在进行数据的插入时,有的业务需要获得相应的生成的主键值,以下是ibatis的实现方式:
有些数据库是预生成(pre-generate)主键的,如Oracle和PostgreSQL;有些是后生成(post-generate)主键的,如MySQL和SQL Server。不管是哪种方式,都可以用iBATIS的<selectkey>节点来获取<insert>语句所产生的主键。

demo如下:

xml 代码
  1. <!-- Oracle SEQUENCE Example using .NET 1.1 System.Data.OracleClient -->??
  2. <insert?id="insertProduct-ORACLE"?parameterClass="product">??
  3. ????<selectKey?resultClass="int"?type="pre"?keyProperty="id"?>??
  4. ???????? SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL??
  5. ????</selectKey>??
  6. ???? insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values (#id#,#description#)??
  7. </insert>??
  8. ??
  9. <!-- Microsoft SQL Server IDENTITY Column Example -->??
  10. <insert?id="insertProduct-MS-SQL"?parameterClass="product">??
  11. ???? insert into PRODUCT (PRD_DESCRIPTION)??
  12. ???? values (#description#)??
  13. ????<selectKey?resultClass="int"?typ