• ms sql server 在abator生成的 insert 无法获取插入 id 的原因


      <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 主键值。

  • 相关阅读:
    Linux下Tomcat服务器-maven项目部署
    数据库设计感悟
    数据库设计规范
    从零到一: 代码调试
    Java泛型与反射的综合应用
    Eclipse中,tomcat插件方式启动项目
    集合查询表--Map
    集合线性表--List之LinkedList(队列与栈)
    集合线性表--List之ArrayList
    Java中的日期操作
  • 原文地址:https://www.cnblogs.com/digdeep/p/4752451.html
Copyright © 2020-2023  润新知