【无标题】

<selectKey>标签详解

<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
    SELECT LAST_INSERT_ID()
</selectKey>

今天在阅读别人写好的Sql中,突然看到在insert语句中,突然出现了如上图所示的一个sql。
这个sql,望文生义,是要查询最后插入的ID,后来百度检索之后,将内容写下来。

参数详情如下:

resultType:主键的数据类型

keyProperty:主键的名称(与数据库对应)

order:有两种主键自增写
AFTER表示SELECT LASTINSERTID() 在insert执行之后执行,用于获取自增的主键值。
BEFORE表示SELECT LASTINSERTID() 在insert执行之前执行,这样的话就拿不到主键了,这种适合那种主键不是自增的类型resultType 主键类型

业务sql如下:

<insert id="insert" parameterType="com.macro.mall.model.OmsOrder">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into oms_order (member_id, coupon_id, order_sn, 
      create_time, member_username, total_amount, 
      pay_amount, freight_amount, promotion_amount, 
      integration_amount, coupon_amount, discount_amount, 
      pay_type, source_type, status, 
      order_type, delivery_company, delivery_sn, 
      auto_confirm_day, integration, growth, 
      promotion_info, bill_type, bill_header, 
      bill_content, bill_receiver_phone, bill_receiver_email, 
      receiver_name, receiver_phone, receiver_post_code, 
      receiver_province, receiver_city, receiver_region, 
      receiver_detail_address, note, confirm_status, 
      delete_status, use_integration, payment_time, 
      delivery_time, receive_time, comment_time, 
      modify_time)
    values (#{memberId,jdbcType=BIGINT}, #{couponId,jdbcType=BIGINT}, #{orderSn,jdbcType=VARCHAR}, 
      #{createTime,jdbcType=TIMESTAMP}, #{memberUsername,jdbcType=VARCHAR}, #{totalAmount,jdbcType=DECIMAL}, 
      #{payAmount,jdbcType=DECIMAL}, #{freightAmount,jdbcType=DECIMAL}, #{promotionAmount,jdbcType=DECIMAL}, 
      #{integrationAmount,jdbcType=DECIMAL}, #{couponAmount,jdbcType=DECIMAL}, #{discountAmount,jdbcType=DECIMAL}, 
      #{payType,jdbcType=INTEGER}, #{sourceType,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, 
      #{orderType,jdbcType=INTEGER}, #{deliveryCompany,jdbcType=VARCHAR}, #{deliverySn,jdbcType=VARCHAR}, 
      #{autoConfirmDay,jdbcType=INTEGER}, #{integration,jdbcType=INTEGER}, #{growth,jdbcType=INTEGER}, 
      #{promotionInfo,jdbcType=VARCHAR}, #{billType,jdbcType=INTEGER}, #{billHeader,jdbcType=VARCHAR}, 
      #{billContent,jdbcType=VARCHAR}, #{billReceiverPhone,jdbcType=VARCHAR}, #{billReceiverEmail,jdbcType=VARCHAR}, 
      #{receiverName,jdbcType=VARCHAR}, #{receiverPhone,jdbcType=VARCHAR}, #{receiverPostCode,jdbcType=VARCHAR}, 
      #{receiverProvince,jdbcType=VARCHAR}, #{receiverCity,jdbcType=VARCHAR}, #{receiverRegion,jdbcType=VARCHAR}, 
      #{receiverDetailAddress,jdbcType=VARCHAR}, #{note,jdbcType=VARCHAR}, #{confirmStatus,jdbcType=INTEGER}, 
      #{deleteStatus,jdbcType=INTEGER}, #{useIntegration,jdbcType=INTEGER}, #{paymentTime,jdbcType=TIMESTAMP}, 
      #{deliveryTime,jdbcType=TIMESTAMP}, #{receiveTime,jdbcType=TIMESTAMP}, #{commentTime,jdbcType=TIMESTAMP}, 
      #{modifyTime,jdbcType=TIMESTAMP})
  </insert>

实际业务场景
在这里插入图片描述
这里就是当执行完第一部分(第一个表)插入操作之后,理论上会自动生成自增的主键Id,但是后续的第二部分(第二个表)插入操作,需要插入第一部分(第一个表的Id)。原则上,我们需要再次查询第一个表,获取到Id,在给予第二个表去插入。此方法过于麻烦。
所以这个标签的作用,就是解决上述麻烦操作的。添加上这个sql标签之后,如果第二部分需要获取第一个表插入的Id,可以通过直接getId()方法,获取到所需要的Id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值