From 3df69f4bb747ce0b81166742dc51a3f05b8a6dc2 Mon Sep 17 00:00:00 2001 From: hewei Date: Tue, 30 Oct 2018 09:44:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B9=90=E8=A7=82=E9=94=81=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/OptimisticLockerPlugin.java | 36 +++++++++- .../plugins/utils/hook/HookAggregator.java | 53 ++++++++++++++- .../utils/hook/ILogicalDeletePluginHook.java | 66 +++++++++++++++++++ 3 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILogicalDeletePluginHook.java diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java index 7fe80be..e067e4f 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java @@ -17,6 +17,7 @@ package com.itfsw.mybatis.generator.plugins; import com.itfsw.mybatis.generator.plugins.utils.*; +import com.itfsw.mybatis.generator.plugins.utils.hook.ILogicalDeletePluginHook; import com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook; import com.itfsw.mybatis.generator.plugins.utils.hook.IOptimisticLockerPluginHook; import org.mybatis.generator.api.IntrospectedColumn; @@ -38,9 +39,11 @@ import java.util.*; * @time:2018/4/26 10:24 * --------------------------------------------------------------------------- */ -public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderPluginHook { +public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderPluginHook, ILogicalDeletePluginHook { public static final String METHOD_DELETE_WITH_VERSION_BY_EXAMPLE = "deleteWithVersionByExample"; // 方法名 public static final String METHOD_DELETE_WITH_VERSION_BY_PRIMARY_KEY = "deleteWithVersionByPrimaryKey"; // 方法名 + public static final String METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE = "logicalDeleteWithVersionByExample"; // 方法名 + public static final String METHOD_LOGICAL_DELETE_WITH_VERSION_BY_PRIMARY_KEY = "logicalDeleteWithVersionByPrimaryKey"; // 方法名 public static final String METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_SELECTIVE = "updateWithVersionByExampleSelective"; // 方法名 public static final String METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITH_BLOBS = "updateWithVersionByExampleWithBLOBs"; // 方法名 public static final String METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITHOUT_BLOBS = "updateWithVersionByExample"; // 方法名 @@ -113,6 +116,28 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP return super.clientDeleteByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable); } + @Override + public boolean clientLogicalDeleteByExampleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { + if (this.versionColumn != null) { + FormatTools.addMethodWithBestPosition( + interfaze, + this.replaceDeleteExampleMethod(introspectedTable, method, interfaze, METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE) + ); + } + return true; + } + + @Override + public boolean clientLogicalDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { + if (this.versionColumn != null) { + FormatTools.addMethodWithBestPosition( + interfaze, + this.replaceDeleteExampleMethod(introspectedTable, method, interfaze, METHOD_LOGICAL_DELETE_WITH_VERSION_BY_PRIMARY_KEY) + ); + } + return true; + } + @Override public boolean clientUpdateByExampleSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { if (this.versionColumn != null) { @@ -420,6 +445,15 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP return super.sqlMapExampleWhereClauseElementGenerated(element, introspectedTable); } + @Override + public boolean sqlMapLogicalDeleteByExampleElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + return true; + } + + @Override + public boolean sqlMapLogicalDeleteByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + return true; + } // =================================================== 一些生成方法 ======================================== diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java index 59f609e..8676f11 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java @@ -43,7 +43,14 @@ import java.util.List; * @time:2018/4/27 11:33 * --------------------------------------------------------------------------- */ -public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHook, IIncrementsPluginHook, IOptimisticLockerPluginHook, ISelectOneByExamplePluginHook, ITableConfigurationHook { +public class HookAggregator implements IUpsertPluginHook, + IModelBuilderPluginHook, + IIncrementsPluginHook, + IOptimisticLockerPluginHook, + ISelectOneByExamplePluginHook, + ITableConfigurationHook, + ILogicalDeletePluginHook { + protected static final Logger logger = LoggerFactory.getLogger(BasePlugin.class); // 日志 private final static HookAggregator instance = new HookAggregator(); private Context context; @@ -256,7 +263,49 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo @Override public void tableConfiguration(IntrospectedTable introspectedTable) { if (!this.getPlugins(ITableConfigurationHook.class).isEmpty()) { - this.getPlugins(ITableConfigurationHook.class).get(0).tableConfiguration(introspectedTable); + this.getPlugins(ITableConfigurationHook.class).get(0).tableConfiguration(introspectedTable); } } + + // ============================================= ILogicalDeletePluginHook ============================================== + + @Override + public boolean clientLogicalDeleteByExampleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { + for (ILogicalDeletePluginHook plugin : this.getPlugins(ILogicalDeletePluginHook.class)) { + if (!plugin.clientLogicalDeleteByExampleMethodGenerated(method, interfaze, introspectedTable)) { + return false; + } + } + return true; + } + + @Override + public boolean clientLogicalDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { + for (ILogicalDeletePluginHook plugin : this.getPlugins(ILogicalDeletePluginHook.class)) { + if (!plugin.clientLogicalDeleteByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable)) { + return false; + } + } + return true; + } + + @Override + public boolean sqlMapLogicalDeleteByExampleElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + for (ILogicalDeletePluginHook plugin : this.getPlugins(ILogicalDeletePluginHook.class)) { + if (!plugin.sqlMapLogicalDeleteByExampleElementGenerated(element, introspectedTable)) { + return false; + } + } + return true; + } + + @Override + public boolean sqlMapLogicalDeleteByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + for (ILogicalDeletePluginHook plugin : this.getPlugins(ILogicalDeletePluginHook.class)) { + if (!plugin.sqlMapLogicalDeleteByPrimaryKeyElementGenerated(element, introspectedTable)) { + return false; + } + } + return true; + } } diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILogicalDeletePluginHook.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILogicalDeletePluginHook.java new file mode 100644 index 0000000..483e9bc --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILogicalDeletePluginHook.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2018. + * + * 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.itfsw.mybatis.generator.plugins.utils.hook; + +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.Interface; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.xml.XmlElement; + +/** + * --------------------------------------------------------------------------- + * + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2018/9/11 11:00 + * --------------------------------------------------------------------------- + */ +public interface ILogicalDeletePluginHook { + /** + * logicalDeleteByExample + * @param method + * @param interfaze + * @param introspectedTable + * @return + */ + boolean clientLogicalDeleteByExampleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable); + + /** + * logicalDeleteByPrimaryKey + * @param method + * @param interfaze + * @param introspectedTable + * @return + */ + boolean clientLogicalDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable); + + /** + * logicalDeleteByExample + * @param element + * @param introspectedTable + * @return + */ + boolean sqlMapLogicalDeleteByExampleElementGenerated(XmlElement element, IntrospectedTable introspectedTable); + + /** + * logicalDeleteByPrimaryKey + * @param element + * @param introspectedTable + * @return + */ + boolean sqlMapLogicalDeleteByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable); +}