Search in sources :

Example 36 with FullyQualifiedJavaType

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

the class AbstractJavaMapperMethodGenerator method getResultAnnotation.

protected String getResultAnnotation(Interface interfaze, IntrospectedColumn introspectedColumn, boolean idColumn, boolean constructorBased) {
    StringBuilder sb = new StringBuilder();
    if (constructorBased) {
        interfaze.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
        //$NON-NLS-1$
        sb.append("@Arg(column=\"");
        sb.append(getRenamedColumnNameForResultMap(introspectedColumn));
        //$NON-NLS-1$
        sb.append("\", javaType=");
        sb.append(introspectedColumn.getFullyQualifiedJavaType().getShortName());
        //$NON-NLS-1$
        sb.append(".class");
    } else {
        //$NON-NLS-1$
        sb.append("@Result(column=\"");
        sb.append(getRenamedColumnNameForResultMap(introspectedColumn));
        //$NON-NLS-1$
        sb.append("\", property=\"");
        sb.append(introspectedColumn.getJavaProperty());
        sb.append('\"');
    }
    if (stringHasValue(introspectedColumn.getTypeHandler())) {
        FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedColumn.getTypeHandler());
        interfaze.addImportedType(fqjt);
        //$NON-NLS-1$
        sb.append(", typeHandler=");
        sb.append(fqjt.getShortName());
        //$NON-NLS-1$
        sb.append(".class");
    }
    //$NON-NLS-1$
    sb.append(", jdbcType=JdbcType.");
    sb.append(introspectedColumn.getJdbcTypeName());
    if (idColumn) {
        //$NON-NLS-1$
        sb.append(", id=true");
    }
    sb.append(')');
    return sb.toString();
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 37 with FullyQualifiedJavaType

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

the class JavaTypeResolverDefaultImpl method calculateJavaType.

public FullyQualifiedJavaType calculateJavaType(IntrospectedColumn introspectedColumn) {
    FullyQualifiedJavaType answer = null;
    JdbcTypeInformation jdbcTypeInformation = typeMap.get(introspectedColumn.getJdbcType());
    if (jdbcTypeInformation != null) {
        answer = jdbcTypeInformation.getFullyQualifiedJavaType();
        answer = overrideDefaultType(introspectedColumn, answer);
    }
    return answer;
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 38 with FullyQualifiedJavaType

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

the class ExampleGenerator method getGeneratedCriteriaInnerClass.

private InnerClass getGeneratedCriteriaInnerClass(TopLevelClass topLevelClass) {
    Field field;
    Method method;
    InnerClass answer = new InnerClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());
    answer.setVisibility(JavaVisibility.PROTECTED);
    answer.setStatic(true);
    answer.setAbstract(true);
    context.getCommentGenerator().addClassComment(answer, introspectedTable);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("GeneratedCriteria");
    method.setConstructor(true);
    //$NON-NLS-1$
    method.addBodyLine("super();");
    //$NON-NLS-1$
    method.addBodyLine("criteria = new ArrayList<Criterion>();");
    answer.addMethod(method);
    List<String> criteriaLists = new ArrayList<String>();
    //$NON-NLS-1$
    criteriaLists.add("criteria");
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            String name = addtypeHandledObjectsAndMethods(introspectedColumn, method, answer);
            criteriaLists.add(name);
        }
    }
    // now generate the isValid method
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("isValid");
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    StringBuilder sb = new StringBuilder();
    Iterator<String> strIter = criteriaLists.iterator();
    //$NON-NLS-1$
    sb.append("return ");
    sb.append(strIter.next());
    //$NON-NLS-1$
    sb.append(".size() > 0");
    if (!strIter.hasNext()) {
        sb.append(';');
    }
    method.addBodyLine(sb.toString());
    while (strIter.hasNext()) {
        sb.setLength(0);
        OutputUtilities.javaIndent(sb, 1);
        //$NON-NLS-1$
        sb.append("|| ");
        sb.append(strIter.next());
        //$NON-NLS-1$
        sb.append(".size() > 0");
        if (!strIter.hasNext()) {
            sb.append(';');
        }
        method.addBodyLine(sb.toString());
    }
    answer.addMethod(method);
    // now generate the getAllCriteria method
    if (criteriaLists.size() > 1) {
        field = new Field();
        //$NON-NLS-1$
        field.setName("allCriteria");
        //$NON-NLS-1$
        field.setType(new FullyQualifiedJavaType("List<Criterion>"));
        field.setVisibility(JavaVisibility.PROTECTED);
        answer.addField(field);
    }
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.setName("getAllCriteria");
    //$NON-NLS-1$
    method.setReturnType(new FullyQualifiedJavaType("List<Criterion>"));
    if (criteriaLists.size() < 2) {
        //$NON-NLS-1$
        method.addBodyLine("return criteria;");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("if (allCriteria == null) {");
        //$NON-NLS-1$
        method.addBodyLine("allCriteria = new ArrayList<Criterion>();");
        strIter = criteriaLists.iterator();
        while (strIter.hasNext()) {
            //$NON-NLS-1$
            method.addBodyLine(String.format("allCriteria.addAll(%s);", strIter.next()));
        }
        //$NON-NLS-1$
        method.addBodyLine("}");
        //$NON-NLS-1$
        method.addBodyLine("return allCriteria;");
    }
    answer.addMethod(method);
    // now we need to generate the methods that will be used in the SqlMap
    // to generate the dynamic where clause
    topLevelClass.addImportedType(FullyQualifiedJavaType.getNewListInstance());
    topLevelClass.addImportedType(FullyQualifiedJavaType.getNewArrayListInstance());
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    FullyQualifiedJavaType listOfCriterion = new FullyQualifiedJavaType(//$NON-NLS-1$
    "java.util.List<Criterion>");
    field.setType(listOfCriterion);
    //$NON-NLS-1$
    field.setName("criteria");
    answer.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    method.addBodyLine("return criteria;");
    answer.addMethod(method);
    // now add the methods for simplifying the individual field set methods
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("addCriterion");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    //$NON-NLS-1$
    method.addBodyLine("if (condition == null) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Value for condition cannot be null\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    //$NON-NLS-1$
    method.addBodyLine("criteria.add(new Criterion(condition));");
    if (criteriaLists.size() > 1) {
        //$NON-NLS-1$
        method.addBodyLine("allCriteria = null;");
    }
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("addCriterion");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$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("}");
    //$NON-NLS-1$
    method.addBodyLine("criteria.add(new Criterion(condition, value));");
    if (criteriaLists.size() > 1) {
        //$NON-NLS-1$
        method.addBodyLine("allCriteria = null;");
    }
    answer.addMethod(method);
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    method.setName("addCriterion");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value1"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$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("}");
    method.addBodyLine(//$NON-NLS-1$
    "criteria.add(new Criterion(condition, value1, value2));");
    if (criteriaLists.size() > 1) {
        //$NON-NLS-1$
        method.addBodyLine("allCriteria = null;");
    }
    answer.addMethod(method);
    FullyQualifiedJavaType listOfDates = new FullyQualifiedJavaType(//$NON-NLS-1$
    "java.util.List<java.util.Date>");
    if (introspectedTable.hasJDBCDateColumns()) {
        topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
        topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCDate");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$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("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Date(value.getTime()), property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCDate");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        //$NON-NLS-1$
        method.addParameter(new Parameter(listOfDates, "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("}");
        method.addBodyLine(//$NON-NLS-1$
        "List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();");
        //$NON-NLS-1$
        method.addBodyLine("Iterator<Date> iter = values.iterator();");
        //$NON-NLS-1$
        method.addBodyLine("while (iter.hasNext()) {");
        method.addBodyLine(//$NON-NLS-1$
        "dateList.add(new java.sql.Date(iter.next().getTime()));");
        //$NON-NLS-1$
        method.addBodyLine("}");
        //$NON-NLS-1$
        method.addBodyLine("addCriterion(condition, dateList, property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCDate");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value1"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$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("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);");
        answer.addMethod(method);
    }
    if (introspectedTable.hasJDBCTimeColumns()) {
        topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
        topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCTime");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "property"));
        //$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("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Time(value.getTime()), property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCTime");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        //$NON-NLS-1$
        method.addParameter(new Parameter(listOfDates, "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("}");
        method.addBodyLine(//$NON-NLS-1$
        "List<java.sql.Time> timeList = new ArrayList<java.sql.Time>();");
        //$NON-NLS-1$
        method.addBodyLine("Iterator<Date> iter = values.iterator();");
        //$NON-NLS-1$
        method.addBodyLine("while (iter.hasNext()) {");
        method.addBodyLine(//$NON-NLS-1$
        "timeList.add(new java.sql.Time(iter.next().getTime()));");
        //$NON-NLS-1$
        method.addBodyLine("}");
        //$NON-NLS-1$
        method.addBodyLine("addCriterion(condition, timeList, property);");
        answer.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PROTECTED);
        //$NON-NLS-1$
        method.setName("addCriterionForJDBCTime");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
        "condition"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$NON-NLS-1$
        "value1"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), //$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("}");
        method.addBodyLine(//$NON-NLS-1$
        "addCriterion(condition, new java.sql.Time(value1.getTime()), new java.sql.Time(value2.getTime()), property);");
        answer.addMethod(method);
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
        // here we need to add the individual methods for setting the
        // conditions for a field
        answer.addMethod(getSetNullMethod(introspectedColumn));
        answer.addMethod(getSetNotNullMethod(introspectedColumn));
        answer.addMethod(getSetEqualMethod(introspectedColumn));
        answer.addMethod(getSetNotEqualMethod(introspectedColumn));
        answer.addMethod(getSetGreaterThanMethod(introspectedColumn));
        answer.addMethod(getSetGreaterThenOrEqualMethod(introspectedColumn));
        answer.addMethod(getSetLessThanMethod(introspectedColumn));
        answer.addMethod(getSetLessThanOrEqualMethod(introspectedColumn));
        if (introspectedColumn.isJdbcCharacterColumn()) {
            answer.addMethod(getSetLikeMethod(introspectedColumn));
            answer.addMethod(getSetNotLikeMethod(introspectedColumn));
        }
        answer.addMethod(getSetInOrNotInMethod(introspectedColumn, true));
        answer.addMethod(getSetInOrNotInMethod(introspectedColumn, false));
        answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, true));
        answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, false));
    }
    return answer;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) InnerClass(org.mybatis.generator.api.dom.java.InnerClass) ArrayList(java.util.ArrayList) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString)

Example 39 with FullyQualifiedJavaType

use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType 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());
    //$NON-NLS-1$
    method.addBodyLine("oredCriteria = new ArrayList<Criteria>();");
    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 = new FullyQualifiedJavaType(//$NON-NLS-1$
    "java.util.List<Criteria>");
    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());
    topLevelClass.addInnerClass(getCriterionInnerClass());
    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)

Example 40 with FullyQualifiedJavaType

use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType 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 the name of the List added to the class by this method
     */
private String addtypeHandledObjectsAndMethods(IntrospectedColumn introspectedColumn, Method constructor, InnerClass innerClass) {
    String answer;
    StringBuilder sb = new StringBuilder();
    // add new private field and public accessor in the class
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("Criteria");
    answer = sb.toString();
    Field field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    field.setType(new FullyQualifiedJavaType("java.util.List<Criterion>"));
    field.setName(answer);
    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);
    // add constructor initialization
    sb.setLength(0);
    sb.append(field.getName());
    //$NON-NLS-1$;
    sb.append(" = new ArrayList<Criterion>();");
    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"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$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("}");
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%s.add(new Criterion(condition, value, \"%s\"));", field.getName(), introspectedColumn.getTypeHandler()));
    //$NON-NLS-1$
    method.addBodyLine("allCriteria = null;");
    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"));
    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"));
    if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
        //$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("}");
    }
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%s.add(new Criterion(condition, value1, value2, \"%s\"));", field.getName(), introspectedColumn.getTypeHandler()));
    //$NON-NLS-1$
    method.addBodyLine("allCriteria = null;");
    innerClass.addMethod(method);
    return answer;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) 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)

Aggregations

FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)163 Method (org.mybatis.generator.api.dom.java.Method)97 Parameter (org.mybatis.generator.api.dom.java.Parameter)54 TreeSet (java.util.TreeSet)53 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)32 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)19 Field (org.mybatis.generator.api.dom.java.Field)19 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)19 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)14 CommentGenerator (org.mybatis.generator.api.CommentGenerator)11 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)11 Interface (org.mybatis.generator.api.dom.java.Interface)10 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)7 GeneratedKey (org.mybatis.generator.config.GeneratedKey)7 Plugin (org.mybatis.generator.api.Plugin)5 Attribute (org.mybatis.generator.api.dom.xml.Attribute)5 TextElement (org.mybatis.generator.api.dom.xml.TextElement)5 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)5 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)5