Mybatis在oracle数据库中插入数据后返回自增值ID

时间:2017-09-03 20:09:34   收藏:0   阅读:551

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

原文:http://www.cnblogs.com/ninicwang/p/7470646.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!