From 46a25cde62f9bacbf3261bd9fa8991ee6868e807 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 21:49:28 +0800 Subject: [PATCH 01/13] =?UTF-8?q?style:=20=E5=AE=8C=E5=96=84=20LogicDelete?= =?UTF-8?q?Processor=20=E6=8E=A5=E5=8F=A3=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../logicdelete/LogicDeleteProcessor.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteProcessor.java index 97a39d5b..39895851 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteProcessor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteProcessor.java @@ -19,28 +19,35 @@ 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 + * 用户构建查询正常数据的条件。 + * + * @param logicColumn 逻辑删除列 + * @param dialect 数据库方言 */ String buildLogicNormalCondition(String logicColumn, IDialect dialect); /** - * 用户与构建删除数据时的内容 - * @param logicColumn - * @param dialect + * 用户与构建删除数据时的内容。 + * + * @param logicColumn 逻辑删除列 + * @param dialect 数据库方言 */ String buildLogicDeletedSet(String logicColumn, IDialect dialect); /** - * 用于构建通过 QueryWrapper 查询数据时的内容 - * @param queryWrapper - * @param tableInfo + * 用于构建通过 {@link QueryWrapper} 查询数据时的内容。 + * + * @param queryWrapper 条件构造器 + * @param tableInfo 表信息 */ void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo); + } From 7e504299f3ff4923b7c31a4ba44a270f02e0e545 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 21:52:41 +0800 Subject: [PATCH 02/13] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E9=BB=98=E8=AE=A4=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractLogicDeleteProcessor.java | 66 +++++++++++++++++++ .../core/logicdelete/LogicDeleteManager.java | 34 ++++++++-- .../DefaultLogicDeleteProcessorImpl.java | 26 +++----- .../core/logicdelete/impl/package-info.java | 20 ++++++ 4 files changed, 122 insertions(+), 24 deletions(-) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java rename mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/{ => impl}/DefaultLogicDeleteProcessorImpl.java (77%) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java new file mode 100644 index 00000000..e3b6d32f --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.QueryCondition; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.table.TableInfo; + +import static com.mybatisflex.core.constant.SqlConsts.EQUALS; + +/** + * 逻辑删除处理器抽象类。 + * + * @author 王帅 + * @since 2023-06-20 + */ +public abstract class AbstractLogicDeleteProcessor 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() + , EQUALS + , getLogicNormalValue())); + } + + /** + * 获取逻辑删除列未删除标记值。 + * + * @return 未删除标记值 + */ + protected abstract Object getLogicNormalValue(); + + /** + * 获取逻辑删除列删除时标记值。 + * + * @return 删除时标记值 + */ + protected abstract Object getLogicDeletedValue(); + +} + + diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java index 8464e8c6..2a0e2235 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java @@ -15,23 +15,41 @@ */ package com.mybatisflex.core.logicdelete; +import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessorImpl; + import java.util.function.Supplier; +/** + * 逻辑删除管理器。 + */ public class LogicDeleteManager { + private LogicDeleteManager() { + } + private static LogicDeleteProcessor processor = new DefaultLogicDeleteProcessorImpl(); private static final ThreadLocal skipFlags = new ThreadLocal<>(); + /** + * 获取逻辑删除处理器。 + * + * @return 逻辑删除处理器 + */ public static LogicDeleteProcessor getProcessor() { return processor; } + /** + * 设置逻辑删除处理器。 + * + * @param processor 逻辑删除处理器 + */ public static void setProcessor(LogicDeleteProcessor processor) { LogicDeleteManager.processor = processor; } /** - * 跳过逻辑删除字段处理,直接进行数据库物理操作 + * 跳过逻辑删除字段处理,直接进行数据库物理操作。 */ public static T execWithoutLogicDelete(Supplier supplier) { try { @@ -43,21 +61,25 @@ public class LogicDeleteManager { } /** - * 跳过逻辑删除字段处理 + * 跳过逻辑删除字段处理。 */ public static void skipLogicDelete() { skipFlags.set(Boolean.TRUE); } - /** - * 恢复逻辑删除字段处理 + * 恢复逻辑删除字段处理。 */ public static void restoreLogicDelete() { skipFlags.remove(); } - + /** + * 获取逻辑删除列,返回 {@code null} 表示跳过逻辑删除。 + * + * @param logicDeleteColumn 逻辑删除列 + * @return 逻辑删除列 + */ public static String getLogicDeleteColumn(String logicDeleteColumn) { if (logicDeleteColumn == null) { return null; @@ -66,7 +88,7 @@ public class LogicDeleteManager { if (skipFlag == null) { return logicDeleteColumn; } - return skipFlag ? null : logicDeleteColumn; } + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/DefaultLogicDeleteProcessorImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java similarity index 77% rename from mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/DefaultLogicDeleteProcessorImpl.java rename to mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java index 4e95ba95..acae4807 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/DefaultLogicDeleteProcessorImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java @@ -13,11 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mybatisflex.core.logicdelete; +package com.mybatisflex.core.logicdelete.impl; import com.mybatisflex.core.FlexGlobalConfig; -import com.mybatisflex.core.constant.SqlConsts; -import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor; import com.mybatisflex.core.query.QueryCondition; import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.table.TableInfo; @@ -25,27 +24,18 @@ 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(); - } +/** + * 默认逻辑删除处理器。 + */ +public class DefaultLogicDeleteProcessorImpl extends AbstractLogicDeleteProcessor { @Override public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) { queryWrapper.and(QueryCondition.create(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getLogicDeleteColumn() - , SqlConsts.EQUALS + , EQUALS , FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete())); } - protected Object getLogicNormalValue() { Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete(); if (normalValueOfLogicDelete instanceof Number @@ -55,7 +45,6 @@ public class DefaultLogicDeleteProcessorImpl implements LogicDeleteProcessor { return SINGLE_QUOTE + normalValueOfLogicDelete + SINGLE_QUOTE; } - protected Object getLogicDeletedValue() { Object deletedValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getDeletedValueOfLogicDelete(); if (deletedValueOfLogicDelete instanceof Number @@ -64,6 +53,7 @@ public class DefaultLogicDeleteProcessorImpl implements LogicDeleteProcessor { } return SINGLE_QUOTE + deletedValueOfLogicDelete + SINGLE_QUOTE; } + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java new file mode 100644 index 00000000..0e5d3e57 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.impl; \ No newline at end of file From 8fef406cf2090e4d2bfcab53c8c31327319652a8 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 21:56:23 +0800 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E5=A4=84=E7=90=86=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/BooleanLogicDeleteProcessor.java | 45 ++++++++++++++++++ .../impl/DateTimeLogicDeleteProcessor.java | 46 +++++++++++++++++++ .../impl/IntegerLogicDeleteProcessor.java | 45 ++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/BooleanLogicDeleteProcessor.java create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/IntegerLogicDeleteProcessor.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/BooleanLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/BooleanLogicDeleteProcessor.java new file mode 100644 index 00000000..bbd18397 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/BooleanLogicDeleteProcessor.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.impl; + +import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor; + +/** + * {@link Boolean} 类型的属性对应的逻辑删除处理器。 + * + * @author 王帅 + * @since 2023-06-20 + */ +public class BooleanLogicDeleteProcessor extends AbstractLogicDeleteProcessor { + + /** + * 逻辑删除字段值为 {@code false} 表示数据未删除。 + */ + @Override + protected Object getLogicNormalValue() { + return "FALSE"; + } + + /** + * 逻辑删除字段值为 {@code true} 表示数据删除。 + */ + @Override + protected Object getLogicDeletedValue() { + return "TRUE"; + } + +} \ No newline at end of file diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java new file mode 100644 index 00000000..c388af3f --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.impl; + +import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor; +import com.mybatisflex.core.query.RawFragment; + +/** + * {@link java.time.LocalDateTime} 类型的属性对应的逻辑删除处理器。 + * + * @author 王帅 + * @since 2023-06-20 + */ +public class DateTimeLogicDeleteProcessor extends AbstractLogicDeleteProcessor { + + /** + * 逻辑删除字段值为 {@code null} 表示数据未删除。 + */ + @Override + protected Object getLogicNormalValue() { + return null; + } + + /** + * 逻辑删除字段值为 {@code NOW()} 表示数据删除,并记录删除时间。 + */ + @Override + protected Object getLogicDeletedValue() { + return new RawFragment("NOW()"); + } + +} \ No newline at end of file diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/IntegerLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/IntegerLogicDeleteProcessor.java new file mode 100644 index 00000000..e62af97f --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/IntegerLogicDeleteProcessor.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.impl; + +import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor; + +/** + * {@link Integer} 类型的属性对应的逻辑删除处理器。 + * + * @author 王帅 + * @since 2023-06-20 + */ +public class IntegerLogicDeleteProcessor extends AbstractLogicDeleteProcessor { + + /** + * 逻辑删除字段值为 {@code 0} 表示数据未删除。 + */ + @Override + protected Object getLogicNormalValue() { + return "0"; + } + + /** + * 逻辑删除字段值为 {@code 1} 表示数据删除。 + */ + @Override + protected Object getLogicDeletedValue() { + return "1"; + } + +} \ No newline at end of file From 75bd9799d4089067aa99ce1cbfe9813cf6c07d1a Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 21:56:31 +0800 Subject: [PATCH 04/13] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E5=A4=84=E7=90=86=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/coretest/LogicDeleteTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java new file mode 100644 index 00000000..1994733c --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.coretest; + +import com.mybatisflex.core.dialect.DialectFactory; +import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.logicdelete.LogicDeleteProcessor; +import com.mybatisflex.core.logicdelete.impl.BooleanLogicDeleteProcessor; +import com.mybatisflex.core.logicdelete.impl.DateTimeLogicDeleteProcessor; +import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessorImpl; +import com.mybatisflex.core.logicdelete.impl.IntegerLogicDeleteProcessor; +import org.junit.Test; + +/** + * 逻辑删除测试。 + * + * @author 王帅 + * @since 2023-06-20 + */ +@SuppressWarnings("all") +public class LogicDeleteTest { + + private final String logicColumn = "deleted"; + private final IDialect dialect = DialectFactory.getDialect(); + + @Test + public void test() { + print("DefaultLogicDeleteProcessor", new DefaultLogicDeleteProcessorImpl()); + print("BooleanLogicDeleteProcessor", new BooleanLogicDeleteProcessor()); + print("IntegerLogicDeleteProcessor", new IntegerLogicDeleteProcessor()); + print("DateTimeLogicDeleteProcessor", new DateTimeLogicDeleteProcessor()); + } + + public void print(String type, LogicDeleteProcessor processor) { + System.out.println("===== " + type + " ====="); + System.out.println(processor.buildLogicDeletedSet(logicColumn, dialect)); + System.out.println(processor.buildLogicNormalCondition(logicColumn, dialect)); + } + +} \ No newline at end of file From afd043051e4db00c93a4630b37fdf0540aaaa171 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 21:56:40 +0800 Subject: [PATCH 05/13] =?UTF-8?q?doc:=20=E6=B7=BB=E5=8A=A0=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E5=A4=84=E7=90=86=E5=99=A8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=87=E6=A1=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/core/logic-delete.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/zh/core/logic-delete.md b/docs/zh/core/logic-delete.md index 361bef95..4bc2af17 100644 --- a/docs/zh/core/logic-delete.md +++ b/docs/zh/core/logic-delete.md @@ -122,12 +122,29 @@ globalConfig.setDeletedValueOfLogicDelete("..."); 此时,我们可以使用 LogicDeleteManager.execWithoutLogicDelete() 方法处理,代码如下: ```java -LogicDeleteManager.execWithoutLogicDelete(() -> - accountMapper.deleteById(1) +LogicDeleteManager.execWithoutLogicDelete(()-> + accountMapper.deleteById(1) ); ``` + 以上代码中,`accountMapper` 会直接对 `Account` 数据进行物理删除,忽略逻辑删除字段配置。 +## 内置逻辑删除处理器 + +MyBatis-Flex 提供了三种字段类型对应的逻辑删除处理器,用户可以根据逻辑删除字段的类型进行设置,它们分别是: + +| 处理器名称 | 对应字段类型 | 数据正常时的值 | 数据被删除时的值 | +|------------------------------|----------|---------|----------| +| IntegerLogicDeleteProcessor | integer | 0 | 1 | +| BooleanLogicDeleteProcessor | tinyint | false | true | +| DateTimeLogicDeleteProcessor | datetime | null | 被删除时间 | + +使用时,只需通过 `LogicDeleteManager` 来设置逻辑删除处理器即可,例如: + +```java +LogicDeleteManager.setProcessor(new DateTimeLogicDeleteProcessor); +``` + ## 自定义逻辑删除处理功能 在社区中,有许多用户提出希望使用时间类型,当删除时,设置删除字段为`当前时间`,当正常时,设置为 `0` 或者 `null`。 From 572bc87e51aa4d0cd30a0dca1017b507088485e4 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 22:00:58 +0800 Subject: [PATCH 06/13] =?UTF-8?q?refactor:=20=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E9=80=BB=E8=BE=91=E5=88=A0=E9=99=A4=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=E5=90=8D=E7=A7=B0=EF=BC=8C=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/logicdelete/LogicDeleteManager.java | 4 ++-- ...eteProcessorImpl.java => DefaultLogicDeleteProcessor.java} | 2 +- .../test/java/com/mybatisflex/coretest/LogicDeleteTest.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/{DefaultLogicDeleteProcessorImpl.java => DefaultLogicDeleteProcessor.java} (96%) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java index 2a0e2235..4ef6d8ee 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/LogicDeleteManager.java @@ -15,7 +15,7 @@ */ package com.mybatisflex.core.logicdelete; -import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessorImpl; +import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessor; import java.util.function.Supplier; @@ -27,7 +27,7 @@ public class LogicDeleteManager { private LogicDeleteManager() { } - private static LogicDeleteProcessor processor = new DefaultLogicDeleteProcessorImpl(); + private static LogicDeleteProcessor processor = new DefaultLogicDeleteProcessor(); private static final ThreadLocal skipFlags = new ThreadLocal<>(); /** diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessor.java similarity index 96% rename from mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java rename to mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessor.java index acae4807..6caf10cf 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessorImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessor.java @@ -27,7 +27,7 @@ import static com.mybatisflex.core.constant.SqlConsts.SINGLE_QUOTE; /** * 默认逻辑删除处理器。 */ -public class DefaultLogicDeleteProcessorImpl extends AbstractLogicDeleteProcessor { +public class DefaultLogicDeleteProcessor extends AbstractLogicDeleteProcessor { @Override public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) { diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java index 1994733c..dc19d3a7 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java @@ -21,7 +21,7 @@ import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.logicdelete.LogicDeleteProcessor; import com.mybatisflex.core.logicdelete.impl.BooleanLogicDeleteProcessor; import com.mybatisflex.core.logicdelete.impl.DateTimeLogicDeleteProcessor; -import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessorImpl; +import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessor; import com.mybatisflex.core.logicdelete.impl.IntegerLogicDeleteProcessor; import org.junit.Test; @@ -39,7 +39,7 @@ public class LogicDeleteTest { @Test public void test() { - print("DefaultLogicDeleteProcessor", new DefaultLogicDeleteProcessorImpl()); + print("DefaultLogicDeleteProcessor", new DefaultLogicDeleteProcessor()); print("BooleanLogicDeleteProcessor", new BooleanLogicDeleteProcessor()); print("IntegerLogicDeleteProcessor", new IntegerLogicDeleteProcessor()); print("DateTimeLogicDeleteProcessor", new DateTimeLogicDeleteProcessor()); From cfaaa711c262087a64a9cff1f80463c5ffb891e9 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 20 Jun 2023 22:44:13 +0800 Subject: [PATCH 07/13] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=E5=8F=A5?= =?UTF-8?q?=E5=8F=B7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/logicdelete/package-info.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/package-info.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/package-info.java index 470c1d97..db112303 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/package-info.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/package-info.java @@ -15,6 +15,6 @@ */ /** - * 逻辑删除相关功能 + * 逻辑删除相关功能。 */ package com.mybatisflex.core.logicdelete; \ No newline at end of file From 0e132dded96a696528b199631a9469c1baa98d0f Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 10:00:09 +0800 Subject: [PATCH 08/13] =?UTF-8?q?doc:=20=E4=BF=AE=E5=A4=8D=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E4=BB=A3=E7=A0=81=E9=94=99=E8=AF=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/core/logic-delete.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/core/logic-delete.md b/docs/zh/core/logic-delete.md index 4bc2af17..b6c4b219 100644 --- a/docs/zh/core/logic-delete.md +++ b/docs/zh/core/logic-delete.md @@ -142,7 +142,7 @@ MyBatis-Flex 提供了三种字段类型对应的逻辑删除处理器,用户 使用时,只需通过 `LogicDeleteManager` 来设置逻辑删除处理器即可,例如: ```java -LogicDeleteManager.setProcessor(new DateTimeLogicDeleteProcessor); +LogicDeleteManager.setProcessor(new DateTimeLogicDeleteProcessor()); ``` ## 自定义逻辑删除处理功能 From 9166c6b32c746751a29e17393fbc3a995a22498b Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 10:03:16 +0800 Subject: [PATCH 09/13] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20Override=20?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/logicdelete/impl/DefaultLogicDeleteProcessor.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessor.java index 6caf10cf..a4f54e55 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DefaultLogicDeleteProcessor.java @@ -36,6 +36,7 @@ public class DefaultLogicDeleteProcessor extends AbstractLogicDeleteProcessor { , FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete())); } + @Override protected Object getLogicNormalValue() { Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete(); if (normalValueOfLogicDelete instanceof Number @@ -45,6 +46,7 @@ public class DefaultLogicDeleteProcessor extends AbstractLogicDeleteProcessor { return SINGLE_QUOTE + normalValueOfLogicDelete + SINGLE_QUOTE; } + @Override protected Object getLogicDeletedValue() { Object deletedValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getDeletedValueOfLogicDelete(); if (deletedValueOfLogicDelete instanceof Number From 1d769c8d413c82518ac1c82a9bbddf996340c19a Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 10:51:00 +0800 Subject: [PATCH 10/13] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20IS=20NUL?= =?UTF-8?q?L=20=E4=BB=A3=E6=9B=BF=20=3D=20null=20=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/DateTimeLogicDeleteProcessor.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java index c388af3f..3b33a050 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/DateTimeLogicDeleteProcessor.java @@ -16,8 +16,11 @@ package com.mybatisflex.core.logicdelete.impl; +import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor; -import com.mybatisflex.core.query.RawFragment; +import com.mybatisflex.core.query.QueryColumn; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.table.TableInfo; /** * {@link java.time.LocalDateTime} 类型的属性对应的逻辑删除处理器。 @@ -27,6 +30,17 @@ import com.mybatisflex.core.query.RawFragment; */ public class DateTimeLogicDeleteProcessor extends AbstractLogicDeleteProcessor { + @Override + public String buildLogicNormalCondition(String logicColumn, IDialect dialect) { + return dialect.wrap(logicColumn) + " IS NULL"; + } + + @Override + public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo) { + QueryColumn queryColumn = new QueryColumn(tableInfo.getSchema(), tableInfo.getTableName(), tableInfo.getLogicDeleteColumn()); + queryWrapper.and(queryColumn.isNull()); + } + /** * 逻辑删除字段值为 {@code null} 表示数据未删除。 */ @@ -40,7 +54,7 @@ public class DateTimeLogicDeleteProcessor extends AbstractLogicDeleteProcessor { */ @Override protected Object getLogicDeletedValue() { - return new RawFragment("NOW()"); + return "NOW()"; } } \ No newline at end of file From c2ea0853688075993cb71f2a031de972b7dec3cd Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 10:52:21 +0800 Subject: [PATCH 11/13] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20LongLogicDel?= =?UTF-8?q?eteProcessor=20=E5=AF=B9=E5=BA=94=E6=97=B6=E9=97=B4=E6=88=B3?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=95=B0=E6=8D=AE=E5=AD=97=E6=AE=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/LongLogicDeleteProcessor.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/LongLogicDeleteProcessor.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/LongLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/LongLogicDeleteProcessor.java new file mode 100644 index 00000000..2f05176e --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/LongLogicDeleteProcessor.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.impl; + +import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor; + +/** + * {@link Long} 类型的属性对应的逻辑删除处理器。 + * + * @author 王帅 + * @since 2023-06-21 + */ +public class LongLogicDeleteProcessor extends AbstractLogicDeleteProcessor { + + /** + * 逻辑删除字段值为 {@code 0} 表示数据未删除。 + */ + @Override + protected Object getLogicNormalValue() { + return "0"; + } + + /** + * 逻辑删除字段值为 {@code NOW()} 表示数据删除,并记录删除时时间戳。 + */ + @Override + protected Object getLogicDeletedValue() { + return "NOW()"; + } + +} \ No newline at end of file From cc8564798005a5a951f038dcc5ac9e2bda6f620c Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 10:52:33 +0800 Subject: [PATCH 12/13] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E5=A4=84=E7=90=86=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/com/mybatisflex/coretest/LogicDeleteTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java index dc19d3a7..2974fddb 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java @@ -19,10 +19,7 @@ package com.mybatisflex.coretest; import com.mybatisflex.core.dialect.DialectFactory; import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.logicdelete.LogicDeleteProcessor; -import com.mybatisflex.core.logicdelete.impl.BooleanLogicDeleteProcessor; -import com.mybatisflex.core.logicdelete.impl.DateTimeLogicDeleteProcessor; -import com.mybatisflex.core.logicdelete.impl.DefaultLogicDeleteProcessor; -import com.mybatisflex.core.logicdelete.impl.IntegerLogicDeleteProcessor; +import com.mybatisflex.core.logicdelete.impl.*; import org.junit.Test; /** @@ -43,6 +40,7 @@ public class LogicDeleteTest { print("BooleanLogicDeleteProcessor", new BooleanLogicDeleteProcessor()); print("IntegerLogicDeleteProcessor", new IntegerLogicDeleteProcessor()); print("DateTimeLogicDeleteProcessor", new DateTimeLogicDeleteProcessor()); + print("LongLogicDeleteProcessor", new LongLogicDeleteProcessor()); } public void print(String type, LogicDeleteProcessor processor) { From 65ffdd15d1c798fddc12142726635b21a9f08167 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 10:53:35 +0800 Subject: [PATCH 13/13] =?UTF-8?q?doc:=20=E6=B7=BB=E5=8A=A0=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E5=A4=84=E7=90=86=E5=99=A8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=87=E6=A1=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/core/logic-delete.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/zh/core/logic-delete.md b/docs/zh/core/logic-delete.md index b6c4b219..f40968e5 100644 --- a/docs/zh/core/logic-delete.md +++ b/docs/zh/core/logic-delete.md @@ -138,6 +138,7 @@ MyBatis-Flex 提供了三种字段类型对应的逻辑删除处理器,用户 | IntegerLogicDeleteProcessor | integer | 0 | 1 | | BooleanLogicDeleteProcessor | tinyint | false | true | | DateTimeLogicDeleteProcessor | datetime | null | 被删除时间 | +| LongLogicDeleteProcessor | bigint | 0 | 被删除时的时间戳 | 使用时,只需通过 `LogicDeleteManager` 来设置逻辑删除处理器即可,例如: