Search in sources :

Example 51 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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 52 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class AnnotatedUpdateByPrimaryKeyWithoutBLOBsMethodGenerator method addMapperAnnotations.

@Override
public void addMapperAnnotations(Method method) {
    //$NON-NLS-1$
    method.addAnnotation("@Update({");
    StringBuilder sb = new StringBuilder();
    javaIndent(sb, 1);
    //$NON-NLS-1$
    sb.append("\"update ");
    sb.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime()));
    //$NON-NLS-1$
    sb.append("\",");
    method.addAnnotation(sb.toString());
    // set up for first column
    sb.setLength(0);
    javaIndent(sb, 1);
    //$NON-NLS-1$
    sb.append("\"set ");
    Iterator<IntrospectedColumn> iter;
    if (isSimple) {
        iter = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns()).iterator();
    } else {
        iter = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getBaseColumns()).iterator();
    }
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(getParameterClause(introspectedColumn));
        if (iter.hasNext()) {
            sb.append(',');
        }
        //$NON-NLS-1$
        sb.append("\",");
        method.addAnnotation(sb.toString());
        // set up for the next column
        if (iter.hasNext()) {
            sb.setLength(0);
            javaIndent(sb, 1);
            //$NON-NLS-1$
            sb.append("  \"");
        }
    }
    boolean and = false;
    iter = introspectedTable.getPrimaryKeyColumns().iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        if (and) {
            //$NON-NLS-1$
            sb.append("  \"and ");
        } else {
            //$NON-NLS-1$
            sb.append("\"where ");
            and = true;
        }
        sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(getParameterClause(introspectedColumn));
        sb.append('\"');
        if (iter.hasNext()) {
            sb.append(',');
        }
        method.addAnnotation(sb.toString());
    }
    //$NON-NLS-1$
    method.addAnnotation("})");
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn)

Example 53 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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 54 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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)

Example 55 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class SimpleModelGenerator 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);
    List<IntrospectedColumn> constructorColumns = introspectedTable.getAllColumns();
    for (IntrospectedColumn introspectedColumn : constructorColumns) {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty()));
    }
    StringBuilder sb = new StringBuilder();
    if (introspectedTable.getRules().generatePrimaryKeyClass()) {
        boolean comma = false;
        //$NON-NLS-1$
        sb.append("super(");
        for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
            if (comma) {
                //$NON-NLS-1$
                sb.append(", ");
            } else {
                comma = true;
            }
            sb.append(introspectedColumn.getJavaProperty());
        }
        //$NON-NLS-1$
        sb.append(");");
        method.addBodyLine(sb.toString());
    }
    List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        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

IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)82 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)34 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)32 Attribute (org.mybatis.generator.api.dom.xml.Attribute)30 Method (org.mybatis.generator.api.dom.java.Method)29 TextElement (org.mybatis.generator.api.dom.xml.TextElement)27 ArrayList (java.util.ArrayList)17 Parameter (org.mybatis.generator.api.dom.java.Parameter)17 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)12 TreeSet (java.util.TreeSet)9 Field (org.mybatis.generator.api.dom.java.Field)9 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)8 CommentGenerator (org.mybatis.generator.api.CommentGenerator)7 Plugin (org.mybatis.generator.api.Plugin)7 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)7 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 GeneratedKey (org.mybatis.generator.config.GeneratedKey)6 HashMap (java.util.HashMap)5 List (java.util.List)5