<mapper namespace="test">
<insert id="insertUser" parameterType="dancheng.mybatis.po.User">
<!-- keyProperty:将查询出的主键设置到parameterType中的哪一个属性上 order:相对于sql语句的执行顺序 resultType:指定返回值类型 LAST_INSERT_ID():获取ID函数 -->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address}) </insert>
</mapper>
<mapper namespace="test">
<insert id="insertUser" parameterType="dancheng.mybatis.po.User">
<!-- keyProperty:将查询出的主键设置到parameterType中的哪一个属性上 order:相对于sql语句的执行顺序 resultType:指定返回值类型 LAST_INSERT_ID():获取ID函数 -->
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> SELECT uuid() </selectKey> insert into user(id,username,birthday,sex,address) value(#{id}.#{username},#{birthday},#{sex},#{address}) </insert>
</mapper>
oracle主键返回
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
SELECT 序列名.nextval() FROM DUAL;
</selectKey>
insert into user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address})
</insert>
mysql 主键返回值
(1)主键返回之自增主键

(2)主键返回值UUID
UUID函数是mysql的函数html

Oracle主键返回值
(1)序列也就是sequence,它是Oracle的主键生成策略java

(2)mysql
<insert id="insert" parameterType="com.xxx.SystemDepartment">
<selectKey keyProperty="id" resultType="String" order="BEFORE">
select sys_guid() from dual
</selectKey>
insert into SYSTEM_DEPARTMENT (ID,DEPNAME, SUPERID, SORT, STATE)
values ( #{id,jdbcType=VARCHAR},#{depname,jdbcType=VARCHAR},
#{superid,jdbcType=DECIMAL}, #{sort,jdbcType=DECIMAL},
#{state,jdbcType=DECIMAL})
</insert>