解决接收POST数组参数并一条SQL批量删除数据

本文介绍了一种通过将前端POST请求的数组参数转换为List的方式,实现后端批量删除关联关系的方法。具体包括如何使用@RequestBody接收参数、如何将数组转换为List以及在DAO层实现批量删除的具体SQL语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当前端POST参数为数组时,如:

[
 { attrId: 4, attrGroupId: 3 },
 { attrId: 1, attrGroupId: 2 }
]

接收参数要使用@RequestBody,可以设置一个实体类以数组方式接收:

    /**
     * 删除关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrAttrgroupRelationEntity[] attrRelation) {
        attrGroupService.deleteBatchRelation(attrRelation);
        return R.ok();
    }

要将接收到的数组 [] 转换为 List<T>方式

     /**
     * 删除关联关系(可批量删除)
     */
    @Override
    public void deleteBatchRelation(AttrAttrgroupRelationEntity[] attrRelation) {
        // 把数组转化为实体List属性
        List<AttrAttrgroupRelationEntity> entities = Arrays.asList(attrRelation).stream().collect(Collectors.toList());
        this.baseMapper.deleteBatchRelation(entities);
    }

批量删除:

@Mapper
public interface AttrGroupDao extends BaseMapper<AttrGroupEntity> {
    void deleteBatchRelation(@Param("entities") List<AttrAttrgroupRelationEntity> entities);

}


在Dao.xml里设置循环
<delete id="deleteBatchRelation">
   DELETE FROM `pms_attr_attrgroup_relation` WHERE
   <foreach collection="entities" item="item" separator="OR">
       attr_group_id = #{item.attrGroupId} and attr_id = #{item.attrId}
   </foreach>
</delete>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值