Search in sources :

Example 11 with Parameter

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

the class TestResourceGenerator method simpleClassWithAllGeneratedItems.

public static String simpleClassWithAllGeneratedItems() {
    TopLevelClass tlc = new TopLevelClass(new FullyQualifiedJavaType("org.mybatis.test.SimpleClass"));
    tlc.setVisibility(JavaVisibility.PUBLIC);
    Field field = new Field("id", FullyQualifiedJavaType.getIntInstance());
    field.setVisibility(JavaVisibility.PRIVATE);
    commentGenerator.addFieldComment(field);
    tlc.addField(field);
    Method method = new Method("getId");
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.addBodyLine("return id;");
    method.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addMethodComment(method);
    tlc.addMethod(method);
    method = new Method("setId");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "id"));
    method.addBodyLine("this.id = id;");
    method.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addMethodComment(method);
    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)

Example 12 with Parameter

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

the class TestResourceGenerator method simpleInterfaceWithAllGeneratedItems.

public static String simpleInterfaceWithAllGeneratedItems() {
    Interface itf = new Interface(new FullyQualifiedJavaType("org.mybatis.test.SimpleInterface"));
    itf.setVisibility(JavaVisibility.PUBLIC);
    Method method = new Method("count");
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    commentGenerator.addMethodComment(method);
    itf.addMethod(method);
    method = new Method("add");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "a"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "b"));
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    commentGenerator.addMethodComment(method);
    itf.addMethod(method);
    return itf.getFormattedContent();
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method) Interface(org.mybatis.generator.api.dom.java.Interface)

Example 13 with Parameter

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

the class ExampleGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.6", //$NON-NLS-1$
    table.toString()));
    CommentGenerator commentGenerator = context.getCommentGenerator();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    TopLevelClass topLevelClass = new TopLevelClass(type);
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    // add default constructor
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName(type.getShortName());
    if (generateForJava5) {
        //$NON-NLS-1$
        method.addBodyLine("oredCriteria = new ArrayList<Criteria>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("oredCriteria = new ArrayList();");
    }
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // add shallow copy constructor if the update by
    // example methods are enabled - because the parameter
    // class for update by example methods will subclass this class
    Rules rules = introspectedTable.getRules();
    if (rules.generateUpdateByExampleSelective() || rules.generateUpdateByExampleWithBLOBs() || rules.generateUpdateByExampleWithoutBLOBs()) {
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        method.setConstructor(true);
        method.setName(type.getShortName());
        //$NON-NLS-1$
        method.addParameter(new Parameter(type, "example"));
        //$NON-NLS-1$
        method.addBodyLine("this.orderByClause = example.orderByClause;");
        //$NON-NLS-1$
        method.addBodyLine("this.oredCriteria = example.oredCriteria;");
        //$NON-NLS-1$
        method.addBodyLine("this.distinct = example.distinct;");
        commentGenerator.addGeneralMethodComment(method, introspectedTable);
        topLevelClass.addMethod(method);
    }
    // add field, getter, setter for orderby clause
    Field field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getStringInstance());
    //$NON-NLS-1$
    field.setName("orderByClause");
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("setOrderByClause");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "orderByClause"));
    //$NON-NLS-1$
    method.addBodyLine("this.orderByClause = orderByClause;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    //$NON-NLS-1$
    method.setName("getOrderByClause");
    //$NON-NLS-1$
    method.addBodyLine("return orderByClause;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // add field, getter, setter for distinct
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    //$NON-NLS-1$
    field.setName("distinct");
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("setDistinct");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), //$NON-NLS-1$
    "distinct"));
    //$NON-NLS-1$
    method.addBodyLine("this.distinct = distinct;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    //$NON-NLS-1$
    method.setName("isDistinct");
    //$NON-NLS-1$
    method.addBodyLine("return distinct;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // add field and methods for the list of ored criteria
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    FullyQualifiedJavaType fqjt;
    if (generateForJava5) {
        //$NON-NLS-1$
        fqjt = new FullyQualifiedJavaType("java.util.List<Criteria>");
    } else {
        //$NON-NLS-1$
        fqjt = new FullyQualifiedJavaType("java.util.List");
    }
    field.setType(fqjt);
    //$NON-NLS-1$
    field.setName("oredCriteria");
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(fqjt);
    //$NON-NLS-1$
    method.setName("getOredCriteria");
    //$NON-NLS-1$
    method.addBodyLine("return oredCriteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("or");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getCriteriaInstance(), //$NON-NLS-1$
    "criteria"));
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("or");
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    //$NON-NLS-1$
    method.addBodyLine("Criteria criteria = createCriteriaInternal();");
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);");
    //$NON-NLS-1$
    method.addBodyLine("return criteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("createCriteria");
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    //$NON-NLS-1$
    method.addBodyLine("Criteria criteria = createCriteriaInternal();");
    //$NON-NLS-1$
    method.addBodyLine("if (oredCriteria.size() == 0) {");
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);");
    //$NON-NLS-1$
    method.addBodyLine("}");
    //$NON-NLS-1$
    method.addBodyLine("return criteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("createCriteriaInternal");
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    //$NON-NLS-1$
    method.addBodyLine("Criteria criteria = new Criteria();");
    //$NON-NLS-1$
    method.addBodyLine("return criteria;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("clear");
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria.clear();");
    //$NON-NLS-1$
    method.addBodyLine("orderByClause = null;");
    //$NON-NLS-1$
    method.addBodyLine("distinct = false;");
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);
    // now generate the inner class that holds the AND conditions
    topLevelClass.addInnerClass(getGeneratedCriteriaInnerClass(topLevelClass));
    topLevelClass.addInnerClass(getCriteriaInnerClass());
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelExampleClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method) Rules(org.mybatis.generator.internal.rules.Rules)

Example 14 with Parameter

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

the class ExampleGenerator method getSetBetweenOrNotBetweenMethod.

/**
     * Generates methods that set between and not between conditions
     * 
     * @param introspectedColumn
     * @param betweenMethod
     * @return a generated method for the between or not between method
     */
private Method getSetBetweenOrNotBetweenMethod(IntrospectedColumn introspectedColumn, boolean betweenMethod) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
    //$NON-NLS-1$
    method.addParameter(new Parameter(type, "value1"));
    //$NON-NLS-1$
    method.addParameter(new Parameter(type, "value2"));
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    //$NON-NLS-1$
    sb.insert(0, "and");
    if (betweenMethod) {
        //$NON-NLS-1$
        sb.append("Between");
    } else {
        //$NON-NLS-1$
        sb.append("NotBetween");
    }
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);
    if (introspectedColumn.isJDBCDateColumn()) {
        //$NON-NLS-1$
        sb.append("addCriterionForJDBCDate(\"");
    } else if (introspectedColumn.isJDBCTimeColumn()) {
        //$NON-NLS-1$
        sb.append("addCriterionForJDBCTime(\"");
    } else if (stringHasValue(introspectedColumn.getTypeHandler())) {
        //$NON-NLS-1$
        sb.append("add");
        sb.append(introspectedColumn.getJavaProperty());
        sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
        //$NON-NLS-1$
        sb.append("Criterion(\"");
    } else {
        //$NON-NLS-1$
        sb.append("addCriterion(\"");
    }
    sb.append(Ibatis2FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    if (betweenMethod) {
        //$NON-NLS-1$
        sb.append(" between");
    } else {
        //$NON-NLS-1$
        sb.append(" not between");
    }
    //$NON-NLS-1$
    sb.append("\", ");
    if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive() && !introspectedTable.isJava5Targeted()) {
        //$NON-NLS-1$
        sb.append("new ");
        sb.append(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName());
        //$NON-NLS-1$
        sb.append("(value1), ");
        //$NON-NLS-1$
        sb.append("new ");
        sb.append(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName());
        //$NON-NLS-1$
        sb.append("(value2)");
    } else {
        //$NON-NLS-1$
        sb.append("value1, value2");
    }
    //$NON-NLS-1$
    sb.append(", \"");
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("\");");
    method.addBodyLine(sb.toString());
    //$NON-NLS-1$
    method.addBodyLine("return (Criteria) this;");
    return method;
}
Also used : 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 15 with Parameter

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

the class ExampleGenerator method getSetInOrNotInMethod.

/**
     * 
     * @param introspectedColumn
     * @param inMethod
     *            if true generates an "in" method, else generates a "not in"
     *            method
     * @return a generated method for the in or not in method
     */
private Method getSetInOrNotInMethod(IntrospectedColumn introspectedColumn, boolean inMethod) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType type = FullyQualifiedJavaType.getNewListInstance();
    if (generateForJava5) {
        if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
        } else {
            type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType());
        }
    }
    //$NON-NLS-1$
    method.addParameter(new Parameter(type, "values"));
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    //$NON-NLS-1$
    sb.insert(0, "and");
    if (inMethod) {
        //$NON-NLS-1$
        sb.append("In");
    } else {
        //$NON-NLS-1$
        sb.append("NotIn");
    }
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);
    if (introspectedColumn.isJDBCDateColumn()) {
        //$NON-NLS-1$
        sb.append("addCriterionForJDBCDate(\"");
    } else if (introspectedColumn.isJDBCTimeColumn()) {
        //$NON-NLS-1$
        sb.append("addCriterionForJDBCTime(\"");
    } else if (stringHasValue(introspectedColumn.getTypeHandler())) {
        //$NON-NLS-1$
        sb.append("add");
        sb.append(introspectedColumn.getJavaProperty());
        sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
        //$NON-NLS-1$
        sb.append("Criterion(\"");
    } else {
        //$NON-NLS-1$
        sb.append("addCriterion(\"");
    }
    sb.append(Ibatis2FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    if (inMethod) {
        //$NON-NLS-1$
        sb.append(" in");
    } else {
        //$NON-NLS-1$
        sb.append(" not in");
    }
    //$NON-NLS-1$
    sb.append("\", values, \"");
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("\");");
    method.addBodyLine(sb.toString());
    //$NON-NLS-1$
    method.addBodyLine("return (Criteria) this;");
    return method;
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) 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