1. 在商家后台实现录入功能,包括商品名称,副标题,价格,包装列表,售后服务
分析:商品名称,副标题,价格在tb_goods表里,包装列表,售后服务在tb_goods_desc表里。tb_goods_desc表中的主键goods_id引用tb_goods表中的主键
。
应用MyBatis,在MyBatis的映射文件TbGoodsMapper.xml
中<insert>
中加入<selectKey>
,在执行insert
语句之后就先把插入的id
值查询出来。
<insert id="insert" parameterType="com.pinyougou.pojo.TbGoods" >
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
select LAST_INSERT_ID() AS id
</selectKey>
insert into tb_goods (id, seller_id, goods_name,
default_item_id, audit_status, is_marketable,
brand_id, caption, category1_id,
category2_id, category3_id, small_pic,
price, type_template_id, is_enable_spec,
is_delete)
values (#{id,jdbcType=BIGINT}, #{sellerId,jdbcType=VARCHAR}, #{goodsName,jdbcType=VARCHAR},
#{defaultItemId,jdbcType=BIGINT}, #{auditStatus,jdbcType=VARCHAR}, #{isMarketable,jdbcType=VARCHAR},
#{brandId,jdbcType=BIGINT}, #{caption,jdbcType=VARCHAR}, #{category1Id,jdbcType=BIGINT},
#{category2Id,jdbcType=BIGINT}, #{category3Id,jdbcType=BIGINT}, #{smallPic,jdbcType=VARCHAR},
#{price,jdbcType=DECIMAL}, #{typeTemplateId,jdbcType=BIGINT}, #{isEnableSpec,jdbcType=VARCHAR},
#{isDelete,jdbcType=VARCHAR})
</insert>
总体解释:将插入数据的主键返回到 TbGoods对象中。
具体解释:
select LAST_INSERT_ID()
:得到刚 insert 进去记录的主键值,只适用与自增主键
keyProperty
:将查询到主键值设置到 parameterType 指定的对象的那个属性
order
:select LAST_INSERT_ID()
执行顺序,相对于 insert 语句来说它的执行顺序
resultType
:指定 select LAST_INSERT_ID()
的结果类型