ibatis中selectKey的作用
ibatis 插入数据将selectKey放在insert之后,通过LAST_INSERT_ID() 获得刚插入的自动增长的id的值。
mysql中:
<selectKey resultClass="int" keyProperty="id">
<![CDATA[SELECT LAST_INSERT_ID() AS ID]]>
</selectKey>
例子:
<insert id="insertUser" parameterClass="ibatis.User">
insert into user
(name,password)
values
(#name#,#password#)
<selectKey resultClass="long" keyProperty="id">
SELECT LAST_INSERT_ID() AS ID
</selectKey>
</insert>
oracle中:
要把<selectKey resultClass="long" keyProperty="id">
SELECT LAST_INSERT_ID() AS ID
</selectKey> 放前面,因为在oracle中是先生成id
<insert id="insertUser" parameterClass="ibatis.User">
<selectKey resultClass="long" keyProperty="id">
SELECT LAST_INSERT_ID() AS ID
</selectKey>
insert into user
(name,password)
values
(#name#,#password#)
</insert>