Search in sources :

Example 6 with Parameter

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

the class DeleteByPrimaryKeyMethodGenerator method addInterfaceElements.

@Override
public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setName(introspectedTable.getDeleteByPrimaryKeyStatementId());
    if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
        importedTypes.add(type);
        //$NON-NLS-1$
        method.addParameter(new Parameter(type, "key"));
    } else {
        // no primary key class - fields are in the base class
        // if more than one PK field, then we need to annotate the
        // parameters
        // for MyBatis
        List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
        boolean annotate = introspectedColumns.size() > 1;
        if (annotate) {
            importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
            "org.apache.ibatis.annotations.Param"));
        }
        StringBuilder sb = new StringBuilder();
        for (IntrospectedColumn introspectedColumn : introspectedColumns) {
            FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
            importedTypes.add(type);
            Parameter parameter = new Parameter(type, introspectedColumn.getJavaProperty());
            if (annotate) {
                sb.setLength(0);
                //$NON-NLS-1$
                sb.append("@Param(\"");
                sb.append(introspectedColumn.getJavaProperty());
                //$NON-NLS-1$
                sb.append("\")");
                parameter.addAnnotation(sb.toString());
            }
            method.addParameter(parameter);
        }
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    addMapperAnnotations(method);
    if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.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 7 with Parameter

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

the class InsertMethodGenerator method addInterfaceElements.

@Override
public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = new Method();
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName(introspectedTable.getInsertStatementId());
    FullyQualifiedJavaType parameterType;
    if (isSimple) {
        parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    } else {
        parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    }
    importedTypes.add(parameterType);
    //$NON-NLS-1$
    method.addParameter(new Parameter(parameterType, "record"));
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    addMapperAnnotations(method);
    if (context.getPlugins().clientInsertMethodGenerated(method, interfaze, introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.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 8 with Parameter

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

the class SelectByExampleWithoutBLOBsMethodGenerator method addInterfaceElements.

@Override
public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(type);
    importedTypes.add(FullyQualifiedJavaType.getNewListInstance());
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
    FullyQualifiedJavaType listType;
    if (introspectedTable.getRules().generateBaseRecordClass()) {
        listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    } else if (introspectedTable.getRules().generatePrimaryKeyClass()) {
        listType = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
    } else {
        //$NON-NLS-1$
        throw new RuntimeException(getString("RuntimeError.12"));
    }
    importedTypes.add(listType);
    returnType.addTypeArgument(listType);
    method.setReturnType(returnType);
    method.setName(introspectedTable.getSelectByExampleStatementId());
    //$NON-NLS-1$
    method.addParameter(new Parameter(type, "example"));
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    addMapperAnnotations(interfaze, method);
    if (context.getPlugins().clientSelectByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.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 9 with Parameter

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

the class SelectByPrimaryKeyMethodGenerator method addInterfaceElements.

@Override
public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType returnType = introspectedTable.getRules().calculateAllFieldsClass();
    method.setReturnType(returnType);
    importedTypes.add(returnType);
    method.setName(introspectedTable.getSelectByPrimaryKeyStatementId());
    if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
        importedTypes.add(type);
        //$NON-NLS-1$
        method.addParameter(new Parameter(type, "key"));
    } else {
        // no primary key class - fields are in the base class
        // if more than one PK field, then we need to annotate the
        // parameters
        // for MyBatis3
        List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
        boolean annotate = introspectedColumns.size() > 1;
        if (annotate) {
            importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
            "org.apache.ibatis.annotations.Param"));
        }
        StringBuilder sb = new StringBuilder();
        for (IntrospectedColumn introspectedColumn : introspectedColumns) {
            FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
            importedTypes.add(type);
            Parameter parameter = new Parameter(type, introspectedColumn.getJavaProperty());
            if (annotate) {
                sb.setLength(0);
                //$NON-NLS-1$
                sb.append("@Param(\"");
                sb.append(introspectedColumn.getJavaProperty());
                //$NON-NLS-1$
                sb.append("\")");
                parameter.addAnnotation(sb.toString());
            }
            method.addParameter(parameter);
        }
    }
    addMapperAnnotations(interfaze, method);
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.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 10 with Parameter

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

the class TestResourceGenerator method simpleClassWithGeneratedAndCustomItems.

public static String simpleClassWithGeneratedAndCustomItems() {
    TopLevelClass tlc = new TopLevelClass(new FullyQualifiedJavaType("org.mybatis.test.SimpleClass"));
    tlc.setVisibility(JavaVisibility.PUBLIC);
    Field field = new Field("description", FullyQualifiedJavaType.getStringInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    commentGenerator.addFieldComment(field);
    tlc.addField(field);
    Method method = new Method("getDescription");
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addBodyLine("return description;");
    method.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addMethodComment(method);
    tlc.addMethod(method);
    method = new Method("setDescription");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "description"));
    method.addBodyLine("this.description = description;");
    method.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addMethodComment(method);
    tlc.addMethod(method);
    // no comments on these items - they should survive a merge
    FullyQualifiedJavaType bigDecimal = new FullyQualifiedJavaType("java.math.BigDecimal");
    tlc.addImportedType(bigDecimal);
    field = new Field("amount", bigDecimal);
    field.setVisibility(JavaVisibility.PRIVATE);
    tlc.addField(field);
    method = new Method("getAmount");
    method.setReturnType(bigDecimal);
    method.addBodyLine("return amount;");
    method.setVisibility(JavaVisibility.PUBLIC);
    tlc.addMethod(method);
    method = new Method("setAmount");
    method.addParameter(new Parameter(bigDecimal, "amount"));
    method.addBodyLine("this.amount = amount;");
    method.setVisibility(JavaVisibility.PUBLIC);
    tlc.addMethod(method);
    return tlc.getFormattedContent();
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Aggregations

Parameter (org.mybatis.generator.api.dom.java.Parameter)67 Method (org.mybatis.generator.api.dom.java.Method)66 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)54 TreeSet (java.util.TreeSet)20 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)17 Field (org.mybatis.generator.api.dom.java.Field)11 ArrayList (java.util.ArrayList)7 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)5 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)5 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)4 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)3 CommentGenerator (org.mybatis.generator.api.CommentGenerator)2 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)2 Interface (org.mybatis.generator.api.dom.java.Interface)2 DAOMethodNameCalculator (org.mybatis.generator.api.DAOMethodNameCalculator)1 Rules (org.mybatis.generator.internal.rules.Rules)1