--使用自增功能
id number(11) generated by default as IDENTITY,
...
--首先建一个表USER_INFO
create table USER_INFO
(
ID number(11) not null
constraint USER_INFO_PK
primary key,
NAME VARCHAR2(20) not null,
ADDRESS VARCHAR2(30),
SEX VARCHAR2(4) not null
)
-- 再建一个序列SEQ_TEST
create sequence SEQ_TEST
minvalue 1 -- 最小值
nomaxvalue -- 不设置最大值
start with 1 -- 从1开始计数
increment by 1 -- 每次加1个
nocycle -- 一直累加,不循环
nocache;
-- 不建缓冲区 cache n / nocache --其中n代表一个整数,默认值为20
--------------------------------------------
create sequence product_id_seq increment by 1 start with 1;
--------------------------------------------
-- 触发器可省略,省略情况下用序列名.nextval获取id:
insert into USER_INFO (ID, NAME, ADDRESS, SEX)
values (SEQ_TEST.nextval, '吴木木', '兰州', '男生');
-- 创建一个触发器
CREATE
OR REPLACE TRIGGER tg_test /*触发器*/
BEFORE INSERT ON TEST_tab /*表*/ FOR EACH ROW /* WHEN (new.nid is null)*/
begin
select /*序列*/seq_test.nextval
into :new.nid/*主键*/
from dual;
-- select /*序列*/seq_test.nextval into:new.id/*主键*/ from dual;
end;
-- 配置
@Configuration
public class MybatisPlusConfig {
@Bean
public OracleKeyGenerator oracleKeyGenerator(){
return new OracleKeyGenerator();
}
}
@Data
@Accessors(chain = true)
@TableName(value = "USER_INFO")
//序列名
@KeySequence(value = "SEQ_TEST")
public class UserInfo implements Serializable {
private static final long serialVersionUID = 929439337157294297L;
@TableId(value = "ID",type = IdType.INPUT)
private long id;
private String name;
private String address;
private String sex;
}