title: Get the id of last inserted record in mybatis
body: <p>I am newbie to mybatis. I am trying to get the id of last inserted record. My database is mysql and my mapper xml is </p>
  
  <pre><code>  &lt;insert id="insertSelective"  parameterType="com.mycom.myproject.db.mybatis.model.FileAttachment" &gt;
    &lt;selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" &gt;
    SELECT LAST_INSERT_ID() as id
  &lt;/selectKey&gt;
   insert into fileAttachment
  &lt;trim prefix="(" suffix=")" suffixOverrides="," &gt;
    &lt;if test="name != null" &gt;
      name,
    &lt;/if&gt;
    &lt;if test="attachmentFileSize != null" &gt;
      size,
    &lt;/if&gt;      
  &lt;/trim&gt;
  &lt;trim prefix="values (" suffix=")" suffixOverrides="," &gt;
    &lt;if test="name != null" &gt;
      #{name,jdbcType=VARCHAR},
    &lt;/if&gt;
   &lt;if test="attachmentFileSize != null" &gt;
      #{attachmentFileSize,jdbcType=INTEGER},
    &lt;/if&gt;
   &lt;/trim&gt;
   &lt;/insert&gt;
  </code></pre>
  
  <p>I thought statement written here 'SELECT LAST_INSERT_ID() as id'  should return id of last inserted record but I am getting always 1 after inserting the record.</p>
  
  <p>My mapper.java class  I have method </p>
  
  <pre><code>   int insertSelective(FileAttachment record);
  </code></pre>
  
  <p>In my dao class I am using </p>
  
  <p>int id = fileAttachmentMapper.insertSelective(fileAttachment);</p>
  
  <p>I am getting value of Id always 1 when new record is inserted. my Id field is auto incremented and records are inserting properly.</p>