Commit 9425e882 authored by hewei's avatar hewei

重构代码插件依赖关系

parent 09065650
...@@ -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