在做配置选项(设备类型,所属楼层等)的时候,当删除某配置的时候,我需要检验该配置是否已被删除。
@Override
public BaseVO deleteOptionDetail(Integer id) {
// 合法性验证
if (null == id) {
return ParamErrorVO.getInstance();
}
ConfigOptionDetail configOptionDetail = configOptionDetailMapper.selectById(id);
if (null == configOptionDetail || 1 == configOptionDetail.getIsDeleted()) {
return new ErrorVO("该配置不存在");
}
if (configOptionDetail.getSystem() == 1) {
return new ErrorVO("系统属性不能删除");
}
if (configOptionDetail.getUseCount() = 0) {
return new ErrorVO("配置正在使用不能删除,请先删除使用配置的地方");
}
// 合法性通过
configOptionDetail.setIsDeleted(1);
configOptionDetail.setGmtModefied(Calendar.getInstance().getTime());
configOptionDetailMapper.updateById(configOptionDetail);
// 更新内存配置
ConfigOptionConstruct.updateOption();
return SuccessVO.getInstance();
}
思考之后我决定采用,给配置选项设备一个use_count字段,代表该配置被引用的次数。 只有当该字段值为 0 时,该配置选项记录才可被删除。

使用情况: 我需要批量删除房间, 删除房间的同时,room对象中使用到了所属楼层的配置选项,我需要将他们的引用减少
@Override
public BaseVO deleteRoomByIds(Integer[] ids) {
if (null == ids) {
return ParamErrorVO.getInstance();
}
EntityWrapper<Room> entityWrapper = new EntityWrapper<>();
entityWrapper.where("isdelete={0}", 0);
// 核查删除的房间中是否存在正在使用的设备
List<Integer> notDelete = deviceInfoService.checkRoomIds(ids);
if (null != notDelete && 0 != notDelete.size()) {

本文介绍了在MyBatis中如何使用foreach元素进行批量更新操作。在删除配置选项前,通过增加use_count字段来判断引用次数。在批量删除房间时,需要减少相关配置选项的引用计数,作者通过将optionIds分组转换为Map,并利用MyBatis的foreach动态SQL进行批量更新,避免了多余的分隔符问题。
最低0.47元/天 解锁文章
980

被折叠的 条评论
为什么被折叠?



