例如,MySQL,使用useGeneratedKeys="true"
useGeneratedKeys="true"
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true"> insert into stu_tbl(name,age) values(#{name},#{age}) </insert>
例如,oracle,使用序列
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true"> <selectKey order="BEFORE" keyColumn="id" resultType="_int"> SELECT stu_seq.nextval FROM dual </selectKey> insert into stu_tbl(name,age) values(#{id},#{name},#{age}) </insert>
keyProperty
keyColumn
resultType
order
statementType
SQL Server 使用 select newId()
select newId()
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true"> <selectKey order="BEFORE" keyColumn="id" resultType="_int"> select newId() </selectKey> insert into stu_tbl(name,age) values(#{id},#{name},#{age}) </insert>
原文链接:http://codingdict.com/