当前端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>