这个问题困扰小白很久了,网上说什么order="BEFORE"或order="AFTER"搞得我就没成功过,现在把这个坑填一下!
1、首先说说Oracle数据库中建表、建序列、建触发器,让ID自增。
(1)、建表,代码如下:
create table USERS
(
id Integer not null,
username VARCHAR2(32) not null,
password VARCHAR2(32) not null,
createtime date,
updatetime date,
rel_name VARCHAR2(32),
telphone VARCHAR2(20),
login_nums Integer default 0 not null,
last_login_time date,
ststus number(2) default 1 not null,
roles_id Integer
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table USERS
is '用户表';
-- Add comments to the columns
comment on column USERS.username
is '用户名';
comment on column USERS.createtime
is '创建时间';
comment on column USERS.updatetime
is '更新时间';
comment on column USERS.rel_name
is '真实姓名';
comment on column USERS.telphone
is '联系电话';
comment on column USERS.login_nums
is '登陆次数';
comment on column USERS.last_login_time
is '最后一次登陆时间';
comment on column USERS.ststus
is '1-启动 0-禁用';
comment on column USERS.roles_id
is '角色id';
-- Create/Recreate primary, unique and foreign key constraints
alter table USERS
add constraint USERS_ID primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
alter table USERS
add constraint ROLE_ID foreign key (ROLES_ID)
references ROLES (ID);
(2)、创建名为users_seq_id的序列,代码如下:
不设置最大值:create sequence users_seq_id minvalue 1 nomaxvalue start with 1 increment by 1;
设置最大值:将nomaxvalue改为:maxvalue 最大值
序列的其他用法 :
查询序列:select 序列名.nextval from dual;
修改序列:
alter sequence 序列名
increment by 1;
alter sequence 序列名
increment by 1;
COMMIT ;
删除序列:drop sequence 序列名;
(3)、新建名为:T_users的触发器
代码如下:
create or replace trigger T_users
before insert
on users
for each row
declare
-- local variables here
begin
select users_seq_id.nextval into:new.id from dual;
end T_users;
2、插入数据,返回ID问题,首先在usersMapper.xml中写SQL语句:
3、在dao层的usersMapper.java中添加如下方法,返回int类型
4、在Service接口(IUsersService.java)中添加方法
public int addUsers(Users users);
5、在Service接口实现类(UsersServiceImpl.java)中添加方法
@Override
public int addUsers(Users users) {
return usersMapper.insertSelective(users);
}
6、控制器:
7、插入数据,测试结果
不足之处,还请大家留言,谢谢!