mybatis 获取insert 返回的主键 和批量插入insert

mybatis 获取insert 返回的主键 id

<insert id="insertSelective" parameterType="com.vip.collection.manager.sms.entity.SmsTask" >
    insert into sms_task
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        `id`,
      </if>
      <if test="taskName != null" >
        `task_name`,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="taskName != null" >
        #{taskName,jdbcType=VARCHAR},
      </if>
    </trim>
    <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
            SELECT LAST_INSERT_ID() as id
        </selectKey>
  </insert>
@Override
    public Long createSelective(SmsTaskModel smsTaskModel) {
        SmsTask temp = beanMapper.map(smsTaskModel, SmsTask.class);
        temp.setId(null);
        smsTaskRepo.insertSelective(temp);
        return temp.getId();
    }

自己再把id取出来

<insert id="insertBatch">
    INSERT INTO t_user
            (id, name, del_flag)
    VALUES
    <foreach collection ="list" item="user" separator =",">
         (#{user.id}, #{user.name}, #{user.delFlag})
    </foreach >
</insert>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页