mapper文件中设置插入返回主键
<insert id="insertSelective" parameterType="com.pyg.pojo.TbGoods" useGeneratedKeys="true" keyProperty="id">
注意:主键是写回到pojo中的;
从pojo中读取主键
//保存货品表数据
TbGoods tbGoods = goods.getGoods();
goodsMapper.insertSelective(tbGoods);
//主键获取的正确方式
Long id = tbGoods.getId();
//保存货品描述表
TbGoodsDesc goodsDesc = goods.getGoodsDesc();
//设置外键
goodsDesc.setGoodsId(tbGoods.getId());
goodsDescMapper.insertSelective(goodsDesc);