Search in sources :

Example 96 with Method

use of org.mybatis.generator.api.dom.java.Method 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 97 with Method

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

the class ProviderApplyWhereMethodGenerator method addClassElements.

@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.WHERE");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }
    importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
    "java.util.List"));
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(fqjt);
    importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
    String.format("%s.Criteria", fqjt.getFullyQualifiedName())));
    importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
    String.format("%s.Criterion", fqjt.getFullyQualifiedName())));
    //$NON-NLS-1$
    Method method = new Method("applyWhere");
    method.setVisibility(JavaVisibility.PROTECTED);
    if (!useLegacyBuilder) {
        //$NON-NLS-1$
        method.addParameter(new Parameter(NEW_BUILDER_IMPORT, "sql"));
    }
    //$NON-NLS-1$
    method.addParameter(new Parameter(fqjt, "example"));
    //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "includeExamplePhrase"));
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    for (String methodLine : BEGINNING_METHOD_LINES) {
        method.addBodyLine(methodLine);
    }
    if (useLegacyBuilder) {
        for (String methodLine : LEGACY_ENDING_METHOD_LINES) {
            method.addBodyLine(methodLine);
        }
    } else {
        for (String methodLine : ENDING_METHOD_LINES) {
            method.addBodyLine(methodLine);
        }
    }
    if (context.getPlugins().providerApplyWhereMethodGenerated(method, topLevelClass, introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
Also used : TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 98 with Method

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

the class ProviderDeleteByExampleMethodGenerator method addClassElements.

@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(fqjt);
    Method method = new Method(introspectedTable.getDeleteByExampleStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    //$NON-NLS-1$
    method.addParameter(new Parameter(fqjt, "example"));
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("BEGIN();");
        method.addBodyLine(//$NON-NLS-1$
        String.format(//$NON-NLS-1$
        "DELETE_FROM(\"%s\");", escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
        //$NON-NLS-1$
        method.addBodyLine("applyWhere(example, false);");
        //$NON-NLS-1$
        method.addBodyLine("return SQL();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("SQL sql = new SQL();");
        method.addBodyLine(//$NON-NLS-1$
        String.format(//$NON-NLS-1$
        "sql.DELETE_FROM(\"%s\");", escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
        //$NON-NLS-1$
        method.addBodyLine("applyWhere(sql, example, false);");
        //$NON-NLS-1$
        method.addBodyLine("return sql.toString();");
    }
    if (context.getPlugins().providerDeleteByExampleMethodGenerated(method, topLevelClass, introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
Also used : TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 99 with Method

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

the class ProviderInsertSelectiveMethodGenerator method addClassElements.

@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.VALUES");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }
    FullyQualifiedJavaType fqjt = introspectedTable.getRules().calculateAllFieldsClass();
    importedTypes.add(fqjt);
    Method method = new Method(introspectedTable.getInsertSelectiveStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    //$NON-NLS-1$
    method.addParameter(new Parameter(fqjt, "record"));
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("BEGIN();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("SQL sql = new SQL();");
    }
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%sINSERT_INTO(\"%s\");", builderPrefix, escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())));
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
        //$NON-NLS-1$
        method.addBodyLine("");
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive() && !introspectedColumn.isSequenceColumn()) {
            method.addBodyLine(//$NON-NLS-1$
            String.format(//$NON-NLS-1$
            "if (record.%s() != null) {", getGetterMethodName(introspectedColumn.getJavaProperty(), introspectedColumn.getFullyQualifiedJavaType())));
        }
        method.addBodyLine(//$NON-NLS-1$
        String.format(//$NON-NLS-1$
        "%sVALUES(\"%s\", \"%s\");", builderPrefix, escapeStringForJava(getEscapedColumnName(introspectedColumn)), getParameterClause(introspectedColumn)));
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive() && !introspectedColumn.isSequenceColumn()) {
            //$NON-NLS-1$
            method.addBodyLine("}");
        }
    }
    //$NON-NLS-1$
    method.addBodyLine("");
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("return SQL();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("return sql.toString();");
    }
    if (context.getPlugins().providerInsertSelectiveMethodGenerated(method, topLevelClass, introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 100 with Method

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

the class RecordWithBLOBsGenerator method addParameterizedConstructor.

private void addParameterizedConstructor(TopLevelClass topLevelClass) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName(topLevelClass.getType().getShortName());
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty()));
        topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
    }
    boolean comma = false;
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("super(");
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        if (comma) {
            //$NON-NLS-1$
            sb.append(", ");
        } else {
            comma = true;
        }
        sb.append(introspectedColumn.getJavaProperty());
    }
    //$NON-NLS-1$
    sb.append(");");
    method.addBodyLine(sb.toString());
    for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) {
        sb.setLength(0);
        //$NON-NLS-1$
        sb.append("this.");
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(introspectedColumn.getJavaProperty());
        sb.append(';');
        method.addBodyLine(sb.toString());
    }
    topLevelClass.addMethod(method);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Aggregations

Method (org.mybatis.generator.api.dom.java.Method)121 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)97 Parameter (org.mybatis.generator.api.dom.java.Parameter)66 TreeSet (java.util.TreeSet)51 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)29 Field (org.mybatis.generator.api.dom.java.Field)20 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)17 ArrayList (java.util.ArrayList)14 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 Plugin (org.mybatis.generator.api.Plugin)7 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 Interface (org.mybatis.generator.api.dom.java.Interface)2 PrimitiveTypeWrapper (org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper)2 DAOMethodNameCalculator (org.mybatis.generator.api.DAOMethodNameCalculator)1 Rules (org.mybatis.generator.internal.rules.Rules)1