Search in sources :

Example 1 with PrimitiveTypeWrapper

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

the class InsertSelectiveMethodGenerator method addImplementationElements.

@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) {
        //$NON-NLS-1$
        sb.append("Object newKey = ");
    }
    sb.append(daoTemplate.getInsertMethod(introspectedTable.getIbatis2SqlMapNamespace(), introspectedTable.getInsertSelectiveStatementId(), //$NON-NLS-1$
    "record"));
    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
            //$NON-NLS-1$
            method.addBodyLine("return newKey;");
        } else {
            sb.setLength(0);
            if (returnType.isPrimitive()) {
                PrimitiveTypeWrapper ptw = returnType.getPrimitiveTypeWrapper();
                //$NON-NLS-1$
                sb.append("return ((");
                sb.append(ptw.getShortName());
                //$NON-NLS-1$
                sb.append(") newKey");
                //$NON-NLS-1$
                sb.append(").");
                sb.append(ptw.getToPrimitiveMethod());
                sb.append(';');
            } else {
                //$NON-NLS-1$
                sb.append("return (");
                sb.append(returnType.getShortName());
                //$NON-NLS-1$
                sb.append(") newKey;");
            }
            method.addBodyLine(sb.toString());
        }
    }
    if (context.getPlugins().clientInsertSelectiveMethodGenerated(method, topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
Also used : TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Method(org.mybatis.generator.api.dom.java.Method) PrimitiveTypeWrapper(org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper)

Example 2 with PrimitiveTypeWrapper

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

the class InsertMethodGenerator method addImplementationElements.

@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) {
        //$NON-NLS-1$
        sb.append("Object newKey = ");
    }
    sb.append(daoTemplate.getInsertMethod(introspectedTable.getIbatis2SqlMapNamespace(), introspectedTable.getInsertStatementId(), //$NON-NLS-1$
    "record"));
    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
            //$NON-NLS-1$
            method.addBodyLine("return newKey;");
        } else {
            sb.setLength(0);
            if (returnType.isPrimitive()) {
                PrimitiveTypeWrapper ptw = returnType.getPrimitiveTypeWrapper();
                //$NON-NLS-1$
                sb.append("return ((");
                sb.append(ptw.getShortName());
                //$NON-NLS-1$
                sb.append(") newKey");
                //$NON-NLS-1$
                sb.append(").");
                sb.append(ptw.getToPrimitiveMethod());
                sb.append(';');
            } else {
                //$NON-NLS-1$
                sb.append("return (");
                sb.append(returnType.getShortName());
                //$NON-NLS-1$
                sb.append(") newKey;");
            }
            method.addBodyLine(sb.toString());
        }
    }
    if (context.getPlugins().clientInsertMethodGenerated(method, topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
Also used : TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Method(org.mybatis.generator.api.dom.java.Method) PrimitiveTypeWrapper(org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper)

Aggregations

TreeSet (java.util.TreeSet)2 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)2 Method (org.mybatis.generator.api.dom.java.Method)2 PrimitiveTypeWrapper (org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper)2