title: Mybatis Use generated keys for Batch Insert
body: <p>I have done batch inserting in Mybatis and it is working fine. But I'm not sure how to store the generated primary keys for each row in the bean class. Here is my code,</p>
  
  <p>Mapper.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;
      &lt;mapper namespace="com.xxxx.sample.test.dao.TestDAO"&gt;
          &lt;insert id="insertEmployeeList" parameterType="java.util.List"&gt;
              INSERT ALL
              &lt;foreach collection="list" item="element" index="index"&gt;
                  INTO EMPLOYEE (name) values (#{element.name})
              &lt;/foreach&gt;
              SELECT * FROM dual
          &lt;/insert&gt;
      &lt;/mapper&gt;
  </code></pre>
  
  <p>Emp.java</p>
  
  <pre><code>public class Emp {
  public Emp(int id, String name) {
  this.id = id;
  this.name = name;
  }
  private int id;
  private String name;
  public int getId() {
  return id;
  }
  public void setId(int id) {
  this.id = id;
  }
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  }
  </code></pre>
  
  <p>Employee.java</p>
  
  <pre><code>public class Employee {
  private List&lt;Emp&gt; list = new ArrayList&lt;Emp&gt;();
  public List&lt;Emp&gt; getList() {
  return list;
  }
  public void setList(List&lt;Emp&gt; list) {
  this.list = list;
  }
  }
  </code></pre>
  
  <p>In the above example Employee is the object to be persisted in database which contains list of Emp.</p>