Search in sources :

Example 6 with Field

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

the class BaseRecordGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.8", //$NON-NLS-1$
    table.toString()));
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();
    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable.getBaseRecordType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    FullyQualifiedJavaType superClass = getSuperClass();
    if (superClass != null) {
        topLevelClass.setSuperClass(superClass);
        topLevelClass.addImportedType(superClass);
    }
    List<IntrospectedColumn> introspectedColumns;
    if (includePrimaryKeyColumns()) {
        if (includeBLOBColumns()) {
            introspectedColumns = introspectedTable.getAllColumns();
        } else {
            introspectedColumns = introspectedTable.getNonBLOBColumns();
        }
    } else {
        if (includeBLOBColumns()) {
            introspectedColumns = introspectedTable.getNonPrimaryKeyColumns();
        } else {
            introspectedColumns = introspectedTable.getBaseColumns();
        }
    }
    String rootClass = getRootClass();
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }
        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }
        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addMethod(method);
        }
        method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addMethod(method);
        }
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelBaseRecordClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) 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) Field(org.mybatis.generator.api.dom.java.Field) JavaBeansUtil.getJavaBeansField(org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) Plugin(org.mybatis.generator.api.Plugin)

Example 7 with Field

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

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

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

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

Aggregations

Field (org.mybatis.generator.api.dom.java.Field)25 Method (org.mybatis.generator.api.dom.java.Method)20 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)19 ArrayList (java.util.ArrayList)14 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)13 Parameter (org.mybatis.generator.api.dom.java.Parameter)11 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 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)9 Plugin (org.mybatis.generator.api.Plugin)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)4 Rules (org.mybatis.generator.internal.rules.Rules)1