持之以恒

记录本
posts - 4, comments - 32, trackbacks - 0, articles - 74
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
关于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类型。

然而,如果我们使用下列的配置文件进行插入的话,返回的值为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数据库为例,改写上述配置文件,具体如下:



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的实现类方法里面就应该这样写了(注意整型转换用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: 关于Ibatis insert后返回值为null的解决办法(转)   回复  更多评论   

2013-02-22 09:06 by 路过看看
并发量大的时候,这正方法可以?

只有注册用户登录后才能发表评论。


网站导航: