sql语句,批量操作,复制表,修改两字段来自两张表

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值