<selectKey resultType="Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
keyProperty是Java对象的属性名,而不是表格的字段名。把这个代码copy到xxx.xml就可以用了,如:
dao层的mapper接口:
int insert(Article record);
mapper包下的实现:
<insert id="insert" parameterType="com.fxy.bean.Article">
insert into article (id, title, publish_time,
comments, user_id, section_id,
content)
values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{publishTime,jdbcType=TIMESTAMP},
#{comments,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{sectionId,jdbcType=INTEGER},
#{content,jdbcType=LONGVARCHAR})
<selectKey resultType="Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
</insert>