Ibatis和Mybatis的insert返回值差异
最近将项目从ibatis移到了Mybatis上 基本上的感觉是平滑升级 但是区别还是有的 我们就来扒一扒这俩返回值得区别好了 我们先来看一下Ibatis的文档
最近将项目从ibatis移到了Mybatis上
基本上的感觉是平滑升级
但是区别还是有的
我们就来扒一扒这俩返回值得区别好了
我们先来看一下Ibatis的文档
insert
public java.lang.Object insert(java.lang.String id,
java.lang.Object parameterObject)
throws java.sql.SQLExceptionExecutes 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.
再看一下Mybatis的
public abstract int insert (String statement, Object parameter)
Execute an insert statement with the given parameter object. Any generated autoincrement values or selectKey entries will modify the given parameter object properties. Only the number of rows affected will be returned.
Parameters
statement Unique identifier matching the statement to execute.
parameter A parameter object to pass to the statement.
Returns
int The number of rows affected by the insert.
结果会发现
在Ibatis时期 Insert返回的是主键内容
而在Mybatis时期 Insert返回的是影响行数
因为这点我被坑了好久啊~
因为在Mybatis里面设置selectKey之后
返回的依然都是1……
原因就在这里了 因为你insert变动的内容只有1条而已
而selectKey的返回值到底在哪呢
将selectKey的keyProperty设置成Object-parameter的property之后
去找找你传入的Object-parameter吧
值已经在里面等著你的调用了喔 XD