新增 LogicDeleteManager,用于处理跳过逻辑删除的场景

This commit is contained in:
开源海哥 2023-06-17 12:05:01 +08:00
parent cfdca8ce71
commit 2d3df97d3a
3 changed files with 82 additions and 1 deletions

View File

@ -69,4 +69,16 @@ globalConfig.setNormalValueOfLogicDelete("...");
//设置数据已被删除时的值
globalConfig.setDeletedValueOfLogicDelete("...");
```
```
## 跳过逻辑删除处理
在某些场景下,我们再执行查询、更新或删除数据时,有必要跳过 MyBatis-Flex 自动添加的逻辑删除的相关条件,
此时,我们可以使用 LogicDeleteManager.execWithoutLogicDelete() 方法处理,代码如下:
```java
LogicDeleteManager.execWithoutLogicDelete(() ->
accountMapper.deleteById(1)
);
```
以上代码中,`accountMapper` 会直接对 `Account` 数据进行物理删除,忽略逻辑删除字段配置。

View File

@ -0,0 +1,49 @@
package com.mybatisflex.core.logicdelete;
import java.util.function.Supplier;
public class LogicDeleteManager {
private static final ThreadLocal<Boolean> skipFlags = new ThreadLocal<>();
/**
* 跳过逻辑删除字段处理直接进行数据库物理操作
*/
public static <T> T execWithoutLogicDelete(Supplier<T> supplier) {
try {
skipLogicDelete();
return supplier.get();
} finally {
restoreLogicDelete();
}
}
/**
* 跳过逻辑删除字段处理
*/
public static void skipLogicDelete() {
skipFlags.set(Boolean.TRUE);
}
/**
* 恢复逻辑删除字段处理
*/
public static void restoreLogicDelete() {
skipFlags.remove();
}
public static String getLogicDeleteColumn(String logicDeleteColumn) {
if (logicDeleteColumn == null) {
return null;
}
Boolean skipFlag = skipFlags.get();
if (skipFlag == null) {
return logicDeleteColumn;
}
return skipFlag ? null : logicDeleteColumn;
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 逻辑删除相关功能
*/
package com.mybatisflex.core.logicdelete;