mybatis 插入数据返回主键到对象, oracle 和 mysql 的区别

mysql的写法,注意只需要写 useGeneratedKeys=“true” keyProperty=“id” 就可以把id返回到对象的id中去

<insert id="insertSelective" parameterType="com.yuan.ngu.springbootmybatis.model.User" useGeneratedKeys="true" keyProperty="id"  >
  insert into user
  <trim prefix="(" suffix=")" suffixOverrides="," >
    <if test="id != null" >
      id,
    </if>
    <if test="name != null" >
      name,
    </if>
    <if test="address != null" >
      address,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides="," >
    <if test="id != null" >
      #{id,jdbcType=INTEGER},
    </if>
    <if test="name != null" >
      #{name,jdbcType=VARCHAR},
    </if>
    <if test="address != null" >
      #{address,jdbcType=VARCHAR},
    </if>
  </trim>
</insert>

oracle写法(注意,在oracle中写useGeneratedKeys=“true” keyProperty=“id” ,并没有效果),oracle 的做法是执行插入前先获取一个id,然后当做一个参数传到了对象中.

<insert id="insertSelective" parameterType="com.ule.pcs.model.PcsRetailFundInfo" >
  <selectKey keyProperty="id" resultType="string" order="BEFORE">
    select SEQ_PCS_RETAIL_FUND_INFO.NEXTVAL from dual
  </selectKey>
  insert into PCS_RETAIL_FUND_INFO
  <trim prefix="(" suffix=")" suffixOverrides="," >
    ID,
    <if test="merchantId != null" >
      MERCHANT_ID
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides="," >
    #{id,jdbcType=VARCHAR},
    <if test="merchantId != null" >
      #{merchantId,jdbcType=VARCHAR}
    </if>
  </trim>
</insert>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值