文章分类:Java编程
关于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了。
分享到:
相关推荐
ibatis的insert方法的返回值。 介绍了mybaits如何处理返回值的问题。
NULL 博文链接:https://vbtboy.iteye.com/blog/831099
NULL 博文链接:https://yhq1212.iteye.com/blog/1772214
通过Ibatis3xml文件配置+Mysql实现数据批量插入,原先在网上找很多,都是关于批量更新,批量插入的很少,而且其写法又完全不同,所以自己研究了一下。可供参考。另外插入时如出现内容报错,请参照my.cnf修改书库配置...
spring mvc+ibatis+oracle单表增删改(包都有)
iBATIS内置别名列表,一些资源可能不完善,大家可以发来给我,以便更加的完善,谢谢大家的支持
ibatis学习 ibatis总结 ibatis ibatis ibatis
ibatis 乱码解决 ibatis乱码 gbk iso_8859_1 乱码
5.iBatis2学习笔记:入参和返回值的问题.doc 6.iBatis2学习笔记:一对多映射(双向).doc 7.iBatis2学习笔记:多对多映射(双向) .doc 8.iBatis2学习笔记:总结与思考.doc 9.iBatis2实体状态图解.doc 10.iBatis ...
iBatis工程的简单例子,在eclipse上实现的,数据库是mysql数据库,里面的字段为CREATE DATABASE MYDB; use MYDB; Drop TABLE IF EXISTS `MYDB`.`student`; Create TABLE `MYDB`.`student` ( `name` varchar(40) NOT...
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
Ibatis的jar包和相关文档可以帮助学习Ibatis
ibatis 用HashMap解决Ibatis未知列名和列数的查询结果的resultClass映射
ibatis demo,ibatis例子,ibatis示例
iBATIS 级联iBATIS 级联iBATIS 级联
WAS上log4j日志不能输出(ibatis)sql语句解决办法[借鉴].pdf
ibatis的原码 ibatis源码 ibatis源码 ibatis源码
ibatis
Ibatis3,手册,Ibatis3手册,Ibatis3使用手册,Ibatis3参考手册
ibatis教程 输入参数详解 ibatis快速入门