Commit 0a4f6162 authored by hewei's avatar hewei

ModelBuilderPlugin 增加静态builder方法

parent e75e36af
......@@ -20,12 +20,15 @@ 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.enhanced.InnerTypeFullyQualifiedJavaType;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.util.JavaBeansUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* ---------------------------------------------------------------------------
......@@ -37,6 +40,7 @@ import java.util.List;
*/
public class ModelBuilderPlugin extends BasePlugin {
public static final String BUILDER_CLASS_NAME = "Builder"; // Builder 类名
private Map<IntrospectedTable, InnerTypeFullyQualifiedJavaType> innerClasses = new HashMap<>();
/**
* Model Methods 生成
......@@ -94,6 +98,27 @@ public class ModelBuilderPlugin extends BasePlugin {
InnerClass innerClass = new InnerClass(BUILDER_CLASS_NAME);
innerClass.setVisibility(JavaVisibility.PUBLIC);
innerClass.setStatic(true);
// 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
// 顺序为 key base withBLOBs
InnerTypeFullyQualifiedJavaType builderType = new InnerTypeFullyQualifiedJavaType(topLevelClass.getType().getFullyQualifiedName() + "." + BUILDER_CLASS_NAME);
if (innerClasses.get(introspectedTable) != null) {
innerClass.setSuperClass(innerClasses.get(introspectedTable));
innerClasses.remove(introspectedTable);
}
innerClasses.put(introspectedTable, builderType);
// 增加静态builder方法实现和lombok一样
Method builder = JavaElementGeneratorTools.generateMethod(
"builder",
JavaVisibility.PUBLIC,
builderType
);
commentGenerator.addGeneralMethodComment(builder, introspectedTable);
builder.setStatic(true);
builder.addBodyLine("return new " + builderType.getShortName() + "();");
topLevelClass.addMethod(builder);
commentGenerator.addClassComment(innerClass, introspectedTable);
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");
......
/*
* Copyright (c) 2017.
*
* 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/8/2 16:55
* ---------------------------------------------------------------------------
*/
public class InnerTypeFullyQualifiedJavaType extends FullyQualifiedJavaType {
private final static Logger logger = LoggerFactory.getLogger(InnerTypeFullyQualifiedJavaType.class);
private String outerType; // 内部类
/**
* Use this constructor to construct a generic type with the specified type parameters.
* @param fullTypeSpecification the full type specification
*/
public InnerTypeFullyQualifiedJavaType(String fullTypeSpecification){
super(fullTypeSpecification);
try{
// 修正package
java.lang.reflect.Field packageName = this.getClass().getSuperclass().getDeclaredField("packageName");
packageName.setAccessible(true);
String oldPackageName = getPackageName();
packageName.set(this, oldPackageName.substring(0, oldPackageName.lastIndexOf(".")));
outerType = oldPackageName.substring(oldPackageName.lastIndexOf(".") + 1);
} catch (Exception e){
logger.error("InnerTypeFullyQualifiedJavaType 赋值失败!", e);
}
}
/**
* This method returns the fully qualified name - including any generic type parameters.
*
* @return Returns the fullyQualifiedName.
*/
@Override
public String getFullyQualifiedName() {
String fullyQualifiedName = super.getFullyQualifiedName();
String before = fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf("."));
String end = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf("."));
return before + "." + outerType + end;
}
/**
* Gets the short name.
*
* @return Returns the shortName - including any type arguments.
*/
@Override
public String getShortName() {
return outerType + "." + super.getShortName();
}
}
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