<insert id="insertStudents" useGeneratedKeys="true" keyProperty="studId" parameterType="java.util.ArrayList"> INSERT INTO STUDENTS(STUD_ID, NAME, EMAIL, DOB, PHONE) VALUES <foreach collection="list" item="item" index="index" separator=","> (#{item.studId},#{item.name},#{item.email},#{item.dob}, #{item.phone}) </foreach> </insert>
Mapper.xml中keyProperty和parameterType属性之间的关系java
useGeneratedKeys="true" keyProperty="studId" parameterType="Student"
上述xml配置,含义为,属性studId是参数类型Student对象的主键属性。毫无疑问,Student对象中有studId属性。app
useGeneratedKeys="true" keyProperty="studId" parameterType="java.util.ArrayList"
含义为:ArrayList集合中的元素的studId属性。因此,keyProperty和parameterType之间的关系,有时是直接关系,有时是间接关系。明白这个道理以后,咱们就能够开始进一步阅读源码了。spa
SimpleExecutor和ReuseExecutor能够正确返回foreach批量插入后的id列表code
配置Executorxml
<setting name="defaultExecutorType" value="SIMPLE" />
public interface UserMapper { @Insert("insert into tbl_user (name, age) values (#{name}, #{age})") @Options(useGeneratedKeys=true, keyProperty="userId", keyColumn="id") void insertUser(User user); }