mybatis.net insert 返回主键

mybatis insert语句

<insert id="Add" parameterClass="UserInfo" resultClass="int">
INSERT INTO UserInfo(UserName,Password,NickName) VALUES (#UserName#,#Password#,#NickName#)
</insert>

这样无法获取到插入记录的ID,如果想要获取的ID,需要用到selectKey,同时需要注意到,有些数据库支持自动增长如 (mssql和mysql),但是有些数据库(如oracle)不支持自动增长

MSSQL

<insert id="Add" parameterClass="UserInfo" resultClass="int">
INSERT INTO UserInfo(UserName,Password,NickName) VALUES (#UserName#,#Password#,#NickName#)
<selectKey type="post" property="Id" resultClass="int">
SELECT @@Identity as value
</selectKey>
</insert>

MYSQL

<insert id="Add" parameterClass="UserInfo" resultClass="int">
INSERT INTO UserInfo(UserName,Password,NickName) VALUES (#UserName#,#Password#,#NickName#)
<selectKey type="post" property="Id" resultClass="int">
SELECT LAST_INSERT_ID() as value
</selectKey>
</insert>

ORACLE

<insert id="Add" parameterClass="UserInfo">
<selectKey resultClass="int" type="pre" property="Id" >
SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL
</selectKey>
INSERT INTO UserInfo(id,UserName,Password,NickName) VALUES (#id#,#UserName#,#Password#,#NickName#)
</insert>
上一篇:Java [Leetcode 122]Best Time to Buy and Sell Stock II


下一篇:startInstrumentation asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL