在使用mysql作为数据库的时候,当设置数据库中某一表主键为自增长时,前端数据回显的时候需要此主键ID,因此这时候需要用到主键返回封装策略。
在mybatis的Mapper.xml文件中
<insert id="insert" parameterType="com.taotao.pojo.TbContentCategory" >
<!-- 返回主键的id,提供给前端,将前端的id由默认值0改成数据库中对应的id -->
<selectKey keyProperty="id" resultType="long" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into tb_content_category (id, parent_id, name,
status, sort_order, is_parent,
created, updated)
values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR},
#{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT},
#{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
</insert>