离现实最近的地方是梦想
Oracle中获取刚刚插入记录的主键值:
<insert id="insertSelective" parameterType="com.jxxx.p2pp.model.UUserInfo">
<selectKey resultType="java.math.BigDecimal" order="BEFORE" keyProperty="id">
SELECT U_USER_INFO_SEQ.Nextval as ID from DUAL
</selectKey>
insert into U_USER_INFO
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
ID,
</if>
<if test="userName != null" >
USER_NAME,
</if>
<if test="realName != null" >
REAL_NAME,
</if>
.....
</insert>
* 要点是这里使用了selectKey来定义返回新生成的PrimaryKey,这个情况仅仅适用于Oracle。
MySQL与SQL Server获取刚刚插入数据的主键值:
<!-- 插入 -->
<insert id="insert" parameterType="com.citic.gatz.model.ChatContentImg" useGeneratedKeys="true" keyProperty="id">
insert into chat_content_img(id, url,filename,thumb,secret,thumb_secret,width,height)
values(null,#{url},#{filename},#{thumb},#{secret},#{thumbSecret},#{width},#{height})
</insert>
* 使用useGeneratedKeys/KeyProperty来实现插入数据的时候,来完成新生成主键的返回。