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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM