上面的saleRuleService.insertLinks(saleContentBo, null);会存在一个问题就是只会将一个saleContentBo对应的saleRuleBoList关系保存到数据库,如果要将全部的saleContentBo跟对应的saleRuleBoList关系保存下来的会就需要像下面这样迭代saleContentBoList集合然后使用saleRuleService.insertLinks(saleContentBo, null);这样就能将全部的saleContentBo对应的saleRuleBoList关系保存下来
@Test
public void testManyInsert() {
SaleActivityBo saleActivityBo = new SaleActivityBo();
saleActivityBo.setCreatorId("活动创建者id");
List<EffectiveAreaBo> effectiveAreaList = new ArrayList<>();
//第一个生效区域
EffectiveAreaBo effectiveArea = new EffectiveAreaBo();
effectiveArea.setEffectiveType(EnumEffectiveType.HOUSE_SPACE);
effectiveArea.setEffectiveId("第一个生效区域");
effectiveAreaList.add(effectiveArea);
//第二个生效区域
effectiveArea = new EffectiveAreaBo();
effectiveArea.setEffectiveType(EnumEffectiveType.ZONE);
effectiveArea.setEffectiveId("第二个生效区域");
effectiveAreaList.add(effectiveArea);
saleActivityBo.setEffectiveAreaBoList(effectiveAreaList);
//第一个优惠内容
List<SaleContentBo> saleContentBoList = new ArrayList<>();
SaleContentBo saleContentBo = new SaleContentBo();
saleContentBo.setCurrentNumber(111);
saleContentBo.setPreferentialValue(111);
//第一个优惠内容对应的规则
List<SaleRuleBo> saleRuleBoList = new ArrayList<>();
SaleRuleBo saleRuleBo = new SaleRuleBo();
saleRuleBo.setMinValue(111111);
saleRuleBo.setMaxValue(111111);
saleRuleBoList.add(saleRuleBo);
saleRuleBo = new SaleRuleBo();
saleRuleBo.setMinValue(111222);
saleRuleBo.setMaxValue(111222);
saleRuleBoList.add(saleRuleBo);
saleContentBo.setSaleRuleBoList(saleRuleBoList);
//将第一个优惠内容添加到集合中
saleContentBoList.add(saleContentBo);
//第二个优惠内容
saleContentBo = new SaleContentBo();
saleContentBo.setCurrentNumber(222);
saleContentBo.setPreferentialValue(222);
//第二个优惠内容对应的规则
saleRuleBoList = new ArrayList<>();
saleRuleBo = new SaleRuleBo();
saleRuleBo.setMinValue(222111);
saleRuleBo.setMaxValue(222111);
saleRuleBoList.add(saleRuleBo);
saleRuleBo = new SaleRuleBo();
saleRuleBo.setMinValue(222222);
saleRuleBo.setMaxValue(222222);
saleRuleBoList.add(saleRuleBo);
saleContentBo.setSaleRuleBoList(saleRuleBoList);
//将第二个优惠内容添加到集合中
saleContentBoList.add(saleContentBo);
//保存优惠内容到优惠活动中
saleActivityBo.setSaleContentBoList(saleContentBoList);
//将所有的关系映射保存起来
saleRuleService.insertWith(saleActivityBo, null);
for (SaleContentBo contentBo : saleContentBoList) {
saleRuleService.insertLinks(contentBo, null);
}
}