[ibatis]关于Ibatis insert后返回值为null的解决办法(转)
2012-12-10 21:32
337 查看
原文地址:http://www.blogjava.net/baizhihui19870626/articles/389469.html
关于com.ibatis.sqlmap.client.SqlMapExecutor下的insert (java.lang.String id, java.lang.Object parameterObject) 方法,ibatis的api文档是这样写的解释的:
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类型。
然而,如果我们使用下列的配置文件进行插入的话,返回的值为null
Xml代码
<insert id="insertPrdcategory" parameterClass="Prdcategory">
insert into Prdcategory ( name,pid,deep ) values ( #name#,
#pid#, #deep# )
</insert>
那我们要怎样解决这个问题,让它返回插入行的主键呢,这时我们就是使用到<selectKey>,下面仅以MSSQL数据库为例,改写上述配置文件,具体如下:
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>
DAO的实现类方法里面就应该这样写了(注意整型转换用Integer类名)
Java代码
public int insertPrdcategory(Prdcategory product) throws Exception {
return (Integer)IbatisUtil.getSqlMapper().insert("insertPrdcategory", product);
}
关于com.ibatis.sqlmap.client.SqlMapExecutor下的insert (java.lang.String id, java.lang.Object parameterObject) 方法,ibatis的api文档是这样写的解释的:
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类型。
然而,如果我们使用下列的配置文件进行插入的话,返回的值为null
Xml代码
<insert id="insertPrdcategory" parameterClass="Prdcategory">
insert into Prdcategory ( name,pid,deep ) values ( #name#,
#pid#, #deep# )
</insert>
那我们要怎样解决这个问题,让它返回插入行的主键呢,这时我们就是使用到<selectKey>,下面仅以MSSQL数据库为例,改写上述配置文件,具体如下:
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>
DAO的实现类方法里面就应该这样写了(注意整型转换用Integer类名)
Java代码
public int insertPrdcategory(Prdcategory product) throws Exception {
return (Integer)IbatisUtil.getSqlMapper().insert("insertPrdcategory", product);
}
相关文章推荐
- 关于ibatis insert 返回值为 null的研究----得到插入数据的逐渐值
- ibatis 关于空值(NULL)的解决办法
- ibatis中使用insert后,返回值为null的解决方案
- 关于dubbo调度时出现Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method insertTestTb in the service cn.cuibusi.core.service.TestTbService.的解决办法
- 关于SQL中使用select sum查询结果为NULL的解决办法
- 关于oracle plsql数据库ORA-01480: STR 绑定值的结尾 Null 字符缺失 的问题解决办法。
- 关于SQL语句中SUM函数返回NULL的解决办法
- 关于SQL语句中SUM函数返回NULL的解决办法
- 关于SQL语句中SUM函数返回NULL的解决办法
- 关于MYSQL进行insert操作时,存在则更新不存在则插入的解决办法
- batis 关于空值(NULL)的解决办法
- 关于ios 程序加载百度地图lib,出现链接错误:找不到符号 (null): _OBJC_CLASS_$_BMKMapManager的解决办法
- 关于Myeclipse中添加onsubmit(return *)提示错误无返回值解决办法
- Android酱油笔记之关于AlertDialog的Unable to add window token null is not for an application解决办法
- 关于SQL语句中SUM函数返回NULL的解决办法
- UCenter uc_user_synlogin同步登陆返回值为空(NULL)的解决办法 及 ucenter原理
- android编程中遇到的关于 java.lang.NullPointerException错误的原因及解决办法
- 关于ADO.Net使用TableAdapter时产生:更新要求有效的deletecommand或update,insert,selectcommand的解决办法,以及“违反并发性”处理
- 关于jquery获取下拉列表的值为null的解决办法
- 关于使用super.getHibernateTemplate()中的方法报java.lang.NullPointerException的解决办法