mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
add 逻辑删除处理器:LogicDeleteProcessor.java
This commit is contained in:
parent
42240f0c20
commit
dc79a7b715
@ -81,4 +81,39 @@ LogicDeleteManager.execWithoutLogicDelete(() ->
|
||||
accountMapper.deleteById(1)
|
||||
);
|
||||
```
|
||||
以上代码中,`accountMapper` 会直接对 `Account` 数据进行物理删除,忽略逻辑删除字段配置。
|
||||
以上代码中,`accountMapper` 会直接对 `Account` 数据进行物理删除,忽略逻辑删除字段配置。
|
||||
|
||||
## 自定义逻辑删除处理能力
|
||||
|
||||
在社区中,有许多用户提出希望使用时间类型,当删除时,设置删除字段为`当前时间`,当正常时,设置为 `0` 或者 `null`。
|
||||
那么,我们可以为 `LogicDeleteManager` 设置一个新的 `LogicDeleteProcessor`:
|
||||
|
||||
`LogicDeleteProcessor` 接口的内容如下:
|
||||
|
||||
```java
|
||||
public interface LogicDeleteProcessor {
|
||||
|
||||
/**
|
||||
* 用户构建查询正常数据的条件
|
||||
* @param logicColumn
|
||||
* @param dialect
|
||||
*/
|
||||
String buildLogicNormalCondition(String logicColumn, IDialect dialect);
|
||||
|
||||
/**
|
||||
* 用户与构建删除数据时的内容
|
||||
* @param logicColumn
|
||||
* @param dialect
|
||||
*/
|
||||
String buildLogicDeletedSet(String logicColumn, IDialect dialect);
|
||||
|
||||
/**
|
||||
* 用于构建通过 QueryWrapper 查询数据时的内容
|
||||
* @param queryWrapper
|
||||
* @param tableInfo
|
||||
*/
|
||||
void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo);
|
||||
}
|
||||
```
|
||||
|
||||
具体实现可以参考:[DefaultLogicDeleteProcessorImpl](DefaultLogicDeleteProcessorImpl)
|
||||
@ -15,11 +15,11 @@
|
||||
*/
|
||||
package com.mybatisflex.core.dialect.impl;
|
||||
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.dialect.IDialect;
|
||||
import com.mybatisflex.core.dialect.KeywordWrap;
|
||||
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
|
||||
import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
|
||||
import com.mybatisflex.core.query.*;
|
||||
import com.mybatisflex.core.row.Row;
|
||||
import com.mybatisflex.core.row.RowCPI;
|
||||
@ -492,7 +492,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
|
||||
@Override
|
||||
public String forDeleteEntityById(TableInfo tableInfo) {
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumn();
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
||||
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
||||
//正常删除
|
||||
if (StringUtil.isBlank(logicDeleteColumn)) {
|
||||
@ -518,7 +518,6 @@ public class CommonsDialectImpl implements IDialect {
|
||||
sql.append(wrap(primaryKeys[i])).append(EQUALS_PLACEHOLDER);
|
||||
}
|
||||
|
||||
// sql.append(AND).append(wrap(logicDeleteColumn)).append(EQUALS).append(getLogicNormalValue());
|
||||
sql.append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
||||
|
||||
//租户ID
|
||||
@ -532,7 +531,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
|
||||
@Override
|
||||
public String forDeleteEntityBatchByIds(TableInfo tableInfo, Object[] primaryValues) {
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumn();
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
||||
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
||||
|
||||
//正常删除
|
||||
@ -594,7 +593,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
@Override
|
||||
public String forDeleteEntityBatchByQuery(TableInfo tableInfo, QueryWrapper queryWrapper) {
|
||||
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumn();
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
||||
|
||||
//正常删除
|
||||
if (StringUtil.isBlank(logicDeleteColumn)) {
|
||||
@ -663,7 +662,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
}
|
||||
|
||||
//逻辑删除条件,已删除的数据不能被修改
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumn();
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
||||
if (StringUtil.isNotBlank(logicDeleteColumn)) {
|
||||
sql.append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
||||
}
|
||||
@ -814,7 +813,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
}
|
||||
|
||||
//逻辑删除的情况下,需要添加逻辑删除的条件
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumn();
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
||||
if (StringUtil.isNotBlank(logicDeleteColumn)) {
|
||||
sql.append(AND).append(buildLogicNormalCondition(logicDeleteColumn));
|
||||
}
|
||||
@ -836,7 +835,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
sql.append(WHERE);
|
||||
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
||||
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumn();
|
||||
String logicDeleteColumn = tableInfo.getLogicDeleteColumnOrSkip();
|
||||
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
|
||||
if (StringUtil.isNotBlank(logicDeleteColumn) || ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
||||
sql.append(BRACKET_LEFT);
|
||||
@ -975,34 +974,14 @@ public class CommonsDialectImpl implements IDialect {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
protected String buildLogicNormalCondition(String logicColumn) {
|
||||
return wrap(logicColumn) + EQUALS + getLogicNormalValue();
|
||||
return LogicDeleteManager.getProcessor().buildLogicNormalCondition(logicColumn,this);
|
||||
}
|
||||
|
||||
|
||||
protected String buildLogicDeletedSet(String logicColumn) {
|
||||
return wrap(logicColumn) + EQUALS + getLogicDeletedValue();
|
||||
return LogicDeleteManager.getProcessor().buildLogicDeletedSet(logicColumn,this);
|
||||
}
|
||||
|
||||
|
||||
protected Object getLogicNormalValue() {
|
||||
Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
|
||||
if (normalValueOfLogicDelete instanceof Number
|
||||
|| normalValueOfLogicDelete instanceof Boolean) {
|
||||
return normalValueOfLogicDelete;
|
||||
}
|
||||
return SINGLE_QUOTE + normalValueOfLogicDelete + SINGLE_QUOTE;
|
||||
}
|
||||
|
||||
|
||||
protected Object getLogicDeletedValue() {
|
||||
Object deletedValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getDeletedValueOfLogicDelete();
|
||||
if (deletedValueOfLogicDelete instanceof Number
|
||||
|| deletedValueOfLogicDelete instanceof Boolean) {
|
||||
return deletedValueOfLogicDelete;
|
||||
}
|
||||
return SINGLE_QUOTE + deletedValueOfLogicDelete + SINGLE_QUOTE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.constant.SqlConsts;
|
||||
import com.mybatisflex.core.dialect.IDialect;
|
||||
import com.mybatisflex.core.query.QueryCondition;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
|
||||
import static com.mybatisflex.core.constant.SqlConsts.EQUALS;
|
||||
import static com.mybatisflex.core.constant.SqlConsts.SINGLE_QUOTE;
|
||||
|
||||
public class DefaultLogicDeleteProcessorImpl implements LogicDeleteProcessor {
|
||||
|
||||
@Override
|
||||
public String buildLogicNormalCondition(String logicColumn, IDialect dialect) {
|
||||
return dialect.wrap(logicColumn) + EQUALS + getLogicNormalValue();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String buildLogicDeletedSet(String logicColumn, IDialect dialect) {
|
||||
return dialect.wrap(logicColumn) + EQUALS + getLogicDeletedValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) {
|
||||
queryWrapper.and(QueryCondition.create(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getLogicDeleteColumn()
|
||||
, SqlConsts.EQUALS
|
||||
, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
||||
}
|
||||
|
||||
|
||||
protected Object getLogicNormalValue() {
|
||||
Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
|
||||
if (normalValueOfLogicDelete instanceof Number
|
||||
|| normalValueOfLogicDelete instanceof Boolean) {
|
||||
return normalValueOfLogicDelete;
|
||||
}
|
||||
return SINGLE_QUOTE + normalValueOfLogicDelete + SINGLE_QUOTE;
|
||||
}
|
||||
|
||||
|
||||
protected Object getLogicDeletedValue() {
|
||||
Object deletedValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getDeletedValueOfLogicDelete();
|
||||
if (deletedValueOfLogicDelete instanceof Number
|
||||
|| deletedValueOfLogicDelete instanceof Boolean) {
|
||||
return deletedValueOfLogicDelete;
|
||||
}
|
||||
return SINGLE_QUOTE + deletedValueOfLogicDelete + SINGLE_QUOTE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,11 +1,34 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LogicDeleteManager {
|
||||
|
||||
private static LogicDeleteProcessor processor = new DefaultLogicDeleteProcessorImpl();
|
||||
private static final ThreadLocal<Boolean> skipFlags = new ThreadLocal<>();
|
||||
|
||||
public static LogicDeleteProcessor getProcessor() {
|
||||
return processor;
|
||||
}
|
||||
|
||||
public static void setProcessor(LogicDeleteProcessor processor) {
|
||||
LogicDeleteManager.processor = processor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳过逻辑删除字段处理,直接进行数据库物理操作
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.mybatisflex.core.dialect.IDialect;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
|
||||
public interface LogicDeleteProcessor {
|
||||
|
||||
/**
|
||||
* 用户构建查询正常数据的条件
|
||||
* @param logicColumn
|
||||
* @param dialect
|
||||
*/
|
||||
String buildLogicNormalCondition(String logicColumn, IDialect dialect);
|
||||
|
||||
/**
|
||||
* 用户与构建删除数据时的内容
|
||||
* @param logicColumn
|
||||
* @param dialect
|
||||
*/
|
||||
String buildLogicDeletedSet(String logicColumn, IDialect dialect);
|
||||
|
||||
/**
|
||||
* 用于构建通过 QueryWrapper 查询数据时的内容
|
||||
* @param queryWrapper
|
||||
* @param tableInfo
|
||||
*/
|
||||
void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo);
|
||||
}
|
||||
|
||||
|
||||
@ -164,10 +164,13 @@ public class TableInfo {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public String getLogicDeleteColumn() {
|
||||
public String getLogicDeleteColumnOrSkip() {
|
||||
return LogicDeleteManager.getLogicDeleteColumn(logicDeleteColumn);
|
||||
}
|
||||
|
||||
public String getLogicDeleteColumn() {
|
||||
return logicDeleteColumn;
|
||||
}
|
||||
public void setLogicDeleteColumn(String logicDeleteColumn) {
|
||||
this.logicDeleteColumn = logicDeleteColumn;
|
||||
}
|
||||
@ -666,9 +669,10 @@ public class TableInfo {
|
||||
}
|
||||
|
||||
//逻辑删除
|
||||
if (StringUtil.isNotBlank(getLogicDeleteColumn())) {
|
||||
queryWrapper.and(QueryCondition.create(schema, tableName, logicDeleteColumn, SqlConsts.EQUALS
|
||||
, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
||||
if (StringUtil.isNotBlank(getLogicDeleteColumnOrSkip())) {
|
||||
// queryWrapper.and(QueryCondition.create(schema, tableName, logicDeleteColumn, SqlConsts.EQUALS
|
||||
// , FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
||||
LogicDeleteManager.getProcessor().buildQueryCondition(queryWrapper,this);
|
||||
}
|
||||
|
||||
//多租户
|
||||
@ -986,7 +990,7 @@ public class TableInfo {
|
||||
* @param entityObject
|
||||
*/
|
||||
public void initLogicDeleteValueIfNecessary(Object entityObject) {
|
||||
if (StringUtil.isBlank(getLogicDeleteColumn())) {
|
||||
if (StringUtil.isBlank(getLogicDeleteColumnOrSkip())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user