iBatis的SqlMapClient.insert()方法的返回值
2015-08-08 20:36
507 查看
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, 不过不知道对于复合主键的情况的结果如何
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, 不过不知道对于复合主键的情况的结果如何
相关文章推荐
- JDBC连接mysql
- MongoDB下载安装
- mysql存储引擎InnoDB插入数据的过程详解
- oracle表连接——处理连接过程中另外一张表没有相关数据不显示问题
- 【MySql】使用心得小结
- Mysql驱动包版本不一致错误定位
- sql左连接 右连接 笛卡尔积 全连接 内连接一看就懂的解释
- Mysql子查询
- Oracle按不同时间分组统计
- MySQL的语句执行顺序
- redhat6.4 安装oracle 10g error
- oracle 11g导入到10g引起的错误
- mongodb 在mac下的安装配置方法
- Oracle设置和删除不可用列
- mac 下面快速安装mongodb
- MySQL常用语法
- 查看MySQL编码格式指令
- django.db.utils.OperationalError: (1049, "Unknown database "test_django_mysql")的解决办法
- 重装MySQL重装遇到的问题解决办法
- django操作mysql时django.db.utils.OperationalError: (2003, "Can't connect to MySQL server")错误的解决办法