LombokPlugin 配合 IncrementsPlugin
This commit is contained in:
@@ -17,7 +17,9 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.*;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.enhanced.SpecTypeArgumentsFullyQualifiedJavaType;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.hook.IIncrementsPluginHook;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.hook.ILombokPluginHook;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
@@ -40,7 +42,7 @@ import java.util.List;
|
||||
* @time:2017/6/19 15:20
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginHook, IIncrementsPluginHook {
|
||||
public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginHook, IIncrementsPluginHook, ILombokPluginHook {
|
||||
public static final String PRO_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
|
||||
public static final String FIELD_INC_MAP = "incrementsColumnsInfoMap"; // 为了防止和用户数据库字段冲突,特殊命名
|
||||
public static final String METHOD_INC_CHECK = "hasIncsForColumn"; // inc 检查方法名称
|
||||
@@ -55,8 +57,8 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
|
||||
public boolean validate(List<String> warnings) {
|
||||
|
||||
// 插件使用前提是使用了ModelBuilderPlugin插件
|
||||
if (!PluginTools.checkDependencyPlugin(getContext(), ModelBuilderPlugin.class)) {
|
||||
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin插件使用!");
|
||||
if (!(PluginTools.checkDependencyPlugin(getContext(), ModelBuilderPlugin.class) || PluginTools.checkDependencyPlugin(getContext(), LombokPlugin.class))) {
|
||||
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件需配合" + ModelBuilderPlugin.class.getTypeName() + "或者" + LombokPlugin.class.getTypeName() + "插件使用!");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -145,6 +147,173 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
|
||||
return super.sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(element, introspectedTable);
|
||||
}
|
||||
|
||||
// =============================================== ILombokPluginHook ===================================================
|
||||
|
||||
@Override
|
||||
public boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
return this.lombokBuilderClassGenerated(topLevelClass, columns, introspectedTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
return this.lombokBuilderClassGenerated(topLevelClass, columns, introspectedTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
return this.lombokBuilderClassGenerated(topLevelClass, columns, introspectedTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lombok Builder 生成
|
||||
* @param topLevelClass
|
||||
* @param columns
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
private boolean lombokBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
if (incTools.support()) {
|
||||
boolean find = false;
|
||||
for (IntrospectedColumn column : columns) {
|
||||
if (incTools.supportColumn(column)) {
|
||||
find = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (find) {
|
||||
// ----------------------------------- topLevelClass 方法 --------------------------------
|
||||
FullyQualifiedJavaType builderType = new FullyQualifiedJavaType(topLevelClass.getType().getShortName() + "." + topLevelClass.getType().getShortName() + "Builder");
|
||||
builderType.addTypeArgument(new SpecTypeArgumentsFullyQualifiedJavaType("<?, ?>"));
|
||||
|
||||
// 增加构造函数
|
||||
Method constructor = new Method(topLevelClass.getType().getShortName());
|
||||
commentGenerator.addGeneralMethodComment(constructor, introspectedTable);
|
||||
constructor.setVisibility(JavaVisibility.PROTECTED);
|
||||
constructor.setConstructor(true);
|
||||
constructor.addParameter(new Parameter(builderType, "builder"));
|
||||
// 是否调用父类构造函数
|
||||
if (topLevelClass.getSuperClass() != null) {
|
||||
constructor.addBodyLine("super(builder);");
|
||||
}
|
||||
for (IntrospectedColumn column : columns) {
|
||||
Field field = JavaBeansUtil.getJavaBeansField(column, context, introspectedTable);
|
||||
constructor.addBodyLine("this." + field.getName() + " = builder." + field.getName() + ";");
|
||||
}
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, constructor);
|
||||
|
||||
// 增加静态builder方法实现和lombok一样
|
||||
Method builderMethod = JavaElementGeneratorTools.generateMethod(
|
||||
"builder",
|
||||
JavaVisibility.PUBLIC,
|
||||
builderType
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(builderMethod, introspectedTable);
|
||||
builderMethod.setStatic(true);
|
||||
builderMethod.addBodyLine("return new " + topLevelClass.getType().getShortName() + "." + topLevelClass.getType().getShortName() + "BuilderImpl();");
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, builderMethod);
|
||||
|
||||
// ------------------------------ builderImpl Class ----------------------------------
|
||||
InnerClass builderImplCls = new InnerClass(topLevelClass.getType().getShortName() + "BuilderImpl");
|
||||
commentGenerator.addClassComment(builderImplCls, introspectedTable);
|
||||
FullyQualifiedJavaType builderType1 = new FullyQualifiedJavaType(topLevelClass.getType().getShortName() + "." + topLevelClass.getType().getShortName() + "Builder");
|
||||
builderType1.addTypeArgument(new SpecTypeArgumentsFullyQualifiedJavaType(
|
||||
"<" + topLevelClass.getType().getShortName() + ", " + topLevelClass.getType().getShortName() + "." + topLevelClass.getType().getShortName() + "BuilderImpl" + ">"
|
||||
));
|
||||
builderImplCls.setSuperClass(builderType1);
|
||||
builderImplCls.setVisibility(JavaVisibility.PRIVATE);
|
||||
builderImplCls.setFinal(true);
|
||||
builderImplCls.setStatic(true);
|
||||
|
||||
topLevelClass.addInnerClass(builderImplCls);
|
||||
|
||||
// self 方法
|
||||
Method selfMethod = JavaElementGeneratorTools.generateMethod(
|
||||
"self",
|
||||
JavaVisibility.PROTECTED,
|
||||
new FullyQualifiedJavaType(topLevelClass.getType().getShortName() + "." + topLevelClass.getType().getShortName() + "BuilderImpl")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(selfMethod, introspectedTable);
|
||||
selfMethod.addBodyLine("return this;");
|
||||
FormatTools.addMethodWithBestPosition(builderImplCls, selfMethod);
|
||||
|
||||
// build 方法
|
||||
Method buildMethod = JavaElementGeneratorTools.generateMethod(
|
||||
"build",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType()
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(buildMethod, introspectedTable);
|
||||
buildMethod.addBodyLine("return new " + topLevelClass.getType().getShortName() + "(this);");
|
||||
FormatTools.addMethodWithBestPosition(builderImplCls, buildMethod);
|
||||
|
||||
// ------------------------------ builder Class ----------------------------------
|
||||
InnerClass builderCls = new InnerClass(topLevelClass.getType().getShortName() + "Builder");
|
||||
commentGenerator.addClassComment(builderCls, introspectedTable);
|
||||
builderCls.setVisibility(JavaVisibility.PUBLIC);
|
||||
builderCls.setStatic(true);
|
||||
builderCls.setAbstract(true);
|
||||
|
||||
builderCls.getType().addTypeArgument(
|
||||
new SpecTypeArgumentsFullyQualifiedJavaType("<C extends " + topLevelClass.getType().getShortName()
|
||||
+ ", B extends " + topLevelClass.getType().getShortName()
|
||||
+ "." + topLevelClass.getType().getShortName() + "Builder<C, B>>")
|
||||
);
|
||||
|
||||
if (topLevelClass.getSuperClass() != null){
|
||||
FullyQualifiedJavaType superBuilderCls = new FullyQualifiedJavaType(topLevelClass.getSuperClass().getShortName() + "Builder");
|
||||
superBuilderCls.addTypeArgument(new SpecTypeArgumentsFullyQualifiedJavaType("<C, B>"));
|
||||
builderCls.setSuperClass(superBuilderCls);
|
||||
}
|
||||
|
||||
// 类注解
|
||||
topLevelClass.addImportedType(LombokPlugin.EnumLombokAnnotations.SETTER.getClazz());
|
||||
builderCls.addAnnotation(LombokPlugin.EnumLombokAnnotations.SETTER.getAnnotation());
|
||||
topLevelClass.addImportedType(LombokPlugin.EnumLombokAnnotations.ACCESSORS_FLUENT_TRUE.getClazz());
|
||||
builderCls.addAnnotation(LombokPlugin.EnumLombokAnnotations.ACCESSORS_FLUENT_TRUE.getAnnotation());
|
||||
if (topLevelClass.getSuperClass() != null){
|
||||
topLevelClass.addImportedType(LombokPlugin.EnumLombokAnnotations.TO_STRING_CALL_SUPER.getClazz());
|
||||
builderCls.addAnnotation(LombokPlugin.EnumLombokAnnotations.TO_STRING_CALL_SUPER.getAnnotation());
|
||||
} else {
|
||||
topLevelClass.addImportedType(LombokPlugin.EnumLombokAnnotations.TO_STRING.getClazz());
|
||||
builderCls.addAnnotation(LombokPlugin.EnumLombokAnnotations.TO_STRING.getAnnotation());
|
||||
}
|
||||
|
||||
|
||||
for (IntrospectedColumn introspectedColumn : columns) {
|
||||
Field field = JavaBeansUtil.getJavaBeansField(introspectedColumn, context, introspectedTable);
|
||||
field.getJavaDocLines().clear();
|
||||
commentGenerator.addFieldComment(field, introspectedTable);
|
||||
|
||||
builderCls.addField(field);
|
||||
}
|
||||
|
||||
// self 方法
|
||||
Method selfMethod1 = JavaElementGeneratorTools.generateMethod(
|
||||
"self",
|
||||
JavaVisibility.PROTECTED,
|
||||
new FullyQualifiedJavaType("B")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(selfMethod1, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(builderCls, selfMethod1);
|
||||
|
||||
// build 方法
|
||||
Method buildMethod1 = JavaElementGeneratorTools.generateMethod(
|
||||
"build",
|
||||
JavaVisibility.PUBLIC,
|
||||
new FullyQualifiedJavaType("C")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(buildMethod1, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(builderCls, buildMethod1);
|
||||
|
||||
|
||||
topLevelClass.addInnerClass(builderCls);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============================================== IModelBuilderPluginHook ===================================================
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.hook.ILombokPluginHook;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.Method;
|
||||
@@ -79,7 +82,8 @@ public class LombokPlugin extends BasePlugin {
|
||||
}
|
||||
|
||||
// @Builder
|
||||
if (this.hasBuilder) {
|
||||
List<IntrospectedColumn> columns = IntrospectedTableTools.getModelBaseRecordClomns(introspectedTable);
|
||||
if (this.hasBuilder && PluginTools.getHook(ILombokPluginHook.class).modelBaseRecordBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
|
||||
// 有子类或者父类
|
||||
if (introspectedTable.getRules().generateRecordWithBLOBsClass() || introspectedTable.getRules().generatePrimaryKeyClass() || topLevelClass.getSuperClass() != null) {
|
||||
this.addAnnotations(topLevelClass, EnumLombokAnnotations.SUPER_BUILDER);
|
||||
@@ -114,7 +118,8 @@ public class LombokPlugin extends BasePlugin {
|
||||
}
|
||||
|
||||
// @Builder
|
||||
if (this.hasBuilder) {
|
||||
List<IntrospectedColumn> columns = introspectedTable.getPrimaryKeyColumns();
|
||||
if (this.hasBuilder && PluginTools.getHook(ILombokPluginHook.class).modelPrimaryKeyBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
|
||||
// 有子类或者父类
|
||||
if (introspectedTable.getRules().generateRecordWithBLOBsClass() || introspectedTable.getRules().generateBaseRecordClass() || topLevelClass.getSuperClass() != null) {
|
||||
this.addAnnotations(topLevelClass, EnumLombokAnnotations.SUPER_BUILDER);
|
||||
@@ -149,7 +154,8 @@ public class LombokPlugin extends BasePlugin {
|
||||
}
|
||||
|
||||
// @Builder
|
||||
if (this.hasBuilder) {
|
||||
List<IntrospectedColumn> columns = introspectedTable.getBLOBColumns();
|
||||
if (this.hasBuilder && PluginTools.getHook(ILombokPluginHook.class).modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
|
||||
// 有子类或者父类
|
||||
if (introspectedTable.getRules().generateBaseRecordClass() || introspectedTable.getRules().generatePrimaryKeyClass() || topLevelClass.getSuperClass() != null) {
|
||||
this.addAnnotations(topLevelClass, EnumLombokAnnotations.SUPER_BUILDER);
|
||||
@@ -216,6 +222,9 @@ public class LombokPlugin extends BasePlugin {
|
||||
ALL_ARGS_CONSTRUCTOR("@AllArgsConstructor", "lombok.AllArgsConstructor"),
|
||||
NO_ARGS_CONSTRUCTOR("@NoArgsConstructor", "lombok.NoArgsConstructor"),
|
||||
EQUALS_AND_HASH_CODE_CALL_SUPER("@EqualsAndHashCode(callSuper = true)", "lombok.EqualsAndHashCode"),
|
||||
SETTER("@Setter", "lombok.Setter"),
|
||||
ACCESSORS_FLUENT_TRUE("@Accessors(fluent = true)", "lombok.experimental.Accessors"),
|
||||
TO_STRING("@ToString", "lombok.ToString"),
|
||||
TO_STRING_CALL_SUPER("@ToString(callSuper = true)", "lombok.ToString");
|
||||
|
||||
private final String annotation;
|
||||
|
||||
+46
-3
@@ -28,12 +28,12 @@ import org.mybatis.generator.internal.util.StringUtility;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* IntrospectedTable 的一些拓展增强
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* @author: hewei
|
||||
* @time:2017/6/13 13:48
|
||||
* ---------------------------------------------------------------------------
|
||||
@@ -42,7 +42,6 @@ public class IntrospectedTableTools {
|
||||
|
||||
/**
|
||||
* 设置DomainObjectName和MapperName
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @param context
|
||||
* @param domainObjectName
|
||||
@@ -84,7 +83,6 @@ public class IntrospectedTableTools {
|
||||
|
||||
/**
|
||||
* 安全获取column 通过正则获取的name可能包含beginningDelimiter&&endingDelimiter
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @param columnName
|
||||
* @return
|
||||
@@ -104,4 +102,49 @@ public class IntrospectedTableTools {
|
||||
|
||||
return introspectedTable.getColumn(columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取生成model baseRecord的列
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
public static List<IntrospectedColumn> getModelBaseRecordClomns(IntrospectedTable introspectedTable) {
|
||||
List<IntrospectedColumn> introspectedColumns;
|
||||
if (includePrimaryKeyColumns(introspectedTable)) {
|
||||
if (includeBLOBColumns(introspectedTable)) {
|
||||
introspectedColumns = introspectedTable.getAllColumns();
|
||||
} else {
|
||||
introspectedColumns = introspectedTable.getNonBLOBColumns();
|
||||
}
|
||||
} else {
|
||||
if (includeBLOBColumns(introspectedTable)) {
|
||||
introspectedColumns = introspectedTable
|
||||
.getNonPrimaryKeyColumns();
|
||||
} else {
|
||||
introspectedColumns = introspectedTable.getBaseColumns();
|
||||
}
|
||||
}
|
||||
|
||||
return introspectedColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有primaryKey 列
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
public static boolean includePrimaryKeyColumns(IntrospectedTable introspectedTable) {
|
||||
return !introspectedTable.getRules().generatePrimaryKeyClass()
|
||||
&& introspectedTable.hasPrimaryKeyColumns();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有 blob 列
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
public static boolean includeBLOBColumns(IntrospectedTable introspectedTable) {
|
||||
return !introspectedTable.getRules().generateRecordWithBLOBsClass()
|
||||
&& introspectedTable.hasBLOBColumns();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,11 +49,16 @@ public class PluginTools {
|
||||
/**
|
||||
* 检查插件依赖
|
||||
* @param context 上下文
|
||||
* @param plugin 插件
|
||||
* @param plugins 插件
|
||||
* @return
|
||||
*/
|
||||
public static boolean checkDependencyPlugin(Context context, Class plugin) {
|
||||
return getPluginIndex(context, plugin) >= 0;
|
||||
public static boolean checkDependencyPlugin(Context context, Class... plugins) {
|
||||
for (Class plugin : plugins) {
|
||||
if (getPluginIndex(context, plugin) < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.enhanced;
|
||||
|
||||
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2018/11/2 18:21
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class SpecTypeArgumentsFullyQualifiedJavaType extends FullyQualifiedJavaType {
|
||||
private String fullTypeSpecification;
|
||||
/**
|
||||
* Use this constructor to construct a generic type with the specified type parameters.
|
||||
* @param fullTypeSpecification the full type specification
|
||||
*/
|
||||
public SpecTypeArgumentsFullyQualifiedJavaType(String fullTypeSpecification) {
|
||||
super("");
|
||||
|
||||
this.fullTypeSpecification = fullTypeSpecification;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getShortName() {
|
||||
return this.fullTypeSpecification.substring(1, this.fullTypeSpecification.length() - 1);
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,7 @@ public class HookAggregator implements IUpsertPluginHook,
|
||||
IOptimisticLockerPluginHook,
|
||||
ISelectOneByExamplePluginHook,
|
||||
ITableConfigurationHook,
|
||||
ILombokPluginHook,
|
||||
ILogicalDeletePluginHook {
|
||||
|
||||
protected static final Logger logger = LoggerFactory.getLogger(BasePlugin.class); // 日志
|
||||
@@ -308,4 +309,38 @@ public class HookAggregator implements IUpsertPluginHook,
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ============================================= ILombokPluginHook ==============================================
|
||||
|
||||
|
||||
@Override
|
||||
public boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
for (ILombokPluginHook plugin : this.getPlugins(ILombokPluginHook.class)) {
|
||||
if (!plugin.modelBaseRecordBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
for (ILombokPluginHook plugin : this.getPlugins(ILombokPluginHook.class)) {
|
||||
if (!plugin.modelPrimaryKeyBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
|
||||
for (ILombokPluginHook plugin : this.getPlugins(ILombokPluginHook.class)) {
|
||||
if (!plugin.modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.TopLevelClass;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2018/10/31 17:27
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public interface ILombokPluginHook {
|
||||
/**
|
||||
* Model builder class 生成
|
||||
* @param topLevelClass
|
||||
* @param columns
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable);
|
||||
|
||||
/**
|
||||
* Model builder class 生成
|
||||
* @param topLevelClass
|
||||
* @param columns
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable);
|
||||
|
||||
/**
|
||||
* Model builder class 生成
|
||||
* @param topLevelClass
|
||||
* @param columns
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable);
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public class IncrementsPluginTest {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-without-model-builder-plugin.xml");
|
||||
tool.generate();
|
||||
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.IncrementsPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin插件使用!");
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.IncrementsPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin或者com.itfsw.mybatis.generator.plugins.LombokPlugin插件使用!");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ public class IncrementsPluginTest {
|
||||
@Test
|
||||
public void testModelBuilderMethod() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml");
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试生成的方法
|
||||
@@ -112,7 +112,7 @@ public class IncrementsPluginTest {
|
||||
@Test
|
||||
public void testSqlAndExecute() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml");
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
|
||||
@@ -214,7 +214,7 @@ public class IncrementsPluginTest {
|
||||
@Test
|
||||
public void testWithSelectiveEnhancedPlugin() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-selective-enhanced-plugin.xml");
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试updateByExampleSelective
|
||||
@@ -307,7 +307,7 @@ public class IncrementsPluginTest {
|
||||
@Test
|
||||
public void testWithUpsertPlugin() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-plugin.xml");
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
@@ -399,7 +399,7 @@ public class IncrementsPluginTest {
|
||||
@Test
|
||||
public void testWithAutoDelimitKeywords() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml");
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试updateByExample、updateByExampleSelective
|
||||
@@ -433,7 +433,7 @@ public class IncrementsPluginTest {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml");
|
||||
|
||||
// upsertSelective 基于原生非空判断
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
@@ -459,7 +459,7 @@ public class IncrementsPluginTest {
|
||||
});
|
||||
|
||||
// upsertByExampleSelective 基于原生非空判断
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试updateByExampleSelective
|
||||
@@ -499,7 +499,7 @@ public class IncrementsPluginTest {
|
||||
});
|
||||
|
||||
// upsertSelective 基于指定字段
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
@@ -536,7 +536,7 @@ public class IncrementsPluginTest {
|
||||
});
|
||||
|
||||
// upsertByExampleSelective 基于指定字段
|
||||
tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试updateByExampleSelective
|
||||
@@ -585,4 +585,33 @@ public class IncrementsPluginTest {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试同时整合 LombokPlugin
|
||||
*/
|
||||
@Test
|
||||
public void testWithLombokPlugin() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-LombokPlugin.xml");
|
||||
|
||||
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init-lombok.sql"), new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// ------------------------------------- builder ----------------------------------------
|
||||
// normal builder
|
||||
ObjectUtil tbBuilder = new ObjectUtil(loader.loadClass(packagz + ".Tb").getMethod("builder").invoke(null));
|
||||
tbBuilder = new ObjectUtil(tbBuilder.invoke("id", 1L));
|
||||
tbBuilder.invoke("field1", "ts1");
|
||||
ObjectUtil tb = new ObjectUtil(tbBuilder.invoke("build"));
|
||||
Assert.assertEquals(tb.invoke("toString"), "Tb(id=1, field1=ts1, field2=null)");
|
||||
// super
|
||||
ObjectUtil tbLombokWithBLOBsBuilder = new ObjectUtil(loader.loadClass(packagz + ".TbLombokWithBLOBs").getMethod("builder").invoke(null));
|
||||
tbLombokWithBLOBsBuilder.invoke("field3", "ts3");
|
||||
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null), field1=null, incF1=null), field3=ts3, field4=null)");
|
||||
tbLombokWithBLOBsBuilder.invoke("field1", "ts1");
|
||||
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=null, key1=null), field1=ts1, incF1=null), field3=ts3, field4=null)");
|
||||
tbLombokWithBLOBsBuilder.invoke("id", 100L);
|
||||
Assert.assertEquals(tbLombokWithBLOBsBuilder.invoke("toString"), "TbLombokWithBLOBs.TbLombokWithBLOBsBuilder(super=TbLombok.TbLombokBuilder(super=TbLombokKey.TbLombokKeyBuilder(id=100, key1=null), field1=ts1, incF1=null), field3=ts3, field4=null)");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,6 +151,7 @@ public class ObjectUtil {
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
method.setAccessible(true);
|
||||
return method.invoke(this.object, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
Navicat MySQL Data Transfer
|
||||
|
||||
Source Server : localhost
|
||||
Source Server Version : 50617
|
||||
Source Host : localhost:3306
|
||||
Source Database : mybatis-generator-plugin
|
||||
|
||||
Target Server Type : MYSQL
|
||||
Target Server Version : 50617
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 2017-07-03 17:34:11
|
||||
*/
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb`;
|
||||
CREATE TABLE `tb` (
|
||||
`id` bigint(20) NOT NULL COMMENT '注释1',
|
||||
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
|
||||
`field2` int(11) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of tb
|
||||
-- ----------------------------
|
||||
INSERT INTO `tb` VALUES ('1', 'fd1', '0');
|
||||
INSERT INTO `tb` VALUES ('2', 'fd2', '1');
|
||||
INSERT INTO `tb` VALUES ('3', null, '3');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb_keys
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb_keys`;
|
||||
CREATE TABLE `tb_keys` (
|
||||
`key1` bigint(20) NOT NULL COMMENT '注释1',
|
||||
`key2` varchar(255) NOT NULL,
|
||||
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
|
||||
`field2` int(11) DEFAULT NULL,
|
||||
`inc_f1` bigint(20) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`key1`,`key2`)
|
||||
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of tb_keys
|
||||
-- ----------------------------
|
||||
INSERT INTO `tb_keys` VALUES ('1', 'key1', 'fd1', '0', '1');
|
||||
INSERT INTO `tb_keys` VALUES ('2', 'key2', 'fd2', '1', '2');
|
||||
INSERT INTO `tb_keys` VALUES ('3', 'key3', null, '3', '3');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb_lombok
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb_lombok`;
|
||||
CREATE TABLE `tb_lombok` (
|
||||
`id` bigint(20) NOT NULL COMMENT '注释1',
|
||||
`key1` varchar(20) NOT NULL,
|
||||
`field1` varchar(10) COMMENT '注释2',
|
||||
`inc_f1` tinyint(1),
|
||||
`field3` longtext,
|
||||
`field4` longtext,
|
||||
PRIMARY KEY (`id`,`key1`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of tb_lombok
|
||||
-- ----------------------------
|
||||
INSERT INTO `tb_lombok` VALUES ('1', 'key1', 'fd1', '0', 'xx1', null);
|
||||
INSERT INTO `tb_lombok` VALUES ('2', 'key2', 'fd2', '1', 'xx2', 'ss2');
|
||||
INSERT INTO `tb_lombok` VALUES ('3', 'key3', null, '3', 'xx3', null);
|
||||
@@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<!DOCTYPE generatorConfiguration
|
||||
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
|
||||
<generatorConfiguration>
|
||||
<properties resource="db.properties"/>
|
||||
|
||||
<!--导入属性配置 -->
|
||||
<context id="default" targetRuntime="MyBatis3">
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.IncrementsPlugin" />
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.LombokPlugin">
|
||||
<property name="@Builder" value="true"/>
|
||||
</plugin>
|
||||
|
||||
<!--jdbc的数据库连接 -->
|
||||
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
|
||||
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
|
||||
targetPackage 指定生成的model生成所在的包名
|
||||
targetProject 指定在该项目下所在的路径 -->
|
||||
<javaModelGenerator targetPackage="" targetProject="">
|
||||
<!-- 是否对model添加 构造函数 -->
|
||||
<!-- 给Model添加一个父类 -->
|
||||
<!--<property name="rootClass" value="com.itfsw.base"/>-->
|
||||
</javaModelGenerator>
|
||||
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
|
||||
<sqlMapGenerator targetPackage="" targetProject="" />
|
||||
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
|
||||
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
|
||||
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
|
||||
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
|
||||
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
|
||||
|
||||
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<table tableName="tb">
|
||||
<property name="incrementsColumns" value="field2"/>
|
||||
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
<table tableName="tb_keys">
|
||||
<property name="incrementsColumns" value="inc_f1, field2"/>
|
||||
</table>
|
||||
<table tableName="tb_lombok">
|
||||
<property name="incrementsColumns" value="inc_f1"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user