!209 refactor:优化主键逻辑删除处理器逻辑

Merge pull request !209 from 王帅/main
This commit is contained in:
Michael Yang 2023-07-29 03:48:26 +00:00 committed by Gitee
commit 223686186f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 15 additions and 14 deletions

View File

@ -139,12 +139,13 @@ LogicDeleteManager.execWithoutLogicDelete(()->
MyBatis-Flex 提供了三种字段类型对应的逻辑删除处理器,用户可以根据逻辑删除字段的类型进行设置,它们分别是:
| 处理器名称 | 对应字段类型 | 数据正常时的值 | 数据被删除时的值 |
|------------------------------|----------|---------|----------|
| IntegerLogicDeleteProcessor | integer | 0 | 1 |
| BooleanLogicDeleteProcessor | tinyint | false | true |
| DateTimeLogicDeleteProcessor | datetime | null | 被删除时间 |
| TimeStampLogicDeleteProcessor | bigint | 0 | 被删除时的时间戳 |
| 处理器名称 | 对应字段类型 | 数据正常时的值 | 数据被删除时的值 |
|--------------------------------|-----------|---------|----------|
| IntegerLogicDeleteProcessor | integer | 0 | 1 |
| BooleanLogicDeleteProcessor | tinyint | false | true |
| DateTimeLogicDeleteProcessor | datetime | null | 被删除时间 |
| TimeStampLogicDeleteProcessor | bigint | 0 | 被删除时的时间戳 |
| PrimaryKeyLogicDeleteProcessor | 该条数据的主键类型 | null | 该条数据的主键值 |
使用时,只需通过 `LogicDeleteManager` 来设置逻辑删除处理器即可,例如:

View File

@ -16,7 +16,6 @@
package com.mybatisflex.core.logicdelete.impl;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexAssert;
import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor;
@ -36,24 +35,25 @@ import static com.mybatisflex.core.constant.SqlConsts.EQUALS;
*/
public class PrimaryKeyLogicDeleteProcessor extends AbstractLogicDeleteProcessor {
@Override
public String buildLogicNormalCondition(String logicColumn, TableInfo tableInfo, IDialect dialect) {
return dialect.wrap(logicColumn) + " IS NULL";
}
@Override
public String buildLogicDeletedSet(String logicColumn, TableInfo tableInfo, IDialect dialect) {
List<IdInfo> primaryKeyList = tableInfo.getPrimaryKeyList();
FlexAssert.notEmpty(primaryKeyList, "Must have one primary key.");
FlexAssert.notEmpty(primaryKeyList, "Entity must have one primary key.");
String column = primaryKeyList.get(0).getColumn();
return dialect.wrap(logicColumn) + EQUALS + dialect.wrap(column);
}
/**
* 正常未删除的值为 0 或者可配置为其他值
* 正常未删除的值为 {@code null}兼容不同的主键类型
*/
@Override
public Object getLogicNormalValue() {
Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
if (normalValueOfLogicDelete instanceof Number) {
return normalValueOfLogicDelete;
}
return 0;
return null;
}
/**