使用通用mapper的insertSelective插入数据后,实体类的初始为null的id主动映射为数据库自增长的id

代码如下:

     spuBo.setId(null);
spuBo.setSaleable(true);
spuBo.setValid(true);
spuBo.setCreateTime(new Date());
spuBo.setLastUpdateTime(spuBo.getCreateTime());
this.spuMapper.insertSelective(spuBo);



// 新增spu_detail
SpuDetail spuDetail = spuBo.getSpuDetail();
spuDetail.setSpuId(spuBo.getId());
this.spuDetailMapper.insertSelective(spuDetail);
// 新增sku和stock
saveSkuAndStock(spuBo);


开始的spuBo的id为null,进行插入操作之后,id不再是Null,而是对应数据库中的新插入的id,
于是下面的代码就可以使用这个id进行操作了
原文地址:https://www.cnblogs.com/zxq-Study-Java/p/10044588.html