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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM