返回主键

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5.   
  6. <mapper namespace="test">  
  7.     <insert id="insertUser" parameterType="dancheng.mybatis.po.User">  
  8.         <!--  
  9.             keyProperty:将查询出的主键设置到parameterType中的哪个属性上  
  10.             order:相对于sql语句的执行顺序  
  11.             resultType:指定返回值类型  
  12.             LAST_INSERT_ID():获取ID函数  
  13.         -->  
  14.         <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">  
  15.             SELECT LAST_INSERT_ID()  
  16.         </selectKey>  
  17.         insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})  
  18.     </insert>  
  19. </mapper>  
  1. <mapper namespace="test">  
  2.     <insert id="insertUser" parameterType="dancheng.mybatis.po.User">  
  3.         <!--  
  4.             keyProperty:将查询出的主键设置到parameterType中的哪个属性上  
  5.             order:相对于sql语句的执行顺序  
  6.             resultType:指定返回值类型  
  7.             LAST_INSERT_ID():获取ID函数  
  8.         -->  
  9.         <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">  
  10.             SELECT uuid()  
  11.         </selectKey>  
  12.         insert into user(id,username,birthday,sex,address) value(#{id}.#{username},#{birthday},#{sex},#{address})  
  13.     </insert>  
  14. </mapper>  
原文地址:https://www.cnblogs.com/gtbky/p/8873100.html