Search in sources :

Example 1 with InnerClass

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

the class UpdateByExampleParmsInnerclassGenerator method addImplementationElements.

@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    topLevelClass.addImportedType(new FullyQualifiedJavaType(introspectedTable.getExampleType()));
    InnerClass innerClass = new InnerClass(new FullyQualifiedJavaType(//$NON-NLS-1$
    "UpdateByExampleParms"));
    innerClass.setVisibility(JavaVisibility.PROTECTED);
    innerClass.setStatic(true);
    innerClass.setSuperClass(introspectedTable.getExampleType());
    context.getCommentGenerator().addClassComment(innerClass, introspectedTable);
    Method method = new Method();
    method.setConstructor(true);
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName(innerClass.getType().getShortName());
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "record"));
    method.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), //$NON-NLS-1$
    "example"));
    //$NON-NLS-1$
    method.addBodyLine("super(example);");
    //$NON-NLS-1$
    method.addBodyLine("this.record = record;");
    innerClass.addMethod(method);
    Field field = new Field();
    field.setVisibility(JavaVisibility.PRIVATE);
    field.setType(FullyQualifiedJavaType.getObjectInstance());
    //$NON-NLS-1$
    field.setName("record");
    innerClass.addField(field);
    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getObjectInstance());
    //$NON-NLS-1$
    method.setName("getRecord");
    //$NON-NLS-1$
    method.addBodyLine("return record;");
    innerClass.addMethod(method);
    topLevelClass.addInnerClass(innerClass);
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) InnerClass(org.mybatis.generator.api.dom.java.InnerClass) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 2 with InnerClass

use of org.mybatis.generator.api.dom.java.InnerClass 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 3 with InnerClass

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

the class ExampleGenerator method getGeneratedCriteriaInnerClass.

private InnerClass getGeneratedCriteriaInnerClass(TopLevelClass topLevelClass) {
    Field field;
    InnerClass answer = new InnerClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());
    answer.setVisibility(JavaVisibility.PROTECTED);
    answer.setStatic(true);
    answer.setAbstract(true);
    context.getCommentGenerator().addClassComment(answer, introspectedTable);
    // $NON-NLS-1$
    Method method = new Method("GeneratedCriteria");
    method.setVisibility(JavaVisibility.PROTECTED);
    method.setConstructor(true);
    // $NON-NLS-1$
    method.addBodyLine("super();");
    // $NON-NLS-1$
    method.addBodyLine("criteria = new ArrayList<>();");
    answer.addMethod(method);
    List<String> criteriaLists = new ArrayList<>();
    // $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
    // $NON-NLS-1$
    method = new Method("isValid");
    method.setVisibility(JavaVisibility.PUBLIC);
    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 // $NON-NLS-1$
        Field(// $NON-NLS-1$
        "allCriteria", // $NON-NLS-1$
        new FullyQualifiedJavaType("List<Criterion>"));
        field.setVisibility(JavaVisibility.PROTECTED);
        answer.addField(field);
    }
    // $NON-NLS-1$
    method = new Method("getAllCriteria");
    method.setVisibility(JavaVisibility.PUBLIC);
    // $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<>();");
        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());
    FullyQualifiedJavaType listOfCriterion = // $NON-NLS-1$
    new FullyQualifiedJavaType("java.util.List<Criterion>");
    // $NON-NLS-1$
    field = new Field("criteria", listOfCriterion);
    field.setVisibility(JavaVisibility.PROTECTED);
    answer.addField(field);
    method = new Method(getGetterMethodName(field.getName(), field.getType()));
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    // $NON-NLS-1$
    method.addBodyLine("return criteria;");
    answer.addMethod(method);
    // now add the methods for simplifying the individual field set methods
    // $NON-NLS-1$
    method = new Method("addCriterion");
    method.setVisibility(JavaVisibility.PROTECTED);
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
    // $NON-NLS-1$
    method.addBodyLine("if (condition == null) {");
    // $NON-NLS-1$
    method.addBodyLine("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);
    // $NON-NLS-1$
    method = new Method("addCriterion");
    method.setVisibility(JavaVisibility.PROTECTED);
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value"));
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "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);
    // $NON-NLS-1$
    method = new Method("addCriterion");
    method.setVisibility(JavaVisibility.PROTECTED);
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value1"));
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value2"));
    // $NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "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("}");
    // $NON-NLS-1$
    method.addBodyLine("criteria.add(new Criterion(condition, value1, value2));");
    if (criteriaLists.size() > 1) {
        // $NON-NLS-1$
        method.addBodyLine("allCriteria = null;");
    }
    answer.addMethod(method);
    FullyQualifiedJavaType listOfDates = // $NON-NLS-1$
    new FullyQualifiedJavaType("java.util.List<java.util.Date>");
    if (introspectedTable.hasJDBCDateColumns()) {
        topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
        topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
        // $NON-NLS-1$
        method = new Method("addCriterionForJDBCDate");
        method.setVisibility(JavaVisibility.PROTECTED);
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), "value"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "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);
        // $NON-NLS-1$
        method = new Method("addCriterionForJDBCDate");
        method.setVisibility(JavaVisibility.PROTECTED);
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(listOfDates, "values"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "property"));
        // $NON-NLS-1$
        method.addBodyLine("if (values == null || values.size() == 0) {");
        method.addBodyLine(// $NON-NLS-1$
        "throw new RuntimeException(\"Value list for \" + property + \"" + // $NON-NLS-1$
        " cannot be null or empty\");");
        // $NON-NLS-1$
        method.addBodyLine("}");
        // $NON-NLS-1$
        method.addBodyLine("List<java.sql.Date> dateList = new ArrayList<>();");
        // $NON-NLS-1$
        method.addBodyLine("Iterator<Date> iter = values.iterator();");
        // $NON-NLS-1$
        method.addBodyLine("while (iter.hasNext()) {");
        // $NON-NLS-1$
        method.addBodyLine("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);
        // $NON-NLS-1$
        method = new Method("addCriterionForJDBCDate");
        method.setVisibility(JavaVisibility.PROTECTED);
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), "value1"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), "value2"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "property"));
        // $NON-NLS-1$
        method.addBodyLine("if (value1 == null || value2 == null) {");
        method.addBodyLine(// $NON-NLS-1$
        "throw new RuntimeException(\"Between values for \" + property + \"" + // $NON-NLS-1$
        " cannot be null\");");
        // $NON-NLS-1$
        method.addBodyLine("}");
        method.addBodyLine(// $NON-NLS-1$
        "addCriterion(condition, new java.sql.Date(value1.getTime())," + // $NON-NLS-1$
        " new java.sql.Date(value2.getTime()), property);");
        answer.addMethod(method);
    }
    if (introspectedTable.hasJDBCTimeColumns()) {
        topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance());
        topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance());
        // $NON-NLS-1$
        method = new Method("addCriterionForJDBCTime");
        method.setVisibility(JavaVisibility.PROTECTED);
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), "value"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "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);
        // $NON-NLS-1$
        method = new Method("addCriterionForJDBCTime");
        method.setVisibility(JavaVisibility.PROTECTED);
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(listOfDates, "values"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "property"));
        // $NON-NLS-1$
        method.addBodyLine("if (values == null || values.size() == 0) {");
        method.addBodyLine(// $NON-NLS-1$
        "throw new RuntimeException(\"Value list for \" + property + \"" + // $NON-NLS-1$
        " cannot be null or empty\");");
        // $NON-NLS-1$
        method.addBodyLine("}");
        // $NON-NLS-1$
        method.addBodyLine("List<java.sql.Time> timeList = new ArrayList<>();");
        // $NON-NLS-1$
        method.addBodyLine("Iterator<Date> iter = values.iterator();");
        // $NON-NLS-1$
        method.addBodyLine("while (iter.hasNext()) {");
        // $NON-NLS-1$
        method.addBodyLine("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);
        // $NON-NLS-1$
        method = new Method("addCriterionForJDBCTime");
        method.setVisibility(JavaVisibility.PROTECTED);
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), "value1"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getDateInstance(), "value2"));
        // $NON-NLS-1$
        method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "property"));
        // $NON-NLS-1$
        method.addBodyLine("if (value1 == null || value2 == null) {");
        method.addBodyLine(// $NON-NLS-1$
        "throw new RuntimeException(\"Between values for \" + property + \"" + // $NON-NLS-1$
        " cannot be null\");");
        // $NON-NLS-1$
        method.addBodyLine("}");
        method.addBodyLine(// $NON-NLS-1$
        "addCriterion(condition, new java.sql.Time(value1.getTime())," + // $NON-NLS-1$
        " 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 4 with InnerClass

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

the class ExampleGenerator method getCriteriaInnerClass.

private InnerClass getCriteriaInnerClass() {
    InnerClass answer = new InnerClass(FullyQualifiedJavaType.getCriteriaInstance());
    answer.setVisibility(JavaVisibility.PUBLIC);
    answer.setStatic(true);
    answer.setSuperClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());
    context.getCommentGenerator().addClassComment(answer, introspectedTable, true);
    // $NON-NLS-1$
    Method method = new Method("Criteria");
    method.setVisibility(JavaVisibility.PROTECTED);
    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 5 with InnerClass

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

the class DynamicSqlSupportClassGenerator method generate.

public TopLevelClass generate() {
    TopLevelClass topLevelClass = buildBasicClass();
    Field tableField = calculateTableDefinition(topLevelClass);
    topLevelClass.addImportedType(tableField.getType());
    topLevelClass.addField(tableField);
    InnerClass innerClass = buildInnerTableClass(topLevelClass);
    topLevelClass.addInnerClass(innerClass);
    List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
    for (IntrospectedColumn column : columns) {
        handleColumn(topLevelClass, innerClass, column, tableField.getName());
    }
    return topLevelClass;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) InnerClass(org.mybatis.generator.api.dom.java.InnerClass)

Aggregations

InnerClass (org.mybatis.generator.api.dom.java.InnerClass)9 Method (org.mybatis.generator.api.dom.java.Method)8 Field (org.mybatis.generator.api.dom.java.Field)5 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)5 Parameter (org.mybatis.generator.api.dom.java.Parameter)5 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)4 ArrayList (java.util.ArrayList)2 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)2 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)1