首页 > 代码库 > Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。