Mybatis插入后返回主键

<insert id="add" parameterType="vo.Category">

<selectKey resultType="java.lang.Short" order="AFTER" keyProperty="id">

SELECT LAST_INSERT_ID()

</selectKey>

insert into category (name_zh, parent_id,

show_order, delete_status, description

)

values (#{nameZh,jdbcType=VARCHAR},

#{parentId,jdbcType=SMALLINT},

#{showOrder,jdbcType=SMALLINT},

#{deleteStatus,jdbcType=BIT},

#{description,jdbcType=VARCHAR}

)

</insert>


在插入操作完成之后,参数category的id属性就已经被赋值了

执行完mapper.insert(category);获取插入返回的主键用category.getId()就可以了

你可能感兴趣的:(mybatis)