mybatis新增数据后获取自增主键

时间:2021-11-10 13:38:55

mybatis对应mysql

<insert id="insert" parameterType="com.timestech.wsgk.web.model.SysOrg" useGeneratedKeys="true">           --------------即可获取service中实体类的自增ID
      
    insert into BJLT.SYS_ORG (ID, NAME, P_ID, 
      P_NAME, REMARK, LEVELS, 
      STATUS, CREATE_TIME, CREATE_ID, 
      CREATE_NAME, UPDATE_TIME, UPDATE_ID, 
      UPDATE_NAME, TREE_PATH, RESERVE1, 
      RESERVE2, RESERVE3, RESERVE4, 
      RESERVE5, IS_ROOT, IS_BUILT_IN
      )
    values (#{id,jdbcType=DECIMAL}, #{name,jdbcType=VARCHAR}, #{pId,jdbcType=DECIMAL}, 
      #{pName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{levels,jdbcType=DECIMAL}, 
      #{status,jdbcType=DECIMAL}, #{createTime,jdbcType=DATE}, #{createId,jdbcType=VARCHAR}, 
      #{createName,jdbcType=VARCHAR}, #{updateTime,jdbcType=DATE}, #{updateId,jdbcType=VARCHAR}, 
      #{updateName,jdbcType=VARCHAR}, #{treePath,jdbcType=VARCHAR}, #{reserve1,jdbcType=VARCHAR}, 
      #{reserve2,jdbcType=VARCHAR}, #{reserve3,jdbcType=VARCHAR}, #{reserve4,jdbcType=VARCHAR}, 
      #{reserve5,jdbcType=VARCHAR}, #{isRoot,jdbcType=DECIMAL}, #{isBuiltIn,jdbcType=DECIMAL}
      )
  </insert>

mybatis对应oracle

<insert id="insert" parameterType="com.timestech.wsgk.web.model.SysOrg" >
      <selectKey keyProperty="id" resultType="_long" order="BEFORE">            -------------------获取oracle的自增序列,在新增之前先赋值
        SELECT BJLT.SYS_ORG_SEQUENCE.Nextval from DUAL
      </selectKey>
    insert into BJLT.SYS_ORG (ID, NAME, P_ID, 
      P_NAME, REMARK, LEVELS, 
      STATUS, CREATE_TIME, CREATE_ID, 
      CREATE_NAME, UPDATE_TIME, UPDATE_ID, 
      UPDATE_NAME, TREE_PATH, RESERVE1, 
      RESERVE2, RESERVE3, RESERVE4, 
      RESERVE5, IS_ROOT, IS_BUILT_IN
      )
    values (#{id,jdbcType=DECIMAL}, #{name,jdbcType=VARCHAR}, #{pId,jdbcType=DECIMAL}, 
      #{pName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{levels,jdbcType=DECIMAL}, 
      #{status,jdbcType=DECIMAL}, #{createTime,jdbcType=DATE}, #{createId,jdbcType=VARCHAR}, 
      #{createName,jdbcType=VARCHAR}, #{updateTime,jdbcType=DATE}, #{updateId,jdbcType=VARCHAR}, 
      #{updateName,jdbcType=VARCHAR}, #{treePath,jdbcType=VARCHAR}, #{reserve1,jdbcType=VARCHAR}, 
      #{reserve2,jdbcType=VARCHAR}, #{reserve3,jdbcType=VARCHAR}, #{reserve4,jdbcType=VARCHAR}, 
      #{reserve5,jdbcType=VARCHAR}, #{isRoot,jdbcType=DECIMAL}, #{isBuiltIn,jdbcType=DECIMAL}
      )
  </insert>