Search in sources :

Example 31 with Method

use of org.mybatis.generator.api.dom.java.Method 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 (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(MyBatis3FormattingUtilities.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)

Example 32 with Method

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

the class PrimaryKeyGenerator method addParameterizedConstructor.

private void addParameterizedConstructor(TopLevelClass topLevelClass) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName(topLevelClass.getType().getShortName());
    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);
        //$NON-NLS-1$
        sb.append("this.");
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(introspectedColumn.getJavaProperty());
        sb.append(';');
        method.addBodyLine(sb.toString());
    }
    topLevelClass.addMethod(method);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 33 with Method

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

the class PrimaryKeyGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.7", //$NON-NLS-1$
    table.toString()));
    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;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) JavaBeansUtil.getJavaBeansField(org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) 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) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method) Plugin(org.mybatis.generator.api.Plugin)

Example 34 with Method

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

the class Test1Generator method generate.

public List<CompilationUnit> generate() {
    FullyQualifiedJavaType cls = new FullyQualifiedJavaType(BASE_PACKAGE + ".SomeClass");
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    TopLevelClass tlcMain = generateFieldTypeMain();
    TopLevelClass tlcSub1 = generateFieldTypeSub1();
    TopLevelClass tlcTcSub1 = generateTestClassSub1();
    TopLevelClass tlcSub2 = generateFieldTypeSub2();
    answer.add(tlcMain);
    answer.add(tlcSub1);
    answer.add(tlcTcSub1);
    answer.add(tlcSub2);
    TopLevelClass topLvlClass = new TopLevelClass(cls);
    topLvlClass.setVisibility(JavaVisibility.PUBLIC);
    topLvlClass.addImportedType(tlcTcSub1.getType());
    Field field = new Field("main", tlcMain.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    field = new Field("tcSub1", tlcTcSub1.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    field = new Field("sub1", tlcSub1.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    field = new Field("sub2", tlcSub2.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    Method m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("executeMain");
    m.addBodyLine("main.mainMethod();");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("setMain");
    m.addParameter(new Parameter(tlcMain.getType(), "main"));
    m.addBodyLine("this.main = main;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("getMain");
    m.setReturnType(tlcMain.getType());
    m.addBodyLine("return main;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("executeSub1");
    m.addBodyLine("sub1.sub1Method();");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("setSub1");
    m.addParameter(new Parameter(tlcSub1.getType(), "sub1"));
    m.addBodyLine("this.sub1 = sub1;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("getSub1");
    m.setReturnType(tlcSub1.getType());
    m.addBodyLine("return sub1;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("executeSub2");
    m.addBodyLine("sub2.sub2Method();");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("setSub2");
    m.addParameter(new Parameter(tlcSub2.getType(), "sub2"));
    m.addBodyLine("this.sub2 = sub2;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("getSub2");
    m.setReturnType(tlcSub2.getType());
    m.addBodyLine("return sub2;");
    topLvlClass.addMethod(m);
    answer.add(topLvlClass);
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) 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)

Example 35 with Method

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

the class UpdateByPrimaryKeySelectiveMethodGenerator method getMethodShell.

private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    FullyQualifiedJavaType parameterType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        parameterType = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType());
    } else {
        parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    }
    importedTypes.add(parameterType);
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setName(getDAOMethodNameCalculator().getUpdateByPrimaryKeySelectiveMethodName(introspectedTable));
    //$NON-NLS-1$
    method.addParameter(new Parameter(parameterType, "record"));
    for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
        method.addException(fqjt);
        importedTypes.add(fqjt);
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    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

Method (org.mybatis.generator.api.dom.java.Method)121 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)97 Parameter (org.mybatis.generator.api.dom.java.Parameter)66 TreeSet (java.util.TreeSet)51 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)29 Field (org.mybatis.generator.api.dom.java.Field)20 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)17 ArrayList (java.util.ArrayList)14 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)11 CommentGenerator (org.mybatis.generator.api.CommentGenerator)10 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)10 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)9 Plugin (org.mybatis.generator.api.Plugin)7 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 Interface (org.mybatis.generator.api.dom.java.Interface)2 PrimitiveTypeWrapper (org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper)2 DAOMethodNameCalculator (org.mybatis.generator.api.DAOMethodNameCalculator)1 Rules (org.mybatis.generator.internal.rules.Rules)1