title: spring mybatis get last insert id with @Transactional
body: <p>I have two tables: User and SocialAccount. One user can have many socialAccount.</p>
  
  <p>I have to create a user before create a socialAccount:</p>
  
  <pre><code>@Transactional
  public SocialAccount create(SocialAccount socialAccount) {
      if (socialAccount.getUserId() == null) {
          User user = new User();
          userDao.createUser(user);
          System.out.println(user.getId());
  
          if(user.getId() == null){
              return null;
          }
  
          socialAccount.setUserId(user.getId());
      }
      return socialAccountDao.create(socialAccount);
  }
  </code></pre>
  
  <p>In this method, firstly i create a user, but with @Transactional I get user.getId() is null. when I delete @Transactional the user.getId() is normal.</p>
  
  <p>here is my spring config:</p>
  
  <pre><code>&lt;bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"&gt;&lt;/tx:annotation-driven&gt;
  </code></pre>
  
  <p>and here is userDao.createUser() methods:</p>
  
  <pre><code>public User createUser(User user) {
      int affectedNum = super.getSqlSession().insert("UserSQL.createUser", user);
      if (affectedNum &gt; 0) {
          return user;
      }
      return null;
  }
  </code></pre>
  
  <p>and my mapper is:</p>
  
  <pre><code>&lt;insert id="createUser" parameterType="User"&gt;
      &lt;selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" &gt;
          SELECT LAST_INSERT_ID() as id
      &lt;/selectKey&gt;
      INSERT INTO User(CTIME, UTIME) VALUES (now(), now())
  &lt;/insert&gt;
  </code></pre>
  
  <p>Can some people tell me how to get user id with @Transactional?</p>