Mybatis批量插入返回主键

插入返回主键:

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

批量插入返回主键:

 /**
  * 批量插入
  * @param optionList
  * @return
  */
  int addBatch(List<TbSpecificationOption> optionList);


	<insert id="addBatch" useGeneratedKeys="true" keyProperty="id">
		insert into tb_specification_option (id, option_name, spec_id,
		orders)
		values
		<foreach collection="list" item="item" separator=",">
			(#{item.id},
			#{item.optionName}, #{item.specId}, #{item.orders})
		foreach>
	insert>

重点:
useGeneratedKeys="true" keyProperty="id" collection="list"

你可能感兴趣的:(项目相关)