Search in sources :

Example 21 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 (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)

Example 22 with Method

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

the class ExampleGenerator method getCriteriaInnerClass.

private InnerClass getCriteriaInnerClass() {
    Method method;
    InnerClass answer = new InnerClass(FullyQualifiedJavaType.getCriteriaInstance());
    answer.setVisibility(JavaVisibility.PUBLIC);
    answer.setStatic(true);
    answer.setSuperClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());
    context.getCommentGenerator().addClassComment(answer, introspectedTable, true);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("Criteria");
    method.setConstructor(true);
    //$NON-NLS-1$
    method.addBodyLine("super();");
    answer.addMethod(method);
    return answer;
}
Also used : InnerClass(org.mybatis.generator.api.dom.java.InnerClass) Method(org.mybatis.generator.api.dom.java.Method)

Example 23 with Method

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

the class ExampleGenerator method addtypeHandledObjectsAndMethods.

/**
     * This method adds all the extra methods and fields required to support a
     * user defined type handler on some column.
     * 
     * @param introspectedColumn
     * @param constructor
     * @param innerClass
     * @return a list of the names of all Lists added to the class by this
     *         method
     */
private List<String> addtypeHandledObjectsAndMethods(IntrospectedColumn introspectedColumn, Method constructor, InnerClass innerClass) {
    List<String> answer = new ArrayList<String>();
    StringBuilder sb = new StringBuilder();
    // add new private fields and public accessors in the class
    FullyQualifiedJavaType listOfMaps;
    if (generateForJava5) {
        listOfMaps = new FullyQualifiedJavaType(//$NON-NLS-1$
        "java.util.List<java.util.Map<java.lang.String, java.lang.Object>>");
    } else {
        //$NON-NLS-1$
        listOfMaps = new FullyQualifiedJavaType("java.util.List");
    }
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("CriteriaWithSingleValue");
    answer.add(sb.toString());
    Field field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(listOfMaps);
    field.setName(sb.toString());
    innerClass.addField(field);
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    sb.insert(0, "return ");
    sb.append(';');
    method.addBodyLine(sb.toString());
    innerClass.addMethod(method);
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("CriteriaWithListValue");
    answer.add(sb.toString());
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(listOfMaps);
    field.setName(sb.toString());
    innerClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    sb.insert(0, "return ");
    sb.append(';');
    method.addBodyLine(sb.toString());
    innerClass.addMethod(method);
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("CriteriaWithBetweenValue");
    answer.add(sb.toString());
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(listOfMaps);
    field.setName(sb.toString());
    innerClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    sb.insert(0, "return ");
    sb.append(';');
    method.addBodyLine(sb.toString());
    innerClass.addMethod(method);
    // add constructor initialization
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    if (generateForJava5) {
        sb.append(//$NON-NLS-1$;
        "CriteriaWithSingleValue = new ArrayList<Map<String, Object>>();");
    } else {
        //$NON-NLS-1$;
        sb.append("CriteriaWithSingleValue = new ArrayList();");
    }
    constructor.addBodyLine(sb.toString());
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    if (generateForJava5) {
        sb.append(//$NON-NLS-1$
        "CriteriaWithListValue = new ArrayList<Map<String, Object>>();");
    } else {
        //$NON-NLS-1$
        sb.append("CriteriaWithListValue = new ArrayList();");
    }
    constructor.addBodyLine(sb.toString());
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    if (generateForJava5) {
        sb.append(//$NON-NLS-1$
        "CriteriaWithBetweenValue = new ArrayList<Map<String, Object>>();");
    } else {
        //$NON-NLS-1$
        sb.append("CriteriaWithBetweenValue = new ArrayList();");
    }
    constructor.addBodyLine(sb.toString());
    // now add the methods for simplifying the individual field set methods
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("add");
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
    //$NON-NLS-1$
    sb.append("Criterion");
    method.setName(sb.toString());
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper(), //$NON-NLS-1$
        "value"));
    } else {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
        "value"));
    }
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
        //$NON-NLS-1$
        method.addBodyLine("if (value == null) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
    }
    if (generateForJava5) {
        method.addBodyLine(//$NON-NLS-1$
        "Map<String, Object> map = new HashMap<String, Object>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("Map map = new HashMap();");
    }
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"condition\", condition);");
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"value\", value);");
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("CriteriaWithSingleValue.add(map);");
    method.addBodyLine(sb.toString());
    innerClass.addMethod(method);
    FullyQualifiedJavaType listOfObjects = FullyQualifiedJavaType.getNewListInstance();
    if (generateForJava5) {
        if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            listOfObjects.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
        } else {
            listOfObjects.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType());
        }
    }
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("add");
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
    //$NON-NLS-1$
    sb.append("Criterion");
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    method.setName(sb.toString());
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    //$NON-NLS-1$
    method.addParameter(new Parameter(listOfObjects, "values"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$NON-NLS-1$
    method.addBodyLine("if (values == null || values.size() == 0) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    if (generateForJava5) {
        method.addBodyLine(//$NON-NLS-1$
        "Map<String, Object> map = new HashMap<String, Object>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("Map map = new HashMap();");
    }
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"condition\", condition);");
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"values\", values);");
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("CriteriaWithListValue.add(map);");
    method.addBodyLine(sb.toString());
    innerClass.addMethod(method);
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("add");
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
    //$NON-NLS-1$
    sb.append("Criterion");
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    method.setName(sb.toString());
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper(), //$NON-NLS-1$
        "value1"));
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper(), //$NON-NLS-1$
        "value2"));
    } else {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
        "value1"));
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
        "value2"));
    }
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$NON-NLS-1$
    method.addBodyLine("if (value1 == null || value2 == null) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    if (generateForJava5) {
        String shortName;
        if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            shortName = introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName();
        } else {
            shortName = introspectedColumn.getFullyQualifiedJavaType().getShortName();
        }
        sb.setLength(0);
        //$NON-NLS-1$
        sb.append("List<");
        sb.append(shortName);
        //$NON-NLS-1$
        sb.append("> list = new ArrayList<");
        sb.append(shortName);
        //$NON-NLS-1$
        sb.append(">();");
        method.addBodyLine(sb.toString());
    } else {
        //$NON-NLS-1$
        method.addBodyLine("List list = new ArrayList();");
    }
    //$NON-NLS-1$
    method.addBodyLine("list.add(value1);");
    //$NON-NLS-1$
    method.addBodyLine("list.add(value2);");
    if (generateForJava5) {
        method.addBodyLine(//$NON-NLS-1$
        "Map<String, Object> map = new HashMap<String, Object>();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("Map map = new HashMap();");
    }
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"condition\", condition);");
    //$NON-NLS-1$
    method.addBodyLine("map.put(\"values\", list);");
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("CriteriaWithBetweenValue.add(map);");
    method.addBodyLine(sb.toString());
    innerClass.addMethod(method);
    return answer;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) Parameter(org.mybatis.generator.api.dom.java.Parameter) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method)

Example 24 with Method

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

the class RecordWithBLOBsGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.9", //$NON-NLS-1$
    table.toString()));
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();
    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable.getRecordWithBLOBsType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    if (introspectedTable.getRules().generateBaseRecordClass()) {
        topLevelClass.setSuperClass(introspectedTable.getBaseRecordType());
    } else {
        topLevelClass.setSuperClass(introspectedTable.getPrimaryKeyType());
    }
    String rootClass = getRootClass();
    for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }
        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.RECORD_WITH_BLOBS)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }
        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.RECORD_WITH_BLOBS)) {
            topLevelClass.addMethod(method);
        }
        method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.RECORD_WITH_BLOBS)) {
            topLevelClass.addMethod(method);
        }
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelRecordWithBLOBsClassGenerated(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) 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 25 with Method

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

the class BaseRecordGenerator 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);
    List<IntrospectedColumn> constructorColumns = includeBLOBColumns() ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns();
    for (IntrospectedColumn introspectedColumn : constructorColumns) {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty()));
        topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
    }
    StringBuilder sb = new StringBuilder();
    if (introspectedTable.getRules().generatePrimaryKeyClass()) {
        boolean comma = false;
        //$NON-NLS-1$
        sb.append("super(");
        for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
            if (comma) {
                //$NON-NLS-1$
                sb.append(", ");
            } else {
                comma = true;
            }
            sb.append(introspectedColumn.getJavaProperty());
        }
        //$NON-NLS-1$
        sb.append(");");
        method.addBodyLine(sb.toString());
    }
    List<IntrospectedColumn> introspectedColumns = getColumnsInThisClass();
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        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)

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