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>