Java Code Examples for org.mybatis.generator.api.dom.java.Method#setVisibility()

The following examples show how to use org.mybatis.generator.api.dom.java.Method#setVisibility() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaBeansUtil.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private static Method getBasicJavaBeansGetter(IntrospectedColumn introspectedColumn) {
    FullyQualifiedJavaType fqjt = introspectedColumn
            .getFullyQualifiedJavaType();
    String property = introspectedColumn.getJavaProperty();

    Method method = new Method(getGetterMethodName(property, fqjt));
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(fqjt);

    StringBuilder sb = new StringBuilder();
    sb.append("return ");
    sb.append(property);
    sb.append(';');
    method.addBodyLine(sb.toString());

    return method;
}
 
Example 2
Source File: ExampleGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
private Method getNoValueMethod(IntrospectedColumn introspectedColumn,
        String nameFragment, String operator) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); //$NON-NLS-1$
    sb.append(nameFragment);
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);
    sb.append("addCriterion(\""); //$NON-NLS-1$
    sb.append(Ibatis2FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    sb.append(' ');
    sb.append(operator);
    sb.append("\");"); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$

    return method;
}
 
Example 3
Source File: CountByExampleMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(
            introspectedTable.getExampleType());
    importedTypes.add(type);

    Method method = new Method();
    method.setVisibility(getExampleMethodVisibility());
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setName(getDAOMethodNameCalculator()
            .getCountByExampleMethodName(introspectedTable));
    method.addParameter(new Parameter(type, "example")); //$NON-NLS-1$

    for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
        method.addException(fqjt);
        importedTypes.add(fqjt);
    }

    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    return method;
}
 
Example 4
Source File: GetByKeyBusinessGenerator.java    From maven-archetype with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static List<Method> generator(FullyQualifiedJavaType beanType,String beanName,
		String mapperFieldName,FullyQualifiedJavaType keyType){
	List<Method> methodList = new ArrayList<Method>();
	
	// add get by key method
	Method method = new Method();
       method.setVisibility(JavaVisibility.PUBLIC);
       method.setReturnType(beanType);
       method.setName("get" + beanName + "ByKey"); //$NON-NLS-1$
       method.addParameter(new Parameter(keyType, "key")); //$NON-NLS-1$
       method.addException(new FullyQualifiedJavaType(Exception.class.getName()));
       method.addBodyLine("return this."+ mapperFieldName +".selectByPrimaryKey(key);"); 
       methodList.add(method);
       
       // add new method here
       
       return methodList;
}
 
Example 5
Source File: ExampleGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
private Method getNoValueMethod(IntrospectedColumn introspectedColumn,
        String nameFragment, String operator) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); //$NON-NLS-1$
    sb.append(nameFragment);
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);
    sb.append("addCriterion(\""); //$NON-NLS-1$
    sb.append(MyBatis3FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    sb.append(' ');
    sb.append(operator);
    sb.append("\");"); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$

    return method;
}
 
Example 6
Source File: DeleteByExampleMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
@Override
public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(
            introspectedTable.getExampleType());
    importedTypes.add(type);

    Method method = new Method(introspectedTable.getDeleteByExampleStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setAbstract(true);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.addParameter(new Parameter(type, "example"));

    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    addMapperAnnotations(method);
    
    if (context.getPlugins().clientDeleteByExampleMethodGenerated(
            method, interfaze, introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);
    }
}
 
Example 7
Source File: JavaBeansUtil.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the java beans setter.
 *
 * @param introspectedColumn
 *            the introspected column
 * @param context
 *            the context
 * @param introspectedTable
 *            the introspected table
 * @return the java beans setter
 */
public static Method getJavaBeansSetter(IntrospectedColumn introspectedColumn,
        Context context,
        IntrospectedTable introspectedTable) {
    FullyQualifiedJavaType fqjt = introspectedColumn
            .getFullyQualifiedJavaType();
    String property = introspectedColumn.getJavaProperty();

    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName(getSetterMethodName(property));
    method.addParameter(new Parameter(fqjt, property));
    context.getCommentGenerator().addSetterComment(method,
            introspectedTable, introspectedColumn);

    StringBuilder sb = new StringBuilder();
    if (isTrimStringsEnabled(context) && introspectedColumn.isStringColumn()) {
        sb.append("this."); //$NON-NLS-1$
        sb.append(property);
        sb.append(" = "); //$NON-NLS-1$
        sb.append(property);
        sb.append(" == null ? null : "); //$NON-NLS-1$
        sb.append(property);
        sb.append(".trim();"); //$NON-NLS-1$
        method.addBodyLine(sb.toString());
    } else {
        sb.append("this."); //$NON-NLS-1$
        sb.append(property);
        sb.append(" = "); //$NON-NLS-1$
        sb.append(property);
        sb.append(';');
        method.addBodyLine(sb.toString());
    }

    return method;
}
 
Example 8
Source File: AbstractDAOTemplate.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the method clones.
 *
 * @param commentGenerator
 *            the comment generator
 * @param introspectedTable
 *            the introspected table
 * @return the method clones
 */
public final List<Method> getMethodClones(
        CommentGenerator commentGenerator,
        IntrospectedTable introspectedTable) {
    configure();
    List<Method> answer = new ArrayList<Method>();
    for (Method oldMethod : methods) {
        Method method = new Method();

        for (String bodyLine : oldMethod.getBodyLines()) {
            method.addBodyLine(bodyLine);
        }

        for (FullyQualifiedJavaType fqjt : oldMethod.getExceptions()) {
            method.addException(fqjt);
        }

        for (Parameter parm : oldMethod.getParameters()) {
            method.addParameter(parm);
        }

        method.setConstructor(oldMethod.isConstructor());
        method.setFinal(oldMethod.isFinal());
        method.setStatic(oldMethod.isStatic());
        method.setName(oldMethod.getName());
        method.setReturnType(oldMethod.getReturnType());
        method.setVisibility(oldMethod.getVisibility());

        commentGenerator.addGeneralMethodComment(method, introspectedTable);

        answer.add(method);
    }

    return answer;
}
 
Example 9
Source File: AbstractJavaGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
public static Method getGetter(Field field) {
    Method method = new Method();
    method.setName(getGetterMethodName(field.getName(), field
            .getType()));
    method.setReturnType(field.getType());
    method.setVisibility(JavaVisibility.PUBLIC);
    StringBuilder sb = new StringBuilder();
    sb.append("return "); //$NON-NLS-1$
    sb.append(field.getName());
    sb.append(';');
    method.addBodyLine(sb.toString());
    return method;
}
 
Example 10
Source File: UpdateByExampleSelectiveMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addInterfaceElements(Interface interfaze) {
    Method method = new Method(introspectedTable
            .getUpdateByExampleSelectiveStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setAbstract(true);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());

    FullyQualifiedJavaType parameterType =
            introspectedTable.getRules().calculateAllFieldsClass();
    method.addParameter(new Parameter(parameterType,
            "record", "@Param(\"record\")")); //$NON-NLS-2$

    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
    importedTypes.add(parameterType);

    FullyQualifiedJavaType exampleType = new FullyQualifiedJavaType(
            introspectedTable.getExampleType());
    method.addParameter(new Parameter(exampleType,
            "example", "@Param(\"example\")")); //$NON-NLS-2$
    importedTypes.add(exampleType);

    importedTypes.add(new FullyQualifiedJavaType(
            "org.apache.ibatis.annotations.Param"));

    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    addMapperAnnotations(method);
    
    if (context.getPlugins()
            .clientUpdateByExampleSelectiveMethodGenerated(method, interfaze,
                    introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);
    }
}
 
Example 11
Source File: AbstractJavaGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public Method getJavaBeansSetter(IntrospectedColumn introspectedColumn) {
    FullyQualifiedJavaType fqjt = introspectedColumn
            .getFullyQualifiedJavaType();
    String property = introspectedColumn.getJavaProperty();

    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName(getSetterMethodName(property));
    method.addParameter(new Parameter(fqjt, property));
    context.getCommentGenerator().addSetterComment(method,
            introspectedTable, introspectedColumn);

    StringBuilder sb = new StringBuilder();
    if (isTrimStringsEnabled() && introspectedColumn.isStringColumn()) {
        sb.append("this."); //$NON-NLS-1$
        sb.append(property);
        sb.append(" = "); //$NON-NLS-1$
        sb.append(property);
        sb.append(" == null ? null : "); //$NON-NLS-1$
        sb.append(property);
        sb.append(".trim();"); //$NON-NLS-1$
        method.addBodyLine(sb.toString());
    } else {
        sb.append("this."); //$NON-NLS-1$
        sb.append(property);
        sb.append(" = "); //$NON-NLS-1$
        sb.append(property);
        sb.append(';');
        method.addBodyLine(sb.toString());
    }

    return method;
}
 
Example 12
Source File: FluentBuilderMethodsPlugin.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public boolean modelSetterMethodGenerated(Method method,
        TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
        IntrospectedTable introspectedTable,
        ModelClassType modelClassType) {
    
    Method fluentMethod = new Method("with" + method.getName().substring(3));
    fluentMethod.setVisibility(JavaVisibility.PUBLIC);
    fluentMethod.setReturnType(topLevelClass.getType());
    fluentMethod.getParameters().addAll(method.getParameters());
     
    if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3_DSQL) {
        context.getCommentGenerator().addGeneralMethodAnnotation(fluentMethod,
                introspectedTable, topLevelClass.getImportedTypes());
    } else {
        context.getCommentGenerator().addGeneralMethodComment(fluentMethod,
                introspectedTable);
    }
    
    StringBuilder sb = new StringBuilder()
            .append("this.")
            .append(method.getName())
            .append('(')
            .append(introspectedColumn.getJavaProperty())
            .append(");");
    fluentMethod.addBodyLine(sb.toString());
    fluentMethod.addBodyLine("return this;");

    topLevelClass.addMethod(fluentMethod);

    return super.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn,
            introspectedTable, modelClassType);
}
 
Example 13
Source File: ToStringPlugin.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
private void generateToString(IntrospectedTable introspectedTable,
        TopLevelClass topLevelClass) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.setName("toString"); //$NON-NLS-1$
    if (introspectedTable.isJava5Targeted()) {
        method.addAnnotation("@Override"); //$NON-NLS-1$
    }

    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    method.addBodyLine("StringBuilder sb = new StringBuilder();"); //$NON-NLS-1$
    method.addBodyLine("sb.append(getClass().getSimpleName());"); //$NON-NLS-1$
    method.addBodyLine("sb.append(\" [\");"); //$NON-NLS-1$
    method.addBodyLine("sb.append(\"Hash = \").append(hashCode());"); //$NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    for (Field field : topLevelClass.getFields()) {
        String property = field.getName();
        sb.setLength(0);
        sb.append("sb.append(\"").append(", ").append(property) //$NON-NLS-1$ //$NON-NLS-2$
                .append("=\")").append(".append(").append(property) //$NON-NLS-1$ //$NON-NLS-2$
                .append(");"); //$NON-NLS-1$
        method.addBodyLine(sb.toString());
    }

    method.addBodyLine("sb.append(\"]\");"); //$NON-NLS-1$
    method.addBodyLine("return sb.toString();"); //$NON-NLS-1$

    topLevelClass.addMethod(method);
}
 
Example 14
Source File: ExampleGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
/**
 * Generates methods that set between and not between conditions
 * 
 * @param introspectedColumn
 * @param betweenMethod
 * @return a generated method for the between or not between method
 */
private Method getSetBetweenOrNotBetweenMethod(
        IntrospectedColumn introspectedColumn, boolean betweenMethod) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType type = introspectedColumn
            .getFullyQualifiedJavaType();

    method.addParameter(new Parameter(type, "value1")); //$NON-NLS-1$
    method.addParameter(new Parameter(type, "value2")); //$NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); //$NON-NLS-1$
    if (betweenMethod) {
        sb.append("Between"); //$NON-NLS-1$
    } else {
        sb.append("NotBetween"); //$NON-NLS-1$
    }
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);

    if (introspectedColumn.isJDBCDateColumn()) {
        sb.append("addCriterionForJDBCDate(\""); //$NON-NLS-1$
    } else if (introspectedColumn.isJDBCTimeColumn()) {
        sb.append("addCriterionForJDBCTime(\""); //$NON-NLS-1$
    } else if (stringHasValue(introspectedColumn
            .getTypeHandler())) {
        sb.append("add"); //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty());
        sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
        sb.append("Criterion(\""); //$NON-NLS-1$
    } else {
        sb.append("addCriterion(\""); //$NON-NLS-1$
    }

    sb.append(MyBatis3FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    if (betweenMethod) {
        sb.append(" between"); //$NON-NLS-1$
    } else {
        sb.append(" not between"); //$NON-NLS-1$
    }
    sb.append("\", "); //$NON-NLS-1$
    sb.append("value1, value2"); //$NON-NLS-1$
    sb.append(", \""); //$NON-NLS-1$
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\");"); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$

    return method;
}
 
Example 15
Source File: ProviderCountByExampleMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();

    if (useLegacyBuilder) {
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.FROM");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }

    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(fqjt);

    Method method = new Method(
            introspectedTable.getCountByExampleStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addParameter(new Parameter(fqjt, "example"));
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    if (useLegacyBuilder) {
        method.addBodyLine("BEGIN();");
        method.addBodyLine("SELECT(\"count(*)\");");
        method.addBodyLine(String.format("FROM(\"%s\");",
                escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
        method.addBodyLine("applyWhere(example, false);");
        method.addBodyLine("return SQL();");
    } else {
        method.addBodyLine("SQL sql = new SQL();");
        method.addBodyLine(String.format("sql.SELECT(\"count(*)\").FROM(\"%s\");",
                escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
        method.addBodyLine("applyWhere(sql, example, false);");
        method.addBodyLine("return sql.toString();");
    }
    
    if (context.getPlugins().providerCountByExampleMethodGenerated(method, topLevelClass,
            introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 16
Source File: ProviderSelectByExampleWithoutBLOBsMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.FROM"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
    
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(fqjt);

    Method method = new Method(getMethodName());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);
    
    method.addBodyLine("BEGIN();"); //$NON-NLS-1$

    boolean distinctCheck = true;
    for (IntrospectedColumn introspectedColumn : getColumns()) {
        if (distinctCheck) {
            method.addBodyLine("if (example != null && example.isDistinct()) {"); //$NON-NLS-1$
            method.addBodyLine(String.format("SELECT_DISTINCT(\"%s\");", //$NON-NLS-1$
                escapeStringForJava(getSelectListPhrase(introspectedColumn))));
            method.addBodyLine("} else {"); //$NON-NLS-1$
            method.addBodyLine(String.format("SELECT(\"%s\");", //$NON-NLS-1$
                escapeStringForJava(getSelectListPhrase(introspectedColumn))));
            method.addBodyLine("}"); //$NON-NLS-1$
        } else {
            method.addBodyLine(String.format("SELECT(\"%s\");", //$NON-NLS-1$
                escapeStringForJava(getSelectListPhrase(introspectedColumn))));
        }
        
        distinctCheck = false;
    }

    method.addBodyLine(String.format("FROM(\"%s\");", //$NON-NLS-1$
            escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    method.addBodyLine("applyWhere(example, false);"); //$NON-NLS-1$
    
    method.addBodyLine(""); //$NON-NLS-1$
    method.addBodyLine("if (example != null && example.getOrderByClause() != null) {"); //$NON-NLS-1$
    method.addBodyLine("ORDER_BY(example.getOrderByClause());"); //$NON-NLS-1$
    method.addBodyLine("}"); //$NON-NLS-1$
    
    method.addBodyLine(""); //$NON-NLS-1$
    method.addBodyLine("return SQL();"); //$NON-NLS-1$
    
    if (callPlugins(method, topLevelClass)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 17
Source File: ProviderDeleteByExampleMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();

    if (useLegacyBuilder) {
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }

    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(fqjt);

    Method method = new Method(
            introspectedTable.getDeleteByExampleStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addParameter(new Parameter(fqjt, "example"));
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    if (useLegacyBuilder) {
        method.addBodyLine("BEGIN();");
        method.addBodyLine(String.format("DELETE_FROM(\"%s\");",
                escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
        method.addBodyLine("applyWhere(example, false);");
        method.addBodyLine("return SQL();");
    } else {
        method.addBodyLine("SQL sql = new SQL();");
        method.addBodyLine(String.format("sql.DELETE_FROM(\"%s\");",
                escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
        method.addBodyLine("applyWhere(sql, example, false);");
        method.addBodyLine("return sql.toString();");
    }
    
    if (context.getPlugins().providerDeleteByExampleMethodGenerated(method, topLevelClass,
            introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 18
Source File: ExampleGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
private Method getSingleValueMethod(IntrospectedColumn introspectedColumn,
        String nameFragment, String operator) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(new Parameter(introspectedColumn
            .getFullyQualifiedJavaType(), "value")); //$NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); //$NON-NLS-1$
    sb.append(nameFragment);
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);

    if (introspectedColumn.isJDBCDateColumn()) {
        sb.append("addCriterionForJDBCDate(\""); //$NON-NLS-1$
    } else if (introspectedColumn.isJDBCTimeColumn()) {
        sb.append("addCriterionForJDBCTime(\""); //$NON-NLS-1$
    } else if (stringHasValue(introspectedColumn
            .getTypeHandler())) {
        sb.append("add"); //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty());
        sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
        sb.append("Criterion(\""); //$NON-NLS-1$
    } else {
        sb.append("addCriterion(\""); //$NON-NLS-1$
    }

    sb.append(Ibatis2FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    sb.append(' ');
    sb.append(operator);
    sb.append("\", "); //$NON-NLS-1$

    if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive() && !introspectedTable.isJava5Targeted()) {
        sb.append("new "); //$NON-NLS-1$
        sb.append(introspectedColumn.getFullyQualifiedJavaType()
                .getPrimitiveTypeWrapper().getShortName());
        sb.append("(value)"); //$NON-NLS-1$
    } else {
        sb.append("value"); //$NON-NLS-1$
    }

    sb.append(", \""); //$NON-NLS-1$
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\");"); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$

    return method;
}
 
Example 19
Source File: ProviderUpdateByExampleWithoutBLOBsMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();

    if (useLegacyBuilder) {
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.UPDATE"); //$NON-NLS-1$
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SET"); //$NON-NLS-1$
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
    } else {
    	importedTypes.add(NEW_BUILDER_IMPORT);
    }

    importedTypes.add(new FullyQualifiedJavaType("java.util.Map")); //$NON-NLS-1$
    
    Method method = new Method(getMethodName());
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(new Parameter(new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
            "parameter")); //$NON-NLS-1$
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    if (useLegacyBuilder) {
    	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
    } else {
    	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
    }
    
    method.addBodyLine(String.format("%sUPDATE(\"%s\");", //$NON-NLS-1$
            builderPrefix,
    		escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    method.addBodyLine(""); //$NON-NLS-1$
    
    for (IntrospectedColumn introspectedColumn : getColumns()) {
        StringBuilder sb = new StringBuilder();
        sb.append(getParameterClause(introspectedColumn));
        sb.insert(2, "record."); //$NON-NLS-1$
        
        method.addBodyLine(String.format("%sSET(\"%s = %s\");", //$NON-NLS-1$
                builderPrefix,
        		escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)),
                sb.toString()));
    }
    
    method.addBodyLine(""); //$NON-NLS-1$
    
    FullyQualifiedJavaType example =
        new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(example);
    method.addBodyLine(String.format("%s example = (%s) parameter.get(\"example\");", //$NON-NLS-1$
            example.getShortName(), example.getShortName()));
    
    if (useLegacyBuilder) {
    	method.addBodyLine("applyWhere(example, true);"); //$NON-NLS-1$
    	method.addBodyLine("return SQL();"); //$NON-NLS-1$
    } else {
    	method.addBodyLine("applyWhere(sql, example, true);"); //$NON-NLS-1$
    	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
    }
    
    if (callPlugins(method, topLevelClass)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 20
Source File: ExampleGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
/**
 * Generates methods that set between and not between conditions
 * 
 * @param introspectedColumn
 * @param betweenMethod
 * @return a generated method for the between or not between method
 */
private Method getSetBetweenOrNotBetweenMethod(
        IntrospectedColumn introspectedColumn, boolean betweenMethod) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType type = introspectedColumn
            .getFullyQualifiedJavaType();

    method.addParameter(new Parameter(type, "value1")); //$NON-NLS-1$
    method.addParameter(new Parameter(type, "value2")); //$NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); //$NON-NLS-1$
    if (betweenMethod) {
        sb.append("Between"); //$NON-NLS-1$
    } else {
        sb.append("NotBetween"); //$NON-NLS-1$
    }
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);

    if (introspectedColumn.isJDBCDateColumn()) {
        sb.append("addCriterionForJDBCDate(\""); //$NON-NLS-1$
    } else if (introspectedColumn.isJDBCTimeColumn()) {
        sb.append("addCriterionForJDBCTime(\""); //$NON-NLS-1$
    } else if (stringHasValue(introspectedColumn
            .getTypeHandler())) {
        sb.append("add"); //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty());
        sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
        sb.append("Criterion(\""); //$NON-NLS-1$
    } else {
        sb.append("addCriterion(\""); //$NON-NLS-1$
    }

    sb.append(MyBatis3FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    if (betweenMethod) {
        sb.append(" between"); //$NON-NLS-1$
    } else {
        sb.append(" not between"); //$NON-NLS-1$
    }
    sb.append("\", "); //$NON-NLS-1$
    sb.append("value1, value2"); //$NON-NLS-1$
    sb.append(", \""); //$NON-NLS-1$
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\");"); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$

    return method;
}