src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
private InnerClass generateModelBuilder(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, List<IntrospectedColumn> columns) {
        // 生成内部Builder类
        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() + "();");
        FormatTools.addMethodWithBestPosition(topLevelClass, builder);

        commentGenerator.addClassComment(innerClass, introspectedTable);
        logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");

        // 构建内部obj变量
        Field f = JavaElementGeneratorTools.generateField("obj", JavaVisibility.PRIVATE, topLevelClass.getType(), null);
        commentGenerator.addFieldComment(f, introspectedTable);
        innerClass.addField(f);

        // 构造构造方法
        Method constructor = new Method(BUILDER_CLASS_NAME);
        constructor.setVisibility(JavaVisibility.PUBLIC);
        constructor.setConstructor(true);
        constructor.addBodyLine(new StringBuilder("this.obj = new ").append(topLevelClass.getType().getShortName()).append("();").toString());
        commentGenerator.addGeneralMethodComment(constructor, introspectedTable);
        FormatTools.addMethodWithBestPosition(innerClass, constructor);
        logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加的构造方法。");

        for (IntrospectedColumn introspectedColumn : columns) {
            Field field = JavaBeansUtil.getJavaBeansField(introspectedColumn, context, introspectedTable);
            Method setterMethod = JavaBeansUtil.getJavaBeansSetter(introspectedColumn, context, introspectedTable);

            Method method = JavaElementGeneratorTools.generateMethod(
                    field.getName(),
                    JavaVisibility.PUBLIC,
                    innerClass.getType(),
                    new Parameter(field.getType(), field.getName())
            );
            commentGenerator.addSetterComment(method, introspectedTable, introspectedColumn);
            method = JavaElementGeneratorTools.generateMethodBody(
                    method,
                    "obj." + setterMethod.getName() + "(" + field.getName() + ");",
                    "return this;"
            );

            // hook
            if (PluginTools.getHook(IModelBuilderPluginHook.class).modelBuilderSetterMethodGenerated(method, topLevelClass, innerClass, introspectedColumn, introspectedTable)) {
                FormatTools.addMethodWithBestPosition(innerClass, method);
                logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加" + method.getName() + "方法(复合主键)。");
            }
        }

        Method build = JavaElementGeneratorTools.generateMethod(
                "build",
                JavaVisibility.PUBLIC,
                topLevelClass.getType()
        );
        build.addBodyLine("return this.obj;");
        commentGenerator.addGeneralMethodComment(build, introspectedTable);
        FormatTools.addMethodWithBestPosition(innerClass, build);
        logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。");

        // hook
        PluginTools.getHook(IModelBuilderPluginHook.class).modelBuilderClassGenerated(topLevelClass, innerClass, columns, introspectedTable);

        return innerClass;
    }
private InnerClass generateModelBuilder(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, List<IntrospectedColumn> columns) {
        // 生成内部Builder类
        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() + "();");
        FormatTools.addMethodWithBestPosition(topLevelClass, builder);

        commentGenerator.addClassComment(innerClass, introspectedTable);
        logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");

        // 构建内部obj变量
        Field f = JavaElementGeneratorTools.generateField("obj", JavaVisibility.PRIVATE, topLevelClass.getType(), null);
        commentGenerator.addFieldComment(f, introspectedTable);
        innerClass.addField(f);

        // 构造构造方法
        Method constructor = new Method(BUILDER_CLASS_NAME);
        constructor.setVisibility(JavaVisibility.PUBLIC);
        constructor.setConstructor(true);
        constructor.addBodyLine(new StringBuilder("this.obj = new ").append(topLevelClass.getType().getShortName()).append("();").toString());
        commentGenerator.addGeneralMethodComment(constructor, introspectedTable);
        FormatTools.addMethodWithBestPosition(innerClass, constructor);
        logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加的构造方法。");

        for (IntrospectedColumn introspectedColumn : columns) {
            Field field = JavaBeansUtil.getJavaBeansField(introspectedColumn, context, introspectedTable);
            Method setterMethod = JavaBeansUtil.getJavaBeansSetter(introspectedColumn, context, introspectedTable);

            Method method = JavaElementGeneratorTools.generateMethod(
                    field.getName(),
                    JavaVisibility.PUBLIC,
                    innerClass.getType(),
                    new Parameter(field.getType(), field.getName())
            );
            commentGenerator.addSetterComment(method, introspectedTable, introspectedColumn);
            method = JavaElementGeneratorTools.generateMethodBody(
                    method,
                    "obj." + setterMethod.getName() + "(" + field.getName() + ");",
                    "return this;"
            );

            // hook
            if (PluginTools.getHook(IModelBuilderPluginHook.class).modelBuilderSetterMethodGenerated(method, topLevelClass, innerClass, introspectedColumn, introspectedTable)) {
                FormatTools.addMethodWithBestPosition(innerClass, method);
                logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加" + method.getName() + "方法(复合主键)。");
            }
        }

        Method build = JavaElementGeneratorTools.generateMethod(
                "build",
                JavaVisibility.PUBLIC,
                topLevelClass.getType()
        );
        build.addBodyLine("return this.obj;");
        commentGenerator.addGeneralMethodComment(build, introspectedTable);
        FormatTools.addMethodWithBestPosition(innerClass, build);
        logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。");

        // hook
        PluginTools.getHook(IModelBuilderPluginHook.class).modelBuilderClassGenerated(topLevelClass, innerClass, columns, introspectedTable);

        return innerClass;
    }
private InnerClass generateModelBuilder ( TopLevelClass topLevelClass , IntrospectedTable introspectedTable , List < IntrospectedColumn > columns ) { // 生成内部Builder类 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 ( ) + "();" ) ; FormatTools . addMethodWithBestPosition ( topLevelClass , builder ) ; commentGenerator . addClassComment ( innerClass , introspectedTable ) ; logger . debug ( "itfsw(数据Model链式构建插件):" + topLevelClass g tType().getSh o rtName( ) +  "增加内部Builde r 类 " ; // 构建内部obj变量 Field f = JavaElementGeneratorTools . generateField ( "obj" , JavaVisibility . PRIVATE , topLevelClass . getType ( ) , null ) ; commentGenerator . addFieldComment ( f , introspectedTable ) ; innerClass . addField ( f ) ; // 构造构造方法 Method constructor = new Method ( BUILDER_CLASS_NAME ) ; constructor . setVisibility ( JavaVisibility . PUBLIC ) ; constructor . setConstructor ( true ) ; constructor . addBodyLine ( new StringBuilder ( "this.obj = new " ) . append ( topLevelClass . getType ( ) . getShortName ( ) ) . append ( "();" ) . toString ( ) ) ; commentGenerator . addGeneralMethodComment ( constructor , introspectedTable ) ; FormatTools . addMethodWithBestPosition ( innerClass , constructor ) ; logger . debug ( "itfsw(数据Model链式构建插件):" + topLevelClass g tType().getSh o rtName( ) +  ".Builder增加 的 构 方 。"); for ( IntrospectedColumn introspectedColumn : columns ) { Field field = JavaBeansUtil . getJavaBeansField ( introspectedColumn , context , introspectedTable ) ; Method setterMethod = JavaBeansUtil . getJavaBeansSetter ( introspectedColumn , context , introspectedTable ) ; Method method = JavaElementGeneratorTools . generateMethod ( field . getName ( ) , JavaVisibility . PUBLIC , innerClass . getType ( ) , new Parameter ( field . getType ( ) , field . getName ( ) ) ) ; commentGenerator . addSetterComment ( method , introspectedTable , introspectedColumn ) ; method = JavaElementGeneratorTools . generateMethodBody ( method , "obj." + setterMethod . getName ( ) + "(" + field . getName ( ) + ");" , "return this;" ) ; // hook if ( PluginTools . getHook ( IModelBuilderPluginHook . class ) . modelBuilderSetterMethodGenerated ( method , topLevelClass , innerClass , introspectedColumn , introspectedTable ) ) { FormatTools . addMethodWithBestPosition ( innerClass , method ) ; logger . debug ( "itfsw(数据Model链式构建插件):" + topLevelClass g tType().getSh o rtName( ) +  ".Builder增加 " ethod.getName()  方法(复合主 键 )。"); } } Method build = JavaElementGeneratorTools . generateMethod ( "build" , JavaVisibility . PUBLIC , topLevelClass . getType ( ) ) ; build . addBodyLine ( "return this.obj;" ) ; commentGenerator . addGeneralMethodComment ( build , introspectedTable ) ; FormatTools . addMethodWithBestPosition ( innerClass , build ) ; logger . debug ( "itfsw(数据Model链式构建插件):" + topLevelClass g tType().getSh o rtName( ) +  ".Builder增加 b u l 方法。"); // hook PluginTools . getHook ( IModelBuilderPluginHook . class ) . modelBuilderClassGenerated ( topLevelClass , innerClass , columns , introspectedTable ) ; return innerClass ; }
生成ModelBuilder
@param topLevelClass
@param introspectedTable
@param columns
@return
生成ModelBuilder