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()+"=========================");

上一篇:Mybatis配置插入数据返回主键ID


下一篇:h5开发中,利用微信或者QQ登陆以后获取用户头像在canvas画布显示问题