1.mybatis中insert后返回自增的主键id
<insert id="insertSelective" parameterType="pd" useGeneratedKeys="true" keyProperty="drillid">
insert into drill
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="drillid != null" >
drillid,
</if>
<if test="drillname != null" >
drillname,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="drillid != null" >
#{drillid,jdbcType=VARCHAR},
</if>
<if test="drillname != null" >
#{drillname,jdbcType=VARCHAR},
</if>
</trim>
</insert>
关键点:useGeneratedKeys="true" keyProperty="drillid",即在普通插入中加入useGeneratedKeys和keyProperty属性即可,在插入完成后直接获取该插入实体封装的ID即可获取到参数。
2.1.mybatis中批量新增后返回自增的主键id
<insert id="batchInsertTaskDrillList" parameterType="java.util.List" keyProperty="tdid" keyColumn="tdid">
insert into task_drill (taskid,drillid)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.taskid},#{item.drillid})
</foreach>
</insert>
关键点:keyProperty="tdid" keyColumn="tdid"