<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); }