Java Code Examples for org.mybatis.generator.api.dom.java.TopLevelClass#addMethod()

The following examples show how to use org.mybatis.generator.api.dom.java.TopLevelClass#addMethod() . 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: UpdateByExampleSelectiveMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    method
            .addBodyLine("UpdateByExampleParms parms = new UpdateByExampleParms(record, example);"); //$NON-NLS-1$

    StringBuilder sb = new StringBuilder();

    sb.append("int rows = "); //$NON-NLS-1$

    sb.append(daoTemplate.getUpdateMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getUpdateByExampleSelectiveStatementId(), "parms")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    method.addBodyLine("return rows;"); //$NON-NLS-1$

    if (context.getPlugins()
            .clientUpdateByExampleSelectiveMethodGenerated(method,
                    topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 2
Source File: CountByExampleMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    // generate the implementation method
    StringBuilder sb = new StringBuilder();

    sb.append("Integer count = (Integer)  "); //$NON-NLS-1$
    sb.append(daoTemplate.getQueryForObjectMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getCountByExampleStatementId(), "example")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    if (generateForJava5) {
        method.addBodyLine("return count;"); //$NON-NLS-1$
    } else {
        method.addBodyLine("return count.intValue();"); //$NON-NLS-1$
    }

    if (context.getPlugins().clientCountByExampleMethodGenerated(method,
            topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 3
Source File: SelectByExampleWithoutBLOBsMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    if (generateForJava5) {
        method.addSuppressTypeWarningsAnnotation();
    }

    StringBuilder sb = new StringBuilder();
    sb.append(method.getReturnType().getShortName());
    sb.append(" list = "); //$NON-NLS-1$
    sb.append(daoTemplate.getQueryForListMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getSelectByExampleStatementId(), "example")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return list;"); //$NON-NLS-1$

    if (context.getPlugins()
            .clientSelectByExampleWithoutBLOBsMethodGenerated(method,
                    topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 4
Source File: UpdateByPrimaryKeyWithoutBLOBsMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    StringBuilder sb = new StringBuilder();
    sb.append("int rows = "); //$NON-NLS-1$
    sb.append(daoTemplate.getUpdateMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getUpdateByPrimaryKeyStatementId(), "record")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    method.addBodyLine("return rows;"); //$NON-NLS-1$

    if (context.getPlugins()
            .clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(method,
                    topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 5
Source File: CountByExampleMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    // generate the implementation method
    StringBuilder sb = new StringBuilder();

    sb.append("Integer count = (Integer)  "); //$NON-NLS-1$
    sb.append(daoTemplate.getQueryForObjectMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getCountByExampleStatementId(), "example")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    if (generateForJava5) {
        method.addBodyLine("return count;"); //$NON-NLS-1$
    } else {
        method.addBodyLine("return count.intValue();"); //$NON-NLS-1$
    }

    if (context.getPlugins().clientCountByExampleMethodGenerated(method,
            topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 6
Source File: PrimaryKeyGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private void addParameterizedConstructor(TopLevelClass topLevelClass) {
    Method method = new Method(topLevelClass.getType().getShortName());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

    StringBuilder sb = new StringBuilder();
    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getPrimaryKeyColumns()) {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(),
                introspectedColumn.getJavaProperty()));
        sb.setLength(0);
        sb.append("this.");
        sb.append(introspectedColumn.getJavaProperty());
        sb.append(" = ");
        sb.append(introspectedColumn.getJavaProperty());
        sb.append(';');
        method.addBodyLine(sb.toString());
    }

    topLevelClass.addMethod(method);
}
 
Example 7
Source File: DeleteByExampleMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    StringBuilder sb = new StringBuilder();
    sb.append("int rows = "); //$NON-NLS-1$
    sb.append(daoTemplate.getDeleteMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getDeleteByExampleStatementId(), "example")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return rows;"); //$NON-NLS-1$

    if (context.getPlugins().clientDeleteByExampleMethodGenerated(
            method, topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 8
Source File: UpdateByExampleWithoutBLOBsMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    method
            .addBodyLine("UpdateByExampleParms parms = new UpdateByExampleParms(record, example);"); //$NON-NLS-1$

    StringBuilder sb = new StringBuilder();
    sb.append("int rows = "); //$NON-NLS-1$
    sb.append(daoTemplate.getUpdateMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getUpdateByExampleStatementId(), "parms")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    method.addBodyLine("return rows;"); //$NON-NLS-1$

    if (context.getPlugins()
            .clientUpdateByExampleWithoutBLOBsMethodGenerated(method,
                    topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 9
Source File: UpdateByPrimaryKeySelectiveMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    StringBuilder sb = new StringBuilder();
    sb.append("int rows = "); //$NON-NLS-1$
    sb.append(daoTemplate.getUpdateMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getUpdateByPrimaryKeySelectiveStatementId(), "record")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    method.addBodyLine("return rows;"); //$NON-NLS-1$

    if (context.getPlugins()
            .clientUpdateByPrimaryKeySelectiveMethodGenerated(method,
                    topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 10
Source File: AbstractJavaGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
protected void addDefaultConstructor(TopLevelClass topLevelClass) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName(topLevelClass.getType().getShortName());
    method.addBodyLine("super();"); //$NON-NLS-1$
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
}
 
Example 11
Source File: DAOGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
protected TopLevelClass getTopLevelClassShell() {
    FullyQualifiedJavaType interfaceType = new FullyQualifiedJavaType(
            introspectedTable.getDAOInterfaceType());
    FullyQualifiedJavaType implementationType = new FullyQualifiedJavaType(
            introspectedTable.getDAOImplementationType());

    CommentGenerator commentGenerator = context.getCommentGenerator();

    TopLevelClass answer = new TopLevelClass(implementationType);
    answer.setVisibility(JavaVisibility.PUBLIC);
    answer.setSuperClass(daoTemplate.getSuperClass());
    answer.addImportedType(daoTemplate.getSuperClass());
    answer.addSuperInterface(interfaceType);
    answer.addImportedType(interfaceType);

    for (FullyQualifiedJavaType fqjt : daoTemplate
            .getImplementationImports()) {
        answer.addImportedType(fqjt);
    }

    commentGenerator.addJavaFileComment(answer);

    // add constructor from the template
    answer.addMethod(daoTemplate.getConstructorClone(commentGenerator,
            implementationType, introspectedTable));

    // add any fields from the template
    for (Field field : daoTemplate.getFieldClones(commentGenerator,
            introspectedTable)) {
        answer.addField(field);
    }

    // add any methods from the template
    for (Method method : daoTemplate.getMethodClones(commentGenerator,
            introspectedTable)) {
        answer.addMethod(method);
    }

    return answer;
}
 
Example 12
Source File: ProviderDeleteByExampleMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();

    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"); //$NON-NLS-1$
    
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(fqjt);

    Method method = new Method(
            introspectedTable.getDeleteByExampleStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    method.addBodyLine("BEGIN();"); //$NON-NLS-1$
    method.addBodyLine(String.format("DELETE_FROM(\"%s\");", //$NON-NLS-1$
            escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    method.addBodyLine("applyWhere(example, false);"); //$NON-NLS-1$
    method.addBodyLine("return SQL();"); //$NON-NLS-1$
    
    
    if (context.getPlugins().providerDeleteByExampleMethodGenerated(method, topLevelClass,
            introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 13
Source File: BaseRecordGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString(
            "Progress.8", table.toString())); //$NON-NLS-1$
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();

    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable
            .getBaseRecordType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);

    FullyQualifiedJavaType superClass = getSuperClass();
    if (superClass != null) {
        topLevelClass.setSuperClass(superClass);
        topLevelClass.addImportedType(superClass);
    }

    List<IntrospectedColumn> introspectedColumns;
    if (includePrimaryKeyColumns()) {
        if (includeBLOBColumns()) {
            introspectedColumns = introspectedTable.getAllColumns();
        } else {
            introspectedColumns = introspectedTable.getNonBLOBColumns();
        }
    } else {
        if (includeBLOBColumns()) {
            introspectedColumns = introspectedTable
                    .getNonPrimaryKeyColumns();
        } else {
            introspectedColumns = introspectedTable.getBaseColumns();
        }
    }

    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);
        }

        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;
}
 
Example 14
Source File: DynamicSqlModelGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.8", 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 = introspectedTable.getAllColumns();

    if (introspectedTable.isConstructorBased()) {
        addParameterizedConstructor(topLevelClass);

        if (!introspectedTable.isImmutable()) {
            addDefaultConstructorWithGeneratedAnnotatoin(topLevelClass);
        }
    }

    String rootClass = getRootClass();
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }

        Field field = getJavaBeansFieldWithGeneratedAnnotation(introspectedColumn, context, introspectedTable, topLevelClass);
        
        if (plugins.modelFieldGenerated(field, topLevelClass,
                introspectedColumn, introspectedTable,
                Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }

        Method method = getJavaBeansGetterWithGeneratedAnnotation(introspectedColumn, context, introspectedTable,
                topLevelClass);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass,
                introspectedColumn, introspectedTable,
                Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addMethod(method);
        }

        if (!introspectedTable.isImmutable()) {
            method = getJavaBeansSetterWithGeneratedAnnotation(introspectedColumn, context, introspectedTable,
                    topLevelClass);
            if (plugins.modelSetterMethodGenerated(method, topLevelClass,
                    introspectedColumn, introspectedTable,
                    Plugin.ModelClassType.BASE_RECORD)) {
                topLevelClass.addMethod(method);
            }
        }
    }

    List<CompilationUnit> answer = new ArrayList<>();
    if (context.getPlugins().modelBaseRecordClassGenerated(topLevelClass,
            introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
 
Example 15
Source File: SimpleModelGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.8", table.toString())); //$NON-NLS-1$
    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);
    }

    List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();

    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);
        if (plugins.modelFieldGenerated(field, topLevelClass,
                introspectedColumn, introspectedTable,
                Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }

        Method method = getJavaBeansGetter(introspectedColumn);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass,
                introspectedColumn, introspectedTable,
                Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addMethod(method);
        }

        if (!introspectedTable.isImmutable()) {
            method = getJavaBeansSetter(introspectedColumn);
            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;
}
 
Example 16
Source File: SelectByPrimaryKeyMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    // generate the implementation method
    StringBuilder sb = new StringBuilder();

    if (!introspectedTable.getRules().generatePrimaryKeyClass()) {
        // no primary key class, but primary key is enabled. Primary
        // key columns must be in the base class.
        FullyQualifiedJavaType keyType = new FullyQualifiedJavaType(
                introspectedTable.getBaseRecordType());
        topLevelClass.addImportedType(keyType);

        sb.setLength(0);
        sb.append(keyType.getShortName());
        sb.append(" _key = new "); //$NON-NLS-1$
        sb.append(keyType.getShortName());
        sb.append("();"); //$NON-NLS-1$
        method.addBodyLine(sb.toString());

        for (IntrospectedColumn introspectedColumn : introspectedTable
                .getPrimaryKeyColumns()) {
            sb.setLength(0);
            sb.append("_key."); //$NON-NLS-1$
            sb.append(getSetterMethodName(introspectedColumn
                    .getJavaProperty()));
            sb.append('(');
            sb.append(introspectedColumn.getJavaProperty());
            sb.append(");"); //$NON-NLS-1$
            method.addBodyLine(sb.toString());
        }
    }

    FullyQualifiedJavaType returnType = method.getReturnType();

    sb.setLength(0);
    sb.append(returnType.getShortName());
    sb.append(" record = ("); //$NON-NLS-1$
    sb.append(returnType.getShortName());
    sb.append(") "); //$NON-NLS-1$
    sb.append(daoTemplate.getQueryForObjectMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getSelectByPrimaryKeyStatementId(), "_key")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return record;"); //$NON-NLS-1$

    if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated(
            method, topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 17
Source File: PrimaryKeyGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString(
            "Progress.7", table.toString())); //$NON-NLS-1$
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();

    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable
            .getPrimaryKeyType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);

    String rootClass = getRootClass();
    if (rootClass != null) {
        topLevelClass.setSuperClass(new FullyQualifiedJavaType(rootClass));
        topLevelClass.addImportedType(topLevelClass.getSuperClass());
    }

    if (introspectedTable.isConstructorBased()) {
        addParameterizedConstructor(topLevelClass);
        
        if (!introspectedTable.isImmutable()) {
            addDefaultConstructor(topLevelClass);
        }
    }

    commentGenerator.addModelClassComment(topLevelClass, introspectedTable);

    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getPrimaryKeyColumns()) {
        if (RootClassInfo.getInstance(rootClass, warnings)
                .containsProperty(introspectedColumn)) {
            continue;
        }

        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass,
                introspectedColumn, introspectedTable,
                Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }

        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass,
                introspectedColumn, introspectedTable,
                Plugin.ModelClassType.PRIMARY_KEY)) {
            topLevelClass.addMethod(method);
        }

        if (!introspectedTable.isImmutable()) {
            method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
            if (plugins.modelSetterMethodGenerated(method, topLevelClass,
                    introspectedColumn, introspectedTable,
                    Plugin.ModelClassType.PRIMARY_KEY)) {
                topLevelClass.addMethod(method);
            }
        }
    }

    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelPrimaryKeyClassGenerated(
            topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
 
Example 18
Source File: ProviderInsertSelectiveMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();

    if (useLegacyBuilder) {
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
        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());
    method.addParameter(new Parameter(fqjt, "record"));
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    if (useLegacyBuilder) {
        method.addBodyLine("BEGIN();");
    } else {
        method.addBodyLine("SQL sql = new SQL();");
    }

    method.addBodyLine(String.format("%sINSERT_INTO(\"%s\");",
            builderPrefix,
            escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())));

    for (IntrospectedColumn introspectedColumn :
            ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
        
        method.addBodyLine("");
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
                && !introspectedColumn.isSequenceColumn()) {
            method.addBodyLine(String.format("if (record.%s() != null) {",
                    getGetterMethodName(introspectedColumn.getJavaProperty(),
                            introspectedColumn.getFullyQualifiedJavaType())));
        }
        method.addBodyLine(String.format("%sVALUES(\"%s\", \"%s\");",
                builderPrefix,
                escapeStringForJava(getEscapedColumnName(introspectedColumn)),
                getParameterClause(introspectedColumn)));

        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
                && !introspectedColumn.isSequenceColumn()) {
            method.addBodyLine("}");
        }
    }

    method.addBodyLine("");
    if (useLegacyBuilder) {
        method.addBodyLine("return SQL();");
    } else {
        method.addBodyLine("return sql.toString();");
    }
    
    if (context.getPlugins().providerInsertSelectiveMethodGenerated(method, topLevelClass,
            introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 19
Source File: InsertSelectiveMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    FullyQualifiedJavaType returnType = method.getReturnType();
    StringBuilder sb = new StringBuilder();

    if (returnType != null) {
        sb.append("Object newKey = "); //$NON-NLS-1$
    }

    sb.append(daoTemplate.getInsertMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getInsertSelectiveStatementId(), "record")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    if (returnType != null) {
        if ("Object".equals(returnType.getShortName())) { //$NON-NLS-1$
            // no need to cast if the return type is Object
            method.addBodyLine("return newKey;"); //$NON-NLS-1$
        } else {
            sb.setLength(0);

            if (returnType.isPrimitive()) {
                PrimitiveTypeWrapper ptw = returnType
                        .getPrimitiveTypeWrapper();
                sb.append("return (("); //$NON-NLS-1$
                sb.append(ptw.getShortName());
                sb.append(") newKey"); //$NON-NLS-1$
                sb.append(")."); //$NON-NLS-1$
                sb.append(ptw.getToPrimitiveMethod());
                sb.append(';');
            } else {
                sb.append("return ("); //$NON-NLS-1$
                sb.append(returnType.getShortName());
                sb.append(") newKey;"); //$NON-NLS-1$
            }

            method.addBodyLine(sb.toString());
        }
    }

    if (context.getPlugins().clientInsertSelectiveMethodGenerated(
            method, topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 20
Source File: AbstractJavaGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
protected void addDefaultConstructor(TopLevelClass topLevelClass) {
    topLevelClass.addMethod(getDefaultConstructor(topLevelClass));
}