持之以恒

          記錄本
          posts - 4, comments - 32, trackbacks - 0, articles - 74
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          關(guān)于com.ibatis.sqlmap.client.SqlMapExecutor下的insert (java.lang.String id, java.lang.Object parameterObject) 方法,ibatis的api文檔是這樣寫的解釋的:

          Java代碼
          Executes a mapped SQL INSERT statement. Insert is a bit different from other update methods, as it provides facilities for returning the primary key of the newly inserted row (rather than the effected rows). This functionality is of course optional.

          The parameter object is generally used to supply the input data for the INSERT values.

          Parameters:
          id - The name of the statement to execute.
          parameterObject - The parameter object (e.g. JavaBean, Map, XML etc.).
          Returns:
          The primary key of the newly inserted row. This might be automatically generated by the RDBMS, or selected from a sequence table or other source.
          Throws:
          java.sql.SQLException - If an error occurs.

          Executes a mapped SQL INSERT statement. Insert is a bit different from other update methods, as it provides facilities for returning the primary key of the newly inserted row (rather than the effected rows). This functionality is of course optional.

          The parameter object is generally used to supply the input data for the INSERT values.

          Parameters:
          id - The name of the statement to execute.
          parameterObject - The parameter object (e.g. JavaBean, Map, XML etc.).
          Returns:
          The primary key of the newly inserted row. This might be automatically generated by the RDBMS, or selected from a sequence table or other source.
          Throws:
          java.sql.SQLException - If an error occurs.


          它的意思是說返回值是新插入記錄的主鍵,類型為Object主要是因為主鍵類型可以是int也可以是String類型。

          然而,如果我們使用下列的配置文件進(jìn)行插入的話,返回的值為null

          Xml代碼
          <insert id="insertPrdcategory" parameterClass="Prdcategory">
          insert into Prdcategory ( name,pid,deep ) values ( #name#,
          #pid#, #deep# )
          </insert>

          <insert id="insertPrdcategory" parameterClass="Prdcategory">
          insert into Prdcategory ( name,pid,deep ) values ( #name#,
          #pid#, #deep# )
          </insert>

          那我們要怎樣解決這個問題,讓它返回插入行的主鍵呢,這時我們就是使用到<selectKey>,下面僅以MSSQL數(shù)據(jù)庫為例,改寫上述配置文件,具體如下:



          Xml代碼
          <insert id="insertPrdcategory" parameterClass="Prdcategory">
          insert into Prdcategory ( name,pid,deep ) values ( #name#,
          #pid#, #deep# )
          <selectKey resultClass="int" keyProperty="id">
          select max(id) from Prdcategory
          </selectKey>
          </insert>

          <insert id="insertPrdcategory" parameterClass="Prdcategory">
          insert into Prdcategory ( name,pid,deep ) values ( #name#,
          #pid#, #deep# )
          <selectKey resultClass="int" keyProperty="id">
          select max(id) from Prdcategory
          </selectKey>
          </insert>


          DAO的實現(xiàn)類方法里面就應(yīng)該這樣寫了(注意整型轉(zhuǎn)換用Integer類名)

          Java代碼
          public int insertPrdcategory(Prdcategory product) throws Exception {
          return (Integer)IbatisUtil.getSqlMapper().insert("insertPrdcategory", product);
          }

          public int insertPrdcategory(Prdcategory product) throws Exception {
          return (Integer)IbatisUtil.getSqlMapper().insert("insertPrdcategory", product);
          }


          這樣就算是OK了。


          小白

          評論

          # re: 關(guān)于Ibatis insert后返回值為null的解決辦法(轉(zhuǎn))   回復(fù)  更多評論   

          2013-02-22 09:06 by 路過看看
          并發(fā)量大的時候,這正方法可以?

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 威宁| 湘潭市| 新平| 利辛县| 遂昌县| 武强县| 布拖县| 松原市| 巴南区| 台湾省| 喜德县| 沙雅县| 鲜城| 奎屯市| 花莲县| 教育| 武清区| 阜康市| 扎鲁特旗| 安新县| 曲松县| 信宜市| 塔河县| 丰原市| 怀化市| 包头市| 威远县| 莱西市| 博乐市| 龙岩市| 新丰县| 通州市| 湖南省| 娱乐| 常熟市| 新蔡县| 平远县| 康定县| 香格里拉县| 吉安县| 镇平县|