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

The following examples show how to use org.mybatis.generator.api.dom.java.Method#addBodyLine() . 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: UpdateByExampleSelectiveMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    method
            .addBodyLine("UpdateByExampleParms parms = new UpdateByExampleParms(record, example);"); //$NON-NLS-1$

    StringBuilder sb = new StringBuilder();

    sb.append("int rows = "); //$NON-NLS-1$

    sb.append(daoTemplate.getUpdateMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getUpdateByExampleSelectiveStatementId(), "parms")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    method.addBodyLine("return rows;"); //$NON-NLS-1$

    if (context.getPlugins()
            .clientUpdateByExampleSelectiveMethodGenerated(method,
                    topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 2
Source File: PrimaryKeyGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
private void addParameterizedConstructor(TopLevelClass topLevelClass) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName(topLevelClass.getType().getShortName());
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    
    StringBuilder sb = new StringBuilder();
    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getPrimaryKeyColumns()) {
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(),
                introspectedColumn.getJavaProperty()));
        sb.setLength(0);
        sb.append("this."); //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty());
        sb.append(" = "); //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty());
        sb.append(';');
        method.addBodyLine(sb.toString());
    }
    
    topLevelClass.addMethod(method);
}
 
Example 3
Source File: SelectByExampleWithBLOBsMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    if (generateForJava5) {
        method.addSuppressTypeWarningsAnnotation();
    }

    StringBuilder sb = new StringBuilder();
    sb.append(method.getReturnType().getShortName());
    sb.append(" list = "); //$NON-NLS-1$
    sb.append(daoTemplate.getQueryForListMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getSelectByExampleWithBLOBsStatementId(), "example")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return list;"); //$NON-NLS-1$

    if (context.getPlugins()
            .clientSelectByExampleWithBLOBsMethodGenerated(method,
                    topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 4
Source File: DeleteByExampleMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    StringBuilder sb = new StringBuilder();
    sb.append("int rows = "); //$NON-NLS-1$
    sb.append(daoTemplate.getDeleteMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getDeleteByExampleStatementId(), "example")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return rows;"); //$NON-NLS-1$

    if (context.getPlugins().clientDeleteByExampleMethodGenerated(
            method, topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 5
Source File: UpdateControllerGenerator.java    From maven-archetype with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static List<Method> generator(FullyQualifiedJavaType beanType,String beanName,
		String businessFieldName){
	List<Method> methodList = new ArrayList<Method>();
	String beanFiledName = StringUtils.uncapitalize(beanName);
	
	Method method = new Method();
	method.addAnnotation("@RequestMapping(value = \"updateSave" + beanName + ".do\")");
	method.setVisibility(JavaVisibility.PUBLIC);
	method.setReturnType(new FullyQualifiedJavaType(String.class.getName()));
	method.setName("updateSave" + beanName);
	Parameter param1 = new Parameter(beanType, beanFiledName);
	param1.addAnnotation("@ModelAttribute ");
	Parameter paramModelMap = new Parameter(new FullyQualifiedJavaType(ModelMap.class.getName()), "map");
	method.addParameter(param1); 
	method.addParameter(paramModelMap); 
	// 方法body
	method.addBodyLine("try {");
	method.addBodyLine("if(" + beanFiledName + " == null ){");
	method.addBodyLine("// || NumberUtil.isNotPositive(" + beanFiledName + ".getId())){");
	method.addBodyLine("map.put(\"message\",\"更新对象为空。\");");
	method.addBodyLine(ControllerPluginUtil.RETURN);
	method.addBodyLine("}");
	method.addBodyLine("int updateCount = this." + businessFieldName + ".update(" + beanFiledName + ");");
	method.addBodyLine("if(updateCount == 1 ){");
	method.addBodyLine("map.put(\"message\",\"更新成功。\");");
	method.addBodyLine("}else{");
	method.addBodyLine("map.put(\"message\",\"更新失败。\");");
	method.addBodyLine("}");
	method.addBodyLine("} catch (Exception e) {");
	method.addBodyLine("logger.error(\"更新异常\" + e.getMessage());");
	method.addBodyLine("map.put(\"message\", \"查询异常\" + e.getMessage());");
	method.addBodyLine("}");	
	method.addBodyLine(ControllerPluginUtil.RETURN);
			
	methodList.add(method);
       
       return methodList;
}
 
Example 6
Source File: ToStringPlugin.java    From mybatis-generator-core-fix 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 7
Source File: SelectByPrimaryKeyMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public MethodAndImports generateMethodAndImports() {
    if (!introspectedTable.getRules().generateSelectByPrimaryKey()) {
        return null;
    }
    
    Set<FullyQualifiedJavaType> imports = new HashSet<>();
    
    imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.select.SelectDSL"));
    imports.add(recordType);
    
    Method method = new Method("selectByPrimaryKey");
    method.setDefault(true);
    context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
    method.setReturnType(recordType);
    
    StringBuilder sb = new StringBuilder();
    sb.append("return SelectDSL.selectWithMapper(this::selectOne, ");
    sb.append(fragmentGenerator.getSelectList());
    sb.append(')');
    method.addBodyLine(sb.toString());
    
    method.addBodyLine("        .from(" + tableFieldName + ")"); //$NON-NLS-2$
    
    MethodAndImports.Builder builder = MethodAndImports.withMethod(method)
            .withStaticImport("org.mybatis.dynamic.sql.SqlBuilder.*")
            .withImports(imports);
    
    MethodParts methodParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters();
    acceptParts(builder, method, methodParts);
    
    method.addBodyLine("        .build()");
    method.addBodyLine("        .execute();");
    
    return builder.build();
}
 
Example 8
Source File: AbstractDAOTemplate.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
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: InsertMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public MethodAndImports generateMethodAndImports() {
    if (!introspectedTable.getRules().generateInsert()) {
        return null;
    }
    
    Set<FullyQualifiedJavaType> imports = new HashSet<>();

    imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.SqlBuilder"));
    imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.render.RenderingStrategy"));
    imports.add(recordType);
    
    Method method = new Method("insert");
    method.setDefault(true);
    context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.addParameter(new Parameter(recordType, "record"));
    
    method.addBodyLine("return insert(SqlBuilder.insert(record)");
    method.addBodyLine("        .into(" + tableFieldName + ")"); //$NON-NLS-2$
    
    List<IntrospectedColumn> columns =
            ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
    for (IntrospectedColumn column : columns) {
        String fieldName = calculateFieldName(column);
        
        method.addBodyLine("        .map(" + fieldName
                + ").toProperty(\"" + column.getJavaProperty()
                + "\")");
    }
    
    method.addBodyLine("        .build()");
    method.addBodyLine("        .render(RenderingStrategy.MYBATIS3));");
    
    return MethodAndImports.withMethod(method)
            .withImports(imports)
            .build();
}
 
Example 10
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 11
Source File: GenericSIDAOTemplate.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
@Override
protected void configureConstructorTemplate() {
    Method method = new Method();
    method.setConstructor(true);
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addBodyLine("super();"); //$NON-NLS-1$
    setConstructorTemplate(method);
}
 
Example 12
Source File: IbatisDAOTemplate.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
protected void configureConstructorTemplate() {
    Method method = new Method();
    method.setConstructor(true);
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(new Parameter(fqjt, "daoManager")); //$NON-NLS-1$
    method.addBodyLine("super(daoManager);"); //$NON-NLS-1$
    setConstructorTemplate(method);
}
 
Example 13
Source File: ProviderUpdateByExampleWithoutBLOBsMethodGenerator.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.UPDATE");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SET");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }

    importedTypes.add(new FullyQualifiedJavaType("java.util.Map"));

    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>"),
            "parameter"));
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    if (useLegacyBuilder) {
        method.addBodyLine("BEGIN();");
    } else {
        method.addBodyLine("SQL sql = new SQL();");
    }

    method.addBodyLine(String.format("%sUPDATE(\"%s\");",
            builderPrefix,
            escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    method.addBodyLine("");

    for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(getColumns())) {
        StringBuilder sb = new StringBuilder();
        sb.append(getParameterClause(introspectedColumn));
        sb.insert(2, "record.");

        method.addBodyLine(String.format("%sSET(\"%s = %s\");",
                builderPrefix,
                escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)),
                sb.toString()));
    }

    method.addBodyLine("");
    
    FullyQualifiedJavaType example =
            new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(example);
    method.addBodyLine(String.format("%s example = (%s) parameter.get(\"example\");",
            example.getShortName(), example.getShortName()));

    if (useLegacyBuilder) {
        method.addBodyLine("applyWhere(example, true);");
        method.addBodyLine("return SQL();");
    } else {
        method.addBodyLine("applyWhere(sql, example, true);");
        method.addBodyLine("return sql.toString();");
    }

    if (callPlugins(method, topLevelClass)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 14
Source File: InsertSelectiveMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    FullyQualifiedJavaType returnType = method.getReturnType();
    StringBuilder sb = new StringBuilder();

    if (returnType != null) {
        sb.append("Object newKey = "); //$NON-NLS-1$
    }

    sb.append(daoTemplate.getInsertMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getInsertSelectiveStatementId(), "record")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    if (returnType != null) {
        if ("Object".equals(returnType.getShortName())) { //$NON-NLS-1$
            // no need to cast if the return type is Object
            method.addBodyLine("return newKey;"); //$NON-NLS-1$
        } else {
            sb.setLength(0);

            if (returnType.isPrimitive()) {
                PrimitiveTypeWrapper ptw = returnType
                        .getPrimitiveTypeWrapper();
                sb.append("return (("); //$NON-NLS-1$
                sb.append(ptw.getShortName());
                sb.append(") newKey"); //$NON-NLS-1$
                sb.append(")."); //$NON-NLS-1$
                sb.append(ptw.getToPrimitiveMethod());
                sb.append(';');
            } else {
                sb.append("return ("); //$NON-NLS-1$
                sb.append(returnType.getShortName());
                sb.append(") newKey;"); //$NON-NLS-1$
            }

            method.addBodyLine(sb.toString());
        }
    }

    if (context.getPlugins().clientInsertSelectiveMethodGenerated(
            method, topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 15
Source File: SelectListControllerGenerator.java    From maven-archetype with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static List<Method> generator(FullyQualifiedJavaType beanType,String beanName,
		String businessFieldName,String criteriaType){
	List<Method> methodList = new ArrayList<Method>();
	
	Method method = new Method();
       method.addAnnotation("@RequestMapping(value = \"select" + beanName + "List.do\")");
       method.setVisibility(JavaVisibility.PUBLIC);
       method.setReturnType(new FullyQualifiedJavaType(String.class.getName()));
       method.setName("select" + beanName + "List");
       Parameter param1 = new Parameter(new FullyQualifiedJavaType(Short.class.getName()), "conditionType");
       param1.addAnnotation("@RequestParam(\"conditionType\") ");
       Parameter param2 = new Parameter(FullyQualifiedJavaType.getStringInstance(), "conditionValue");
       param2.addAnnotation("@RequestParam(\"conditionValue\") ");
       Parameter param3 = new Parameter(new FullyQualifiedJavaType(PaginationBean.class.getName()), "paginationBean");
       param3.addAnnotation("@ModelAttribute ");
       Parameter paramModelMap = new Parameter(new FullyQualifiedJavaType(ModelMap.class.getName()), "map");
       method.addParameter(param1); 
       method.addParameter(param2); 
       method.addParameter(param3); 
       method.addParameter(paramModelMap); 
       // 方法body
       method.addBodyLine("try {");
       method.addBodyLine("int currentPage = paginationBean.getPageNum();");
       method.addBodyLine("int pageSize = paginationBean.getNumPerPage(); ");
       method.addBodyLine("");
       method.addBodyLine("if(pageSize < 1){");
       method.addBodyLine("map.put(\"message\",\"pageSize 错误。\"); ");
       method.addBodyLine(ControllerPluginUtil.RETURN);
       method.addBodyLine("}");
       method.addBodyLine("if(currentPage<1){");
       method.addBodyLine("map.put(\"message\",\"currentPage 错误。\"); ");
       method.addBodyLine(ControllerPluginUtil.RETURN);
       method.addBodyLine("}");
       method.addBodyLine("// 构造查询参数");
       method.addBodyLine(""+criteriaType + " param =new " + criteriaType + "();");
       method.addBodyLine("//"+criteriaType + ".Criteria criteria = param.createCriteria();");
       method.addBodyLine("");
       method.addBodyLine("// 根据参数设置查询条件");
       method.addBodyLine("");
       method.addBodyLine("// 取得当前查询的总记录结果");
       method.addBodyLine("int total = this." + businessFieldName + ".count" + beanName + "List(param);");
       method.addBodyLine("if(total == 0){");
       method.addBodyLine("// 没有记录数");
       method.addBodyLine("map.put(\"message\",\"没有记录。\"); ");
       method.addBodyLine(ControllerPluginUtil.RETURN);
       method.addBodyLine("}");
       method.addBodyLine("paginationBean.setTotalCount(total);");
       method.addBodyLine("// 判断当前请求的页码有没有超过总页数");
       method.addBodyLine("int totalPages = PaginationUtil.getPages(total, pageSize);");
       method.addBodyLine("paginationBean.setTotalPages(totalPages);");
       method.addBodyLine("");
       method.addBodyLine("if(currentPage > totalPages){");
       method.addBodyLine("// 当前页超过总页数,取最大数");
       method.addBodyLine("currentPage = totalPages;");
       method.addBodyLine("paginationBean.setPageNum(currentPage);");
       method.addBodyLine("}");
       method.addBodyLine("");
       method.addBodyLine("// 设置排序");
       method.addBodyLine("// param.setOrderByClause(\" id asc \");");
       method.addBodyLine("");
       method.addBodyLine("int start = (currentPage - 1) * pageSize;");
       method.addBodyLine("param.setStart(start);");
       method.addBodyLine("param.setCount(pageSize);");
       method.addBodyLine("");
       method.addBodyLine("List<" + beanName + "> " + StringUtils.uncapitalize(beanName) +"List = this." + businessFieldName + ".select" + beanName + "List(param);");
       method.addBodyLine("");
       method.addBodyLine("paginationBean.setResult(" + StringUtils.uncapitalize(beanName) +"List);  // 返回数据结果");
       method.addBodyLine("map.put(\"message\",JSON.toJSONString(" + StringUtils.uncapitalize(beanName) +"List));");
       method.addBodyLine("} catch (Exception e) {");
       method.addBodyLine("logger.error(\"查询异常\" + e.getMessage());");
       method.addBodyLine("map.put(\"message\", \"查询异常\" + e.getMessage());");
       method.addBodyLine("}"); 
       method.addBodyLine(ControllerPluginUtil.RETURN);
       
	methodList.add(method);
       
       return methodList;
}
 
Example 16
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 17
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(Ibatis2FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    if (betweenMethod) {
        sb.append(" between"); //$NON-NLS-1$
    } else {
        sb.append(" not between"); //$NON-NLS-1$
    }
    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("(value1), "); //$NON-NLS-1$
        sb.append("new "); //$NON-NLS-1$
        sb.append(introspectedColumn.getFullyQualifiedJavaType()
                .getPrimitiveTypeWrapper().getShortName());
        sb.append("(value2)"); //$NON-NLS-1$
    } else {
        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 18
Source File: InsertMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    FullyQualifiedJavaType returnType = method.getReturnType();

    StringBuilder sb = new StringBuilder();

    if (returnType != null) {
        sb.append("Object newKey = "); //$NON-NLS-1$
    }

    sb.append(daoTemplate.getInsertMethod(introspectedTable
            .getIbatis2SqlMapNamespace(), introspectedTable
            .getInsertStatementId(), "record")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    if (returnType != null) {
        if ("Object".equals(returnType.getShortName())) { //$NON-NLS-1$
            // no need to cast if the return type is Object
            method.addBodyLine("return newKey;"); //$NON-NLS-1$
        } else {
            sb.setLength(0);

            if (returnType.isPrimitive()) {
                PrimitiveTypeWrapper ptw = returnType
                        .getPrimitiveTypeWrapper();
                sb.append("return (("); //$NON-NLS-1$
                sb.append(ptw.getShortName());
                sb.append(") newKey"); //$NON-NLS-1$
                sb.append(")."); //$NON-NLS-1$
                sb.append(ptw.getToPrimitiveMethod());
                sb.append(';');
            } else {
                sb.append("return ("); //$NON-NLS-1$
                sb.append(returnType.getShortName());
                sb.append(") newKey;"); //$NON-NLS-1$
            }

            method.addBodyLine(sb.toString());
        }
    }

    if (context.getPlugins().clientInsertMethodGenerated(method,
            topLevelClass, introspectedTable)) {
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 19
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 20
Source File: ExampleGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param introspectedColumn
 * @param inMethod
 *            if true generates an "in" method, else generates a "not in"
 *            method
 * @return a generated method for the in or not in method
 */
private Method getSetInOrNotInMethod(IntrospectedColumn introspectedColumn,
        boolean inMethod) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType type = FullyQualifiedJavaType
            .getNewListInstance();
    if (generateForJava5) {
        if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            type.addTypeArgument(introspectedColumn
                    .getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
        } else {
            type.addTypeArgument(introspectedColumn
                    .getFullyQualifiedJavaType());
        }
    }

    method.addParameter(new Parameter(type, "values")); //$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 (inMethod) {
        sb.append("In"); //$NON-NLS-1$
    } else {
        sb.append("NotIn"); //$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(Ibatis2FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    if (inMethod) {
        sb.append(" in"); //$NON-NLS-1$
    } else {
        sb.append(" not in"); //$NON-NLS-1$
    }
    sb.append("\", values, \""); //$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;
}