Commit 9425e882 authored by hewei's avatar hewei

重构代码插件依赖关系

parent 09065650
...@@ -17,12 +17,14 @@ ...@@ -17,12 +17,14 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.*; import com.itfsw.mybatis.generator.plugins.utils.*;
import com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Element; import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.JavaBeansUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -35,7 +37,7 @@ import java.util.List; ...@@ -35,7 +37,7 @@ import java.util.List;
* @time:2017/6/19 15:20 * @time:2017/6/19 15:20
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class IncrementsPlugin extends BasePlugin { public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginHook {
public static final String PRO_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property public static final String PRO_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
public static final String FIELD_INC_MAP = "incrementsColumnsInfoMap"; // 为了防止和用户数据库字段冲突,特殊命名 public static final String FIELD_INC_MAP = "incrementsColumnsInfoMap"; // 为了防止和用户数据库字段冲突,特殊命名
public static final String METHOD_INC_CHECK = "hasIncsForColumn"; // inc 检查方法名称 public static final String METHOD_INC_CHECK = "hasIncsForColumn"; // inc 检查方法名称
...@@ -64,33 +66,10 @@ public class IncrementsPlugin extends BasePlugin { ...@@ -64,33 +66,10 @@ public class IncrementsPlugin extends BasePlugin {
*/ */
@Override @Override
public void initialized(IntrospectedTable introspectedTable) { public void initialized(IntrospectedTable introspectedTable) {
super.initialized(introspectedTable);
this.incTools = IncrementsPluginTools.getTools(context, introspectedTable, warnings); this.incTools = IncrementsPluginTools.getTools(context, introspectedTable, warnings);
} }
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 具体实现在 ModelBuilderPlugin.generateModelBuilder
return true;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 具体实现在 ModelBuilderPlugin.generateModelBuilder
return true;
}
/** /**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param element * @param element
...@@ -99,13 +78,8 @@ public class IncrementsPlugin extends BasePlugin { ...@@ -99,13 +78,8 @@ public class IncrementsPlugin extends BasePlugin {
*/ */
@Override @Override
public boolean sqlMapUpdateByExampleSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { public boolean sqlMapUpdateByExampleSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
if (PluginTools.checkDependencyPlugin(context, SelectiveEnhancedPlugin.class)) { generatedWithSelective(element, introspectedTable, true);
// TODO SelectiveEnhancedPlugin.sqlMapUpdateByExampleSelectiveElementGenerated return super.sqlMapUpdateByExampleSelectiveElementGenerated(element, introspectedTable);
} else {
generatedWithSelective(element, introspectedTable, true);
}
return true;
} }
/** /**
...@@ -117,7 +91,7 @@ public class IncrementsPlugin extends BasePlugin { ...@@ -117,7 +91,7 @@ public class IncrementsPlugin extends BasePlugin {
@Override @Override
public boolean sqlMapUpdateByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { public boolean sqlMapUpdateByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
generatedWithoutSelective(element, introspectedTable, true); generatedWithoutSelective(element, introspectedTable, true);
return true; return super.sqlMapUpdateByExampleWithBLOBsElementGenerated(element, introspectedTable);
} }
/** /**
...@@ -129,7 +103,7 @@ public class IncrementsPlugin extends BasePlugin { ...@@ -129,7 +103,7 @@ public class IncrementsPlugin extends BasePlugin {
@Override @Override
public boolean sqlMapUpdateByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { public boolean sqlMapUpdateByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
generatedWithoutSelective(element, introspectedTable, true); generatedWithoutSelective(element, introspectedTable, true);
return true; return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
} }
/** /**
...@@ -140,13 +114,8 @@ public class IncrementsPlugin extends BasePlugin { ...@@ -140,13 +114,8 @@ public class IncrementsPlugin extends BasePlugin {
*/ */
@Override @Override
public boolean sqlMapUpdateByPrimaryKeySelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { public boolean sqlMapUpdateByPrimaryKeySelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
if (PluginTools.checkDependencyPlugin(context, SelectiveEnhancedPlugin.class)) { generatedWithSelective(element, introspectedTable, false);
// TODO SelectiveEnhancedPlugin.sqlMapUpdateByPrimaryKeySelectiveElementGenerated return super.sqlMapUpdateByPrimaryKeySelectiveElementGenerated(element, introspectedTable);
} else {
generatedWithSelective(element, introspectedTable, false);
}
return true;
} }
/** /**
...@@ -158,7 +127,7 @@ public class IncrementsPlugin extends BasePlugin { ...@@ -158,7 +127,7 @@ public class IncrementsPlugin extends BasePlugin {
@Override @Override
public boolean sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { public boolean sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
generatedWithoutSelective(element, introspectedTable, false); generatedWithoutSelective(element, introspectedTable, false);
return true; return super.sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(element, introspectedTable);
} }
/** /**
...@@ -170,9 +139,100 @@ public class IncrementsPlugin extends BasePlugin { ...@@ -170,9 +139,100 @@ public class IncrementsPlugin extends BasePlugin {
@Override @Override
public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
generatedWithoutSelective(element, introspectedTable, false); generatedWithoutSelective(element, introspectedTable, false);
return true; return super.sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(element, introspectedTable);
} }
// =============================================== IModelBuilderPluginHook ===================================================
/**
* Model builder class 生成
* @param topLevelClass
* @param builderClass
* @param columns
* @param introspectedTable
* @return
*/
@Override
public boolean modelBuilderClassGenerated(TopLevelClass topLevelClass, InnerClass builderClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
// 增加枚举
InnerEnum eIncrements = new InnerEnum(new FullyQualifiedJavaType("Inc"));
eIncrements.setVisibility(JavaVisibility.PUBLIC);
eIncrements.setStatic(true);
eIncrements.addEnumConstant("INC(\"+\")");
eIncrements.addEnumConstant("DEC(\"-\")");
commentGenerator.addEnumComment(eIncrements, introspectedTable);
// 生成属性和构造函数
Field fValue = new Field("value", FullyQualifiedJavaType.getStringInstance());
fValue.setVisibility(JavaVisibility.PRIVATE);
fValue.setFinal(true);
commentGenerator.addFieldComment(fValue, introspectedTable);
eIncrements.addField(fValue);
Method mInc = new Method("Inc");
mInc.setConstructor(true);
mInc.addBodyLine("this.value = value;");
mInc.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "value"));
commentGenerator.addGeneralMethodComment(mInc, introspectedTable);
eIncrements.addMethod(mInc);
Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue);
commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
eIncrements.addMethod(mValue);
builderClass.addInnerEnum(eIncrements);
// 增加field
Field fIncrements = JavaElementGeneratorTools.generateField(
IncrementsPlugin.FIELD_INC_MAP,
JavaVisibility.PROTECTED,
new FullyQualifiedJavaType("Map<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">"),
"new HashMap<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">()"
);
commentGenerator.addFieldComment(fIncrements, introspectedTable);
topLevelClass.addField(fIncrements);
topLevelClass.addImportedType("java.util.Map");
topLevelClass.addImportedType("java.util.HashMap");
// getter&setter
Method mGetter = JavaElementGeneratorTools.generateGetterMethod(fIncrements);
commentGenerator.addGetterComment(mGetter, introspectedTable, null);
topLevelClass.addMethod(mGetter);
Method mSetter = JavaElementGeneratorTools.generateSetterMethod(fIncrements);
commentGenerator.addSetterComment(mSetter, introspectedTable, null);
topLevelClass.addMethod(mSetter);
// 增加判断方法
Method mHasIncsForColumn = JavaElementGeneratorTools.generateMethod(
IncrementsPlugin.METHOD_INC_CHECK,
JavaVisibility.PUBLIC,
FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
new Parameter(FullyQualifiedJavaType.getStringInstance(), "column")
);
commentGenerator.addGeneralMethodComment(mHasIncsForColumn, introspectedTable);
mHasIncsForColumn.addBodyLine("return " + IncrementsPlugin.FIELD_INC_MAP + ".get(column) != null;");
FormatTools.addMethodWithBestPosition(topLevelClass, mHasIncsForColumn);
// Builder 中 添加字段支持
for (IntrospectedColumn column : columns) {
if (incTools.supportColumn(column)) {
Field field = JavaBeansUtil.getJavaBeansField(column, context, introspectedTable);
// 增加方法
Method mIncrements = JavaElementGeneratorTools.generateMethod(
field.getName(),
JavaVisibility.PUBLIC,
builderClass.getType(),
new Parameter(field.getType(), field.getName()),
new Parameter(incTools.getIncEnum(), "inc")
);
commentGenerator.addSetterComment(mIncrements, introspectedTable, column);
Method setterMethod = JavaBeansUtil.getJavaBeansSetter(column, context, introspectedTable);
mIncrements.addBodyLine("obj." + IncrementsPlugin.FIELD_INC_MAP + ".put(\"" + column.getActualColumnName() + "\", inc);");
mIncrements.addBodyLine("obj." + setterMethod.getName() + "(" + field.getName() + ");");
mIncrements.addBodyLine("return this;");
FormatTools.addMethodWithBestPosition(builderClass, mIncrements);
}
}
return true;
}
/** /**
* 有Selective代码生成 * 有Selective代码生成
......
...@@ -17,10 +17,10 @@ ...@@ -17,10 +17,10 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
import com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools; import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType; import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType;
import com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.java.*;
...@@ -163,91 +163,10 @@ public class ModelBuilderPlugin extends BasePlugin { ...@@ -163,91 +163,10 @@ public class ModelBuilderPlugin extends BasePlugin {
build.addBodyLine("return this.obj;"); build.addBodyLine("return this.obj;");
commentGenerator.addGeneralMethodComment(build, introspectedTable); commentGenerator.addGeneralMethodComment(build, introspectedTable);
innerClass.addMethod(build); innerClass.addMethod(build);
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。"); logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。");
// hook
// ========================================== IncrementsPlugin ======================================= PluginTools.getHook(IModelBuilderPluginHook.class).modelBuilderClassGenerated(topLevelClass, innerClass, columns, introspectedTable);
IncrementsPluginTools incTools = IncrementsPluginTools.getTools(context, introspectedTable, warnings);
if (incTools.support()) {
// 增加枚举
InnerEnum eIncrements = new InnerEnum(new FullyQualifiedJavaType("Inc"));
eIncrements.setVisibility(JavaVisibility.PUBLIC);
eIncrements.setStatic(true);
eIncrements.addEnumConstant("INC(\"+\")");
eIncrements.addEnumConstant("DEC(\"-\")");
commentGenerator.addEnumComment(eIncrements, introspectedTable);
// 生成属性和构造函数
Field fValue = new Field("value", FullyQualifiedJavaType.getStringInstance());
fValue.setVisibility(JavaVisibility.PRIVATE);
fValue.setFinal(true);
commentGenerator.addFieldComment(fValue, introspectedTable);
eIncrements.addField(fValue);
Method mInc = new Method("Inc");
mInc.setConstructor(true);
mInc.addBodyLine("this.value = value;");
mInc.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "value"));
commentGenerator.addGeneralMethodComment(mInc, introspectedTable);
eIncrements.addMethod(mInc);
Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue);
commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
eIncrements.addMethod(mValue);
innerClass.addInnerEnum(eIncrements);
// 增加field
Field fIncrements = JavaElementGeneratorTools.generateField(
IncrementsPlugin.FIELD_INC_MAP,
JavaVisibility.PROTECTED,
new FullyQualifiedJavaType("Map<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">"),
"new HashMap<String, " + incTools.getIncEnum().getFullyQualifiedName() + ">()"
);
commentGenerator.addFieldComment(fIncrements, introspectedTable);
topLevelClass.addField(fIncrements);
topLevelClass.addImportedType("java.util.Map");
topLevelClass.addImportedType("java.util.HashMap");
// getter&setter
Method mGetter = JavaElementGeneratorTools.generateGetterMethod(fIncrements);
commentGenerator.addGetterComment(mGetter, introspectedTable, null);
topLevelClass.addMethod(mGetter);
Method mSetter = JavaElementGeneratorTools.generateSetterMethod(fIncrements);
commentGenerator.addSetterComment(mSetter, introspectedTable, null);
topLevelClass.addMethod(mSetter);
// 增加判断方法
Method mHasIncsForColumn = JavaElementGeneratorTools.generateMethod(
IncrementsPlugin.METHOD_INC_CHECK,
JavaVisibility.PUBLIC,
FullyQualifiedJavaType.getBooleanPrimitiveInstance(),
new Parameter(FullyQualifiedJavaType.getStringInstance(), "column")
);
commentGenerator.addGeneralMethodComment(mHasIncsForColumn, introspectedTable);
mHasIncsForColumn.addBodyLine("return " + IncrementsPlugin.FIELD_INC_MAP + ".get(column) != null;");
FormatTools.addMethodWithBestPosition(topLevelClass, mHasIncsForColumn);
// Builder 中 添加字段支持
for (IntrospectedColumn column : columns) {
if (incTools.supportColumn(column)) {
Field field = JavaBeansUtil.getJavaBeansField(column, context, introspectedTable);
// 增加方法
Method mIncrements = JavaElementGeneratorTools.generateMethod(
field.getName(),
JavaVisibility.PUBLIC,
innerClass.getType(),
new Parameter(field.getType(), field.getName()),
new Parameter(incTools.getIncEnum(), "inc")
);
commentGenerator.addSetterComment(mIncrements, introspectedTable, column);
Method setterMethod = JavaBeansUtil.getJavaBeansSetter(column, context, introspectedTable);
mIncrements.addBodyLine("obj." + IncrementsPlugin.FIELD_INC_MAP + ".put(\"" + column.getActualColumnName() + "\", inc);");
mIncrements.addBodyLine("obj." + setterMethod.getName() + "(" + field.getName() + ");");
mIncrements.addBodyLine("return this;");
FormatTools.addMethodWithBestPosition(innerClass, mIncrements);
}
}
}
return innerClass; return innerClass;
} }
......
...@@ -20,8 +20,10 @@ import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; ...@@ -20,8 +20,10 @@ import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.Plugin; import org.mybatis.generator.api.Plugin;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Interface; import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Context; import org.mybatis.generator.config.Context;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -38,7 +40,7 @@ import java.util.List; ...@@ -38,7 +40,7 @@ import java.util.List;
* @time:2018/4/27 11:33 * @time:2018/4/27 11:33
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class HookAggregator implements IUpsertPluginHook { public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHook {
protected static final Logger logger = LoggerFactory.getLogger(BasePlugin.class); // 日志 protected static final Logger logger = LoggerFactory.getLogger(BasePlugin.class); // 日志
private final static HookAggregator instance = new HookAggregator(); private final static HookAggregator instance = new HookAggregator();
private Context context; private Context context;
...@@ -67,16 +69,6 @@ public class HookAggregator implements IUpsertPluginHook { ...@@ -67,16 +69,6 @@ public class HookAggregator implements IUpsertPluginHook {
this.context = context; this.context = context;
} }
/**
* 获取挂载
* @param clazz
* @param <T>
* @return
*/
public <T> T getHook(Class<T> clazz) {
return (T) this;
}
/** /**
* 获取插件 * 获取插件
* @param clazz * @param clazz
...@@ -101,7 +93,18 @@ public class HookAggregator implements IUpsertPluginHook { ...@@ -101,7 +93,18 @@ public class HookAggregator implements IUpsertPluginHook {
return list; return list;
} }
// ================================================= default =============================================== // ============================================ IModelBuilderPluginHook =============================================
@Override
public boolean modelBuilderClassGenerated(TopLevelClass topLevelClass, InnerClass builderClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
for (IModelBuilderPluginHook plugin : this.getPlugins(IModelBuilderPluginHook.class)) {
if (!plugin.modelBuilderClassGenerated(topLevelClass, builderClass, columns, introspectedTable)) {
return false;
}
}
return true;
}
// ================================================= IUpsertPluginHook ===============================================
@Override @Override
public boolean clientUpsertSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) { public boolean clientUpsertSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
for (IUpsertPluginHook plugin : this.getPlugins(IUpsertPluginHook.class)) { for (IUpsertPluginHook plugin : this.getPlugins(IUpsertPluginHook.class)) {
......
/*
* 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.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.util.List;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/4/28 17:29
* ---------------------------------------------------------------------------
*/
public interface IModelBuilderPluginHook {
/**
* Model builder class 生成
* @param topLevelClass
* @param builderClass
* @param columns
* @param introspectedTable
* @return
*/
boolean modelBuilderClassGenerated(TopLevelClass topLevelClass, InnerClass builderClass, List<IntrospectedColumn> columns, 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