1.批量更新
业务层(lambda)
List<SpuDO> spuList = spuMapper.find(name);
spuList.stream().filter(Objects::nonNull).forEach(o->{
o.setValue("二公子");
});
aBoolean = spuMapper.update(spuList);
mapper
public Boolean update(@Param("spuList") List<SpuDo> spuList);
sql语句
<update id="update" parameterType="list">
<foreach collection="spuList" index="index" item="item" separator=";">
UPDATE unimall_spu_attribute
<set>
<if test="item.attribute != null">
attribute = #{item.attribute},
</if>
<if test="item.value != null">
value = #{item.value},
</if>
</set>
WHERE id = #{item.id}
</foreach>
</update>
2.批量删除
业务层(lambda)
List<SpuDo> spuList = spuAttributeMapper.find(name);
List<Long> spuIds=new ArrayList<>();
spuList.stream().filter(Objects::nonNull).forEach(o->{
spuIds.add(o.getId());
});
Integer integer = spuMapper.delete(spuIds);
mapper
public Boolean delete(@Param("spuIdList") List<Long> spuIdList);
sql语句
<delete id="batchRemove">
delete from repair_check_car_properties where id in
<foreach item="id" collection="spuIdList" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
3.批量增加
业务层(正常for循环)
@ResponseBody
@PostMapping("/save")
public R save(PartsManagementBuyingDO partsManagementBuying){ //对象里面有集合(一对多)
List<ChooselistingDO> chooseList = partsManagementBuying.getChoose();
for(int i = 0; i < chooseList.size(); i++){ //给两表关联字段赋值
chooseList.get(i).setProductlistnumber(partsManagementBuying.getId());
}
if(partsManagementBuyingService.save(partsManagementBuying,chooseList)>0){
return R.ok();
}
return R.error();
}
mapper
int save(List<ChooselistingDO> chooselistingDOList);
sql语句
<insert id="save">
insert into common_chooselisting
(
`productId`,
`productListNumber`,
`number`,
`money`,
`remark`
)
values
<foreach item="chooselistingDOList" collection="list" separator=",">
(
#{chooselistingDOList.productid},
#{chooselistingDOList.productlistnumber},
#{chooselistingDOList.number},
#{chooselistingDOList.money},
#{chooselistingDOList.remark}
)
</foreach>
</insert>
4.将一个表的数据添加到另一个表
insert into b select * from a;
5.一个sql语句,要修改的两个字段来自两张表
两个字段来自两张表(要修改的值不一样,表1主表,表2从表)
UPDATE 表1,表2 SET 表1.title='好',表2.summary='我好' WHERE 表1.id=表2.categoryId AND 表2.`id`=1