1.mysql
mysql支持自增
所以
通过useGeneratedKeys=“true” keyProperty=“回写字段值” 两个属性使用主键生成策略
<insert id="addPerson" parameterType="person" useGeneratedKeys="true" keyProperty="回写字段值">
######
</insert>
2.oracle
oracle自身不支持
所以
通过序列 myseq(1,2,3,4,5,6,7,8, …) 来模拟
创建序列
create sequence myseq
increment by 1
start with 1;
序列自带两个属性
nexseq 序列中下一个值
currval 当前值
<insert id="add" parameterType="student">
<selectKey keyProperty="stuNo" order="BEFORE" resultType="INTEGER">
select myseq.nextval from dual;
</selectKey>
insert into student (stuNo,stuAge) values (#{stuNo},#{stuAge});
</insert>
或者
<insert id="add" parameterType="student">
<selectKey keyProperty="stuNo" order="AFTER" resultType="INTEGER">
select myseq.currval from dual;
</selectKey>
insert into student (stuNo,stuAge) values (myseq.nextval,#{stuAge});
</insert>