Java Code Examples for org.mybatis.generator.api.dom.java.Method#getReturnType()

The following examples show how to use org.mybatis.generator.api.dom.java.Method#getReturnType() . 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: SelectByPrimaryKeyMethodGenerator.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);

    // 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 2
Source File: InsertMethodGenerator.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
            .getInsertStatementId(), "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().clientInsertMethodGenerated(method,
            topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 3
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 4
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 5
Source File: InsertMethodGenerator.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);

    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
            .getInsertStatementId(), "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().clientInsertMethodGenerated(method,
            topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 6
Source File: InsertSelectiveMethodGenerator.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);

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