Java Code Examples for org.mybatis.generator.internal.util.JavaBeansUtil#getJavaBeansField()

The following examples show how to use org.mybatis.generator.internal.util.JavaBeansUtil#getJavaBeansField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IncrementsPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {
    if (this.support()) {
        if (this.incEnum == null) {
            this.incEnum = this.generatedIncEnum(introspectedTable);
            this.incEnumBuilder = builderClass;
            // 增加枚举
            builderClass.addInnerEnum(this.incEnum);
            // topLevel class 添加必要的操作方法
            this.addIncMethodToTopLevelClass(topLevelClass, builderClass, introspectedTable, false);
        }


        // Builder 中 添加字段支持
        for (IntrospectedColumn column : columns) {
            if (this.supportIncrement(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(this.getIncEnum(builderClass, introspectedTable), "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;
}
 
Example 2
Source File: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 构建Increment Enum
 * @param topLevelClass
 * @param introspectedTable
 * @return
 */
private InnerEnum generateIncrementEnum(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
    InnerEnum innerEnum = new InnerEnum(new FullyQualifiedJavaType(ENUM_INCREMENT));
    innerEnum.setVisibility(JavaVisibility.PUBLIC);
    innerEnum.setStatic(true);
    commentGenerator.addEnumComment(innerEnum, introspectedTable);

    // 生成属性和构造函数
    Field columnField = new Field("column", new FullyQualifiedJavaType(ModelColumnPlugin.ENUM_NAME));
    columnField.setVisibility(JavaVisibility.PRIVATE);
    columnField.setFinal(true);
    innerEnum.addField(columnField);

    Method constructor = new Method(ENUM_INCREMENT);
    constructor.setConstructor(true);
    constructor.addBodyLine("this.column = column;");
    constructor.addParameter(new Parameter(new FullyQualifiedJavaType(ModelColumnPlugin.ENUM_NAME), "column"));
    FormatTools.addMethodWithBestPosition(innerEnum, constructor);

    // 属性的Getter方法
    innerEnum.addMethod(JavaElementGeneratorTools.generateGetterMethod(columnField));

    // 添加增减方法
    Method mInc = JavaElementGeneratorTools.generateMethod(
            METHOD_INC,
            JavaVisibility.PUBLIC,
            new FullyQualifiedJavaType(ENUM_INCREMENT + "." + CLASS_INCREMENT_ITEM),
            new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value")
    );
    mInc.addBodyLine("return new " + ENUM_INCREMENT + "." + CLASS_INCREMENT_ITEM + "(this.column, \"+\", value);");
    innerEnum.addMethod(mInc);

    Method mDec = JavaElementGeneratorTools.generateMethod(
            METHOD_DEC,
            JavaVisibility.PUBLIC,
            new FullyQualifiedJavaType(ENUM_INCREMENT + "." + CLASS_INCREMENT_ITEM),
            new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value")
    );
    mDec.addBodyLine("return new " + ENUM_INCREMENT + "." + CLASS_INCREMENT_ITEM + "(this.column, \"-\", value);");
    innerEnum.addMethod(mDec);

    // Enum枚举
    for (IntrospectedColumn introspectedColumn : this.incColumns) {
        Field field = JavaBeansUtil.getJavaBeansField(introspectedColumn, context, introspectedTable);

        StringBuffer sb = new StringBuffer();
        sb.append(field.getName());
        sb.append("(");
        sb.append(ModelColumnPlugin.ENUM_NAME);
        sb.append(".");
        sb.append(field.getName());
        sb.append(")");

        innerEnum.addEnumConstant(sb.toString());
    }


    // 添加内部类IncrementItem
    innerEnum.addInnerClass(this.generateIncrementItemClass(introspectedTable));

    return innerEnum;
}
 
Example 3
Source File: ModelBuilderPlugin.java    From mybatis-generator-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 生成ModelBuilder
 * @param topLevelClass
 * @param introspectedTable
 * @param columns
 * @return
 */
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);
    if (topLevelClass.getSuperClass() != null) {
        innerClass.setSuperClass(topLevelClass.getSuperClass().getShortName() + "." + BUILDER_CLASS_NAME);
    }

    // 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
    // 顺序为 key base withBLOBs
    InnerTypeFullyQualifiedJavaType builderType = new InnerTypeFullyQualifiedJavaType(topLevelClass.getType().getFullyQualifiedName() + "." + BUILDER_CLASS_NAME);

    // 增加静态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;
}