Mybatis在oracle数据库中插入数据后返回自增值ID
1.将id设置成自增序列
CREATE OR REPLACE TRIGGER "DATALIB"."TRIG_USER_ADD"
BEFORE INSERT ON "sys_user" for each row
begin
select SEQ_USER.nextval into :new."user_id" from dual;
end;
ALTER TRIGGER "DATALIB"."TRIG_USER_ADD" ENABLE;
2.mybatis中关于数据库的xml配置文件
<insert id="save" parameterType="io.renren.entity.SysUserEntity" keyProperty="userId" databaseId="oracle">
<selectKey keyProperty="userId" resultType="long" order="BEFORE">
select SEQ_USER.NEXTVAL from dual
</selectKey>
insert into "sys_user"
(
"username",
"password",
"email",
"mobile",
"status",
"create_time"
)
values
(
#{username, jdbcType=VARCHAR},
#{password, jdbcType=VARCHAR},
#{email, jdbcType=VARCHAR},
#{mobile, jdbcType=VARCHAR},
#{status, jdbcType=NUMERIC},
#{createTime, jdbcType=DATE}
)
</insert>
3. 测试代码
SysUserEntity user=new SysUserEntity();
user.setUsername(username);
user.setPassword(passwd);
user.setEmail(email);
user.setMobile(mobile);
user.setStatus(status);
System.out.println(user.getUserId()+"888888888888888888888888");
sysUserService.save(user);
System.out.println(user.getUserId()+"=========================");