批量或单个 删除主表的同时也删除子表业务处理
##场景:一个消防栓对应多个硬件设备,
##所以需要删除主表数据的同时,也要把子表的数据全部删除
@Autowiredprivate DevicesMapper devicesMapper;@Autowiredprivate UserMapper userMapper;@Autowiredprivate OnenetDeviceMapper onenetDeviceMapper;/*** 批量或单个* 逻辑删除消防设备表和硬件设备表的数据* 先根据消防设备ID查询出所有硬件设备ID*/@Override@Transactionalpublic int delete(String[] devicesId){for(String str:devicesId){String devicesID=str;List<OnenetDevice> list=onenetDeviceMapper.findAllDevice(devicesID);//创建一个字符串数组,用于存放硬件设备表的所有idString [] ids=new String [list.size()];//取出集合中的所有id存到数组中for(int i=0;i<ids.length; i++){ids[i]=list.get(i).getId();}//逻辑删除硬件表的所有数据int result=onenetDeviceMapper.deleteOnenetDevice(ids);//物理删除oneNet平台上的数据for(OnenetDevice od:list){String onenetDevicesId=od.getOnenetDevicesId();DeleteDeviceApi api = new DeleteDeviceApi(onenetDevicesId, CommonUtil.key);BasicResponse<Void> response = api.executeApi();System.out.println("errno:"+response.errno+" error:"+response.error); } }//最后逻辑删除消防设备表中的数据int iden=devicesMapper.delete(devicesId);return iden;}
/*** 逻辑删除,可以批量*/int delete(@Param("devicesId")String[] devicesId);<!--逻辑删除 可批量 --><update id="delete">update sys_devices <set>status = 1</set>where devices_id in <foreach item="devicesId" index="index" collection="devicesId" open="(" separator="," close=")">#{devicesId}</foreach></update>
/*** 批量删除* @param onenetDevicesId* @return*/public Integer deleteOnenetDevice(@Param("id")String[] id);<delete id="deleteOnenetDevice">update sys_onenet_device<set>status = 1set>where id in<foreach item="id" index="index" collection="id" open="(" separator="," close=")">#{id}foreach>delete>
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!