MyBatis中插入并返回主键

开发过程中经常遇到需要插入一条数据,并且返回这条数据自增的主键,在MyBatis中只需要在mapper中添加keyProperty属性即可

 

 在mapper中添加keyProperty属性

 <insert id="insertAndGetId" parameterType="com.lili.log.dto.LogPay" useGeneratedKeys="true" keyProperty="payId" >
    insert into t_log_pay (payId, orderId, waterNum, 
      studentId, coachId, payWay, 
      payMoney, couponMoney, couponId, 
      payTime)
    values (#{payid,jdbcType=INTEGER}, #{orderid,jdbcType=VARCHAR}, #{waternum,jdbcType=VARCHAR}, 
      #{studentid,jdbcType=BIGINT}, #{coachid,jdbcType=BIGINT}, #{payway,jdbcType=VARCHAR}, 
      #{paymoney,jdbcType=DOUBLE}, #{couponmoney,jdbcType=DOUBLE}, #{couponid,jdbcType=INTEGER}, 
      #{paytime,jdbcType=TIMESTAMP})
  </insert>

你可能感兴趣的:(MyBatis中插入并返回主键)