Ibatis
<insert id="insertAdmin">
INSERT INTO admin (
id
<dynamic prepend="">
<isNotNull prepend="," property="username"> username </isNotNull>
<isNotNull prepend="," property="created_at"> created_at </isNotNull>
<isNotNull prepend="," property="updated_at"> updated_at </isNotNull>
<isNotNull prepend="," property="password"> password </isNotNull>
</dynamic>
)
VALUES (
#id#
<dynamic prepend="">
<isNotNull prepend="," property="username"> #username# </isNotNull>
<isNotNull prepend="," property="created_at"> #created_at# </isNotNull>
<isNotNull prepend="," property="updated_at"> #updated_at# </isNotNull>
<isNotNull prepend="," property="password"> #password# </isNotNull>
</dynamic>
)
<selectKey resultClass="int" keyProperty="id" >
SELECT last_insert_id() as id
</selectKey>
</insert>
myIbatis
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.koala.blog.model.Test" >
insert into test (id, name)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
</insert>
在insert中指定了keyProperty=”id”,其中id代表插入的Test对象的主键属性。
还要明确useGeneratedKeys=”true”否则也不会起作用