Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)
Posted 邱明成
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)相关的知识,希望对你有一定的参考价值。
1.通过Oracle序列
- -- Create sequence
- create sequence SEQ_DW_EWSYSTEM
- minvalue 1
- maxvalue 999999999999999999999999999
- start with 1
- increment by 1
- cache 20;
- <insert id="insertEwsystem" parameterType="Ewsystem">
- <selectKey keyProperty="id" resultType="long" order="BEFORE">
- SELECT SEQ_EWSYSTEM.NEXTVAL FROM DUAL <!-- SELECT DECODE(MAX(ID),NULL,0,MAX(ID))+1 FROM EWSYSTEM -->
- </selectKey>
- INSERT INTO EWSYSTEM (ID,ORG_ID,NAME)VALUES(#{id},#{orgId,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR})
- </insert>
2.通过Oracle触发器
- create or replace trigger TRG_DW_EWSYSTEM
- before insert
- on DW_EWSYSTEM for each row
- begin
- select SEQ_DW_EWSYSTEM.nextval into :new.id from dual;
- end;
- <insert id="insertEwsystem" parameterType="Ewsystem">
- <selectKey resultType="int" keyProperty="id" order="AFTER">
- SELECT SEQ_DW_EWSYSTEM.CURRVAL FROM DUAL
- </selectKey>
- INSERT INTO EWSYSTEM (ID,ORG_ID,NAME)VALUES(#{id},#{orgId,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR})
- </insert>
以上是关于Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)的主要内容,如果未能解决你的问题,请参考以下文章