在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>