mysql
<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
insert into person(name,pswd) values(#{name},#{pswd})
</insert>
<insert id="insert" parameterType="Person">
<selectKey keyProperty="id" resultType="long">
select LAST_INSERT_ID()
</selectKey>
insert into person(name,pswd) values(#{name},#{pswd})
</insert>
oracle
CREATE SEQUENCE "SEQ_M_USER"
MINVALUE 1
MAXVALUE 9999999999999999999999999999
INCREMENT BY 1 START WITH 501 CACHE 20 ORDER NOCYCLE ;
1.先通过sequence查出来id,再insert
<insert id=”add” useGeneratedKeys=”false” keyProperty=”id” parameterType=”Person”>
<selectKey resultType=”String” keyProperty=”id” order=”BEFORE”>
select yoursequence.nextval as id from dual
</selectKey>
insert into Person (id,user_id, user_name) values(#{id},#{userid},#{username})
</insert>
2.直接insertsequence.nextval,如下
<insert id=”add” useGeneratedKeys=”false” keyProperty=”id” parameterType=”Person”>
insert into Person (id,user_id, user_name) values(yoursequence.nextval,#{userid},#{username})
</insert>