Commit 3df69f4b authored by hewei's avatar hewei

乐观锁插件

parent 858db7a8
......@@ -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;
}
// =================================================== 一些生成方法 ========================================
......
......@@ -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;
}
}
/*
* 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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment