Search in sources :

Example 21 with Field

use of org.mybatis.generator.api.dom.java.Field in project generator by mybatis.

the class GenericCIDAOTemplate method configureFields.

@Override
protected void configureFields() {
    Field field = new Field();
    field.setVisibility(JavaVisibility.PRIVATE);
    field.setType(sqlMapClientType);
    //$NON-NLS-1$
    field.setName("sqlMapClient");
    addField(field);
}
Also used : Field(org.mybatis.generator.api.dom.java.Field)

Example 22 with Field

use of org.mybatis.generator.api.dom.java.Field in project generator by mybatis.

the class BaseRecordGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.8", //$NON-NLS-1$
    table.toString()));
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    TopLevelClass topLevelClass = new TopLevelClass(type);
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    FullyQualifiedJavaType superClass = getSuperClass();
    if (superClass != null) {
        topLevelClass.setSuperClass(superClass);
        topLevelClass.addImportedType(superClass);
    }
    commentGenerator.addModelClassComment(topLevelClass, introspectedTable);
    List<IntrospectedColumn> introspectedColumns = getColumnsInThisClass();
    if (introspectedTable.isConstructorBased()) {
        addParameterizedConstructor(topLevelClass);
        if (!introspectedTable.isImmutable()) {
            addDefaultConstructor(topLevelClass);
        }
    }
    String rootClass = getRootClass();
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }
        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }
        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addMethod(method);
        }
        if (!introspectedTable.isImmutable()) {
            method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
            if (plugins.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
                topLevelClass.addMethod(method);
            }
        }
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelBaseRecordClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method) Field(org.mybatis.generator.api.dom.java.Field) JavaBeansUtil.getJavaBeansField(org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) Plugin(org.mybatis.generator.api.Plugin)

Example 23 with Field

use of org.mybatis.generator.api.dom.java.Field in project generator by mybatis.

the class ExampleGenerator method getCriterionInnerClass.

private InnerClass getCriterionInnerClass() {
    Field field;
    Method method;
    InnerClass answer = new InnerClass(new FullyQualifiedJavaType(//$NON-NLS-1$
    "Criterion"));
    answer.setVisibility(JavaVisibility.PUBLIC);
    answer.setStatic(true);
    context.getCommentGenerator().addClassComment(answer, introspectedTable);
    field = new Field();
    //$NON-NLS-1$
    field.setName("condition");
    field.setType(FullyQualifiedJavaType.getStringInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    field = new Field();
    //$NON-NLS-1$
    field.setName("value");
    field.setType(FullyQualifiedJavaType.getObjectInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    field = new Field();
    //$NON-NLS-1$
    field.setName("secondValue");
    field.setType(FullyQualifiedJavaType.getObjectInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    field = new Field();
    //$NON-NLS-1$
    field.setName("noValue");
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    field = new Field();
    //$NON-NLS-1$
    field.setName("singleValue");
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    field = new Field();
    //$NON-NLS-1$
    field.setName("betweenValue");
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    field = new Field();
    //$NON-NLS-1$
    field.setName("listValue");
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    field = new Field();
    //$NON-NLS-1$
    field.setName("typeHandler");
    field.setType(FullyQualifiedJavaType.getStringInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    answer.addField(field);
    answer.addMethod(getGetter(field));
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("Criterion");
    method.setConstructor(true);
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    //$NON-NLS-1$
    method.addBodyLine("super();");
    //$NON-NLS-1$
    method.addBodyLine("this.condition = condition;");
    //$NON-NLS-1$
    method.addBodyLine("this.typeHandler = null;");
    //$NON-NLS-1$
    method.addBodyLine("this.noValue = true;");
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("Criterion");
    method.setConstructor(true);
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "typeHandler"));
    //$NON-NLS-1$
    method.addBodyLine("super();");
    //$NON-NLS-1$
    method.addBodyLine("this.condition = condition;");
    //$NON-NLS-1$
    method.addBodyLine("this.value = value;");
    //$NON-NLS-1$
    method.addBodyLine("this.typeHandler = typeHandler;");
    //$NON-NLS-1$
    method.addBodyLine("if (value instanceof List<?>) {");
    //$NON-NLS-1$
    method.addBodyLine("this.listValue = true;");
    //$NON-NLS-1$
    method.addBodyLine("} else {");
    //$NON-NLS-1$
    method.addBodyLine("this.singleValue = true;");
    //$NON-NLS-1$
    method.addBodyLine("}");
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("Criterion");
    method.setConstructor(true);
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    //$NON-NLS-1$
    method.addBodyLine("this(condition, value, null);");
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("Criterion");
    method.setConstructor(true);
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "secondValue"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "typeHandler"));
    //$NON-NLS-1$
    method.addBodyLine("super();");
    //$NON-NLS-1$
    method.addBodyLine("this.condition = condition;");
    //$NON-NLS-1$
    method.addBodyLine("this.value = value;");
    //$NON-NLS-1$
    method.addBodyLine("this.secondValue = secondValue;");
    //$NON-NLS-1$
    method.addBodyLine("this.typeHandler = typeHandler;");
    //$NON-NLS-1$
    method.addBodyLine("this.betweenValue = true;");
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("Criterion");
    method.setConstructor(true);
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "secondValue"));
    //$NON-NLS-1$
    method.addBodyLine("this(condition, value, secondValue, null);");
    answer.addMethod(method);
    return answer;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) InnerClass(org.mybatis.generator.api.dom.java.InnerClass) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 24 with Field

use of org.mybatis.generator.api.dom.java.Field in project generator by mybatis.

the class ToStringPlugin method generateToString.

private void generateToString(IntrospectedTable introspectedTable, TopLevelClass topLevelClass) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    //$NON-NLS-1$
    method.setName("toString");
    if (introspectedTable.isJava5Targeted()) {
        //$NON-NLS-1$
        method.addAnnotation("@Override");
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    //$NON-NLS-1$
    method.addBodyLine("StringBuilder sb = new StringBuilder();");
    //$NON-NLS-1$
    method.addBodyLine("sb.append(getClass().getSimpleName());");
    //$NON-NLS-1$
    method.addBodyLine("sb.append(\" [\");");
    //$NON-NLS-1$
    method.addBodyLine("sb.append(\"Hash = \").append(hashCode());");
    StringBuilder sb = new StringBuilder();
    for (Field field : topLevelClass.getFields()) {
        String property = field.getName();
        sb.setLength(0);
        //$NON-NLS-1$ //$NON-NLS-2$
        sb.append("sb.append(\"").append(", ").append(property).append("=\")").append(".append(").append(//$NON-NLS-1$ //$NON-NLS-2$
        property).append(//$NON-NLS-1$
        ");");
        method.addBodyLine(sb.toString());
    }
    //$NON-NLS-1$
    method.addBodyLine("sb.append(\"]\");");
    if (useToStringFromRoot && topLevelClass.getSuperClass() != null) {
        //$NON-NLS-1$
        method.addBodyLine("sb.append(\", from super class \");");
        //$NON-NLS-1$
        method.addBodyLine("sb.append(super.toString());");
    }
    //$NON-NLS-1$
    method.addBodyLine("return sb.toString();");
    topLevelClass.addMethod(method);
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) Method(org.mybatis.generator.api.dom.java.Method)

Example 25 with Field

use of org.mybatis.generator.api.dom.java.Field in project generator by mybatis.

the class JavaBeansUtil method getJavaBeansField.

/**
     * Gets the java beans field.
     *
     * @param introspectedColumn
     *            the introspected column
     * @param context
     *            the context
     * @param introspectedTable
     *            the introspected table
     * @return the java beans field
     */
public static Field getJavaBeansField(IntrospectedColumn introspectedColumn, Context context, IntrospectedTable introspectedTable) {
    FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
    String property = introspectedColumn.getJavaProperty();
    Field field = new Field();
    field.setVisibility(JavaVisibility.PRIVATE);
    field.setType(fqjt);
    field.setName(property);
    context.getCommentGenerator().addFieldComment(field, introspectedTable, introspectedColumn);
    return field;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Aggregations

Field (org.mybatis.generator.api.dom.java.Field)25 Method (org.mybatis.generator.api.dom.java.Method)20 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)19 ArrayList (java.util.ArrayList)14 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)13 Parameter (org.mybatis.generator.api.dom.java.Parameter)11 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)11 CommentGenerator (org.mybatis.generator.api.CommentGenerator)10 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)10 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)9 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)9 Plugin (org.mybatis.generator.api.Plugin)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)4 Rules (org.mybatis.generator.internal.rules.Rules)1