ibatis的insert方法的返回值

返回值竟然是id

ibatis的SqlMapClient的insert方法是有返回值的,类型是object,包裹的其实就是刚刚插入的自增长字段的值。请注意,配置文件要这样写:

Xml代码 
  1. < insert id = "insertJizhan" parameterClass = "Jizhan" >   
  2.          INSERT INTO jizhan (   
  3.              name              
  4.          ) VALUES (   
  5.              #name#              
  6.          )   
  7.          < selectKey resultClass = "int" keyProperty = "gid" >   
  8.              SELECT lastval() AS gid   
  9.          </ selectKey >        
  10. </ insert >   


这里lastval()是postgres中得到刚刚插入的自增长字段值的函数,SQL中用@@IDENTITY。即:

<selectKey resultClass="int" keyProperty="id" >
      SELECT @@IDENTITY AS ID
</selectKey>

你可能感兴趣的:(ibatis)