mybatis使用oracle自动生成主键

下面总结了两种方式,一种是使用oracle的sys_guid函数自动生成,另一种是使用sequence,

 方法一:
  <insert id="insert" useGeneratedKeys="false" keyProperty="storeId" parameterType="zttc.itat.user.po.TStore" >
   <selectKey resultType="String"  keyProperty="storeId" order="BEFORE">
   select sys_guid() from dual
 </selectKey>
    insert into T_STORE (STORE_ID, STORE_NAME, STORE_PRICE,CREATE_DATE,END_DATE
      )
    values ( #{storeId,jdbcType=VARCHAR}, #{storeName,jdbcType=VARCHAR}, #{storePrice,jdbcType=DECIMAL},
      #{createDate,jdbcType=VARCHAR},#{endDate,jdbcType=VARCHAR}
      )
  </insert>
 

 方法二:

 <insert id="insert" parameterType="zttc.itat.user.po.TStore" >
    insert into T_STORE (STORE_ID, STORE_NAME, STORE_PRICE,CREATE_DATE,END_DATE
      )
    values (my_sequence.nextval, #{storeName,jdbcType=VARCHAR}, #{storePrice,jdbcType=DECIMAL},
      #{createDate,jdbcType=VARCHAR},#{endDate,jdbcType=VARCHAR}
      )
  </insert>
 
  <insert id="insert" useGeneratedKeys="false" keyProperty="storeId" parameterType="zttc.itat.user.po.TStore" >
   <selectKey resultType="String"  keyProperty="storeId" order="BEFORE">
  select my_sequence.nextval as storeId from dual
 </selectKey>
    insert into T_STORE (STORE_ID, STORE_NAME, STORE_PRICE,CREATE_DATE,END_DATE
      )
    values ( #{storeId,jdbcType=VARCHAR}, #{storeName,jdbcType=VARCHAR}, #{storePrice,jdbcType=DECIMAL},
      #{createDate,jdbcType=VARCHAR},#{endDate,jdbcType=VARCHAR}
      )
  </insert>
 

 

posted @ 2016-05-17 10:24  ycblus  阅读(885)  评论(0编辑  收藏  举报