iBatis的SqlMapClient.insert()方法的返回值

Object com.ibatis.sqlmap.client.SqlMapExecutor.queryForObject(String id, Object parameterObject) throws SQLException

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. 
(这个方法返回的是一个主键object)
Throws: 
java.sql.SQLException If an error occurs.
====看下例子=======================

<!-- Insert example, using the Account parameter class -->
  <insert id="insertAccount" parameterClass="Account">
    insert into ACCOUNT (
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL)
    values (
      #firstName#, #lastName#, #emailAddress#
    )
  </insert>

  public static Object insertAccount (Account account) throws SQLException {
    return sqlMapper.insert("insertAccount", account);
  }

实际上, insertAccount 返回的总是一个null。

原来用法是这样的:

<!-- Insert example, using the Account parameter class -->
  <insert id="insertAccount" parameterClass="Account">
    insert into ACCOUNT (
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL)
    values (
      #firstName#, #lastName#, #emailAddress#
    )
   <selectKey resultClass="int" keyProperty="id" > 
   SELECT @@IDENTITY AS ID 
  </selectKey> 
  </insert>
关键在嵌套的那句selectKey -

我得表的主键是ID,所以返回的是一个Integer对象,其值就是插入的那个account的id, 不过不知道对于复合主键的情况的结果如何

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值