mybatis 做 insert操作的时候返回插入的那条数据的id

著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
作者:吃丸子的小鹿
链接:http://www.zhihu.com/question/20810321/answer/16843223
来源:知乎

对于支持自动生成主键的数据库(如SQL Server),可以采用以下方式
<insert id="xxx" parameterType="yyy" useGeneratedKeys="true" keyProperty="id">
....
</insert>

对于不支持自动生成主键(如Oracle),可以采用以下方式
<insert id="xxx" parameterType="yyy">
  <selectKey keyProperty="id" resultType="long" order="BEFORE">
    select my_seq.nextval from dual
  </selectKey>
  ....
</insert>


著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
作者:林小米
链接:http://www.zhihu.com/question/20810321/answer/17086431
来源:知乎

useGeneratedKeys="true" 可以获取自增长的ID 只支持具有自增长方式的那种数据库(mysql, mssql 等 但 oracle 就不支持了 )
所以可以使用selectKey来获取
<insert id="xxx" parameterType="yyy" useGeneratedKeys="true">
   insert into table(...) values (...)
   <selectKey resultType="long" order="AFTER" keyProperty="id">
	SELECT LAST_INSERT_ID() AS id
   </selectKey>
</insert>
原文地址:https://www.cnblogs.com/yanduanduan/p/5159435.html