首先我们应该保证数据库的主键Id是自增的,另外需要设置的两个属性为:
keyProperty="id"
useGeneratedKeys="true"
这样的话,我们在插入数据之后,就可以得到插入数据之后的对象,然后通过该对象获取该对象的id。
<insert id="insert" parameterType="Say" keyProperty="id" useGeneratedKeys="true">
INSERT INTO <include refid="say"/> (id,author,content) VALUES (#{id},#{author},#{content})
</insert> //亲测有效
<insert id="addUser" parameterType="User">
INSERT INTO <include refid="say"/> (id,author,content) VALUES (#{id},#{author},#{content})
<selectKey keyProperty="uid" order="AFTER" resultType="int">
select LAST_INSERT_ID()
</selectKey>
</insert>