如果批量插入需要返回自增ID,需要满足4个条件:

  1. mybatis的版本需要在3.3.1以上
  2. batchInsert方法参数中不能含有@param注解
  3. batchInsert方法参数中只能有一个参数,那就是需要插入的List
  4. batchInsert的返回值不能是List

下面说给出一个例子:

/**
 * @author: dwtfukgv
 * @version: 1.0
 * @date: 2020/12/29 8:42 下午
 */
public interface QuestionDiyMapper {

    /**
     * 批量插入
     * @param list 需要插入的PO
     */
    void batchInsert(List<QuestionPO> list);
}

<insert >
  insert into `tb_net_course_question` (
  course_id, description
  ) values
  <foreach collection="list" item="item" separator=",">
    (#{item.courseId,jdbcType=BIGINT}, #{item.description,jdbcType=LONGVARCHAR})
  </foreach>
</insert>

上面的keyColumn是数据库中对应的字段名称,keyProperty是实体类中对应的字段名称

相关文章: