主键自增的操作

 

 

在mysql中

create table Student(
 Student_ID  int(6) NOT NULL PRIMARY KEY AUTO_INCREMENT,
 Student_Name varchar(10) NOT NULL,
 Student_Age int(2) NOT NULL
);
 
insert into student(student_name,student_age) values('zhangsan',20);

在oracle中

create table Student(
 Student_ID  number(6) NOT NULL PRIMARY KEY,
 Student_Name varchar2(10) NOT NULL,
 Student_Age number(2) NOT NULL
);

而oracle如果想设置主键自增长,则需要创建序列

CREATE SEQUENCE student_sequence
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
CACHE 10;
 
insert into Student values(student_sequence.nextval,'aa',20);

 

Mybatis插入时候获取自增主键方法有二

方法1

<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
        insert into person(name,pswd) values(#{name},#{pswd})
</insert> 

 

方法2

<insert id="insert" parameterType="Person">
        <selectKey keyProperty="id" resultType="long">
            select LAST_INSERT_ID()
        </selectKey>
        insert into person(name,pswd) values(#{name},#{pswd})
</insert>

原文:https://www.cnblogs.com/sharpest/p/10162533.html

posted @ 2020-08-04 19:35  从来没有平凡的时刻  阅读(540)  评论(0编辑  收藏  举报