mysql的写法,注意只需要写 useGeneratedKeys=“true” keyProperty=“id” 就可以把id返回到对象的id中去
<insert id="insertSelective" parameterType="com.yuan.ngu.springbootmybatis.model.User" useGeneratedKeys="true" keyProperty="id" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="address != null" >
address,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="address != null" >
#{address,jdbcType=VARCHAR},
</if>
</trim>
</insert>
oracle写法(注意,在oracle中写useGeneratedKeys=“true” keyProperty=“id” ,并没有效果),oracle 的做法是执行插入前先获取一个id,然后当做一个参数传到了对象中.
<insert id="insertSelective" parameterType="com.ule.pcs.model.PcsRetailFundInfo" >
<selectKey keyProperty="id" resultType="string" order="BEFORE">
select SEQ_PCS_RETAIL_FUND_INFO.NEXTVAL from dual
</selectKey>
insert into PCS_RETAIL_FUND_INFO
<trim prefix="(" suffix=")" suffixOverrides="," >
ID,
<if test="merchantId != null" >
MERCHANT_ID
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
#{id,jdbcType=VARCHAR},
<if test="merchantId != null" >
#{merchantId,jdbcType=VARCHAR}
</if>
</trim>
</insert>