mybatis 做 insert操作的时候返回插入的那条数据的id
生活随笔
收集整理的這篇文章主要介紹了
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>
商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
作者:吃丸子的小鹿
鏈接: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>
總結
以上是生活随笔為你收集整理的mybatis 做 insert操作的时候返回插入的那条数据的id的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle——17概要文件
- 下一篇: 匿名函数预解析思考