<insert id="abatorgenerated_insert" parameterClass="net.xxxxxl.dataobject.Users"> <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Fri Aug 21 21:21:33 CST 2015. --> insert into Users (user_Pwd, user_Nickname, user_Firstname, user_Lastname, user_Sex, user_Birthday, FK_City, .... user_LoginState, user_Collecter, user_Collected, user_lang, user_login_fail_times, user_Money, weixin_openid) values (#user_Pwd:VARCHAR#, #user_Nickname:VARCHAR#, #user_Firstname:VARCHAR#, #user_Lastname:VARCHAR#, #user_Sex:CHAR#, #user_Birthday:VARCHAR#, #FK_City:BIGINT#, ..... #user_login_fail_times:INTEGER#, #user_Money:DOUBLE#, #weixin_openid:VARCHAR#) <selectKey keyProperty="userId" resultClass="java.lang.Long"> SELECT SCOPE_IDENTITY() </selectKey> </insert>
上面是 abator 生成的对应于ms sql server 的insert 代码,实际运行时,无法获取 插入记录对应的 id 值,虽然里面包含了:
<selectKey keyProperty="userId" resultClass="java.lang.Long"> SELECT SCOPE_IDENTITY() </selectKey>
正确的方法是,让 <selectKey> 子句包含住整个 insert 语句,如下所示:
<insert id="abatorgenerated_insert" parameterClass="net.xxxxxx.dal.dataobject.Users"> <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Fri Aug 21 21:21:33 CST 2015. --> <selectKey keyProperty="userId" resultClass="java.lang.Long"> insert into Users (user_Pwd, user_Nickname, user_Firstname, user_Lastname, user_Sex, user_Birthday, ..... weixin_openid) values (#user_Pwd:VARCHAR#, #user_Nickname:VARCHAR#, #user_Firstname:VARCHAR#, ...... #user_login_fail_times:INTEGER#, #user_Money:DOUBLE#, #weixin_openid:VARCHAR#) SELECT SCOPE_IDENTITY() </selectKey> </insert>
这样才能正确的获取 insert 语句执行时对应的 id 主键值。