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

The following examples show how to use org.mybatis.generator.api.dom.java.Method#addParameter() . 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: DeleteByExampleMethodGenerator.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()
            .getDeleteByExampleMethodName(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 2
Source File: RowBoundsPlugin.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private void addNewComposedFunction(Interface interfaze, IntrospectedTable introspectedTable,
        Optional<FullyQualifiedJavaType> baseMethodReturnType) {
    if (!baseMethodReturnType.isPresent()) {
        // shouldn't happen, but just in case...
        return;
    }
    
    interfaze.addImportedType(new FullyQualifiedJavaType("java.util.function.Function"));
    
    FullyQualifiedJavaType returnType =
            new FullyQualifiedJavaType("Function<SelectStatementProvider, "
                    + baseMethodReturnType.get().getShortName() + ">");
    
    Method method = new Method("selectManyWithRowbounds");
    method.setDefault(true);
    method.setReturnType(returnType);
    method.addParameter(new Parameter(rowBounds, "rowBounds"));
    method.addBodyLine(
            "return selectStatement -> selectManyWithRowbounds(selectStatement, rowBounds);");
    context.getCommentGenerator().addGeneralMethodAnnotation(
            method, introspectedTable, interfaze.getImportedTypes());
    interfaze.addMethod(method);
}
 
Example 3
Source File: DeleteByExampleMethodGenerator.java    From mybatis-generator-plus 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()
            .getDeleteByExampleMethodName(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: CreateGenericInterfacePlugin.java    From mybatis-generator-plugins with Apache License 2.0 6 votes vote down vote up
void addGenericMethod(Method method, FullyQualifiedJavaType returnType, FullyQualifiedJavaType... types) {
	method.addAnnotation("@Override");

	if (!methodsAdded.contains(method.getName())) {
		Method genericMethod = new Method(method.getName());
		genericMethod.addJavaDocLine("/**");
		genericMethod.addJavaDocLine(" * This method was generated by MyBatis Generator.");
		genericMethod.addJavaDocLine(" *");
		genericMethod.addJavaDocLine(" * @mbg.generated");
		genericMethod.addJavaDocLine(" */");

		genericMethod.setReturnType(returnType);

		for (int i = 0; i < method.getParameters().size(); i++) {
			Parameter parameter = method.getParameters().get(i);
			FullyQualifiedJavaType paramType = types.length > i ? types[i] : parameter.getType();

			Parameter genericParameter = new Parameter(paramType, parameter.getName());
			genericMethod.addParameter(genericParameter);
		}

		genericInterface.addMethod(genericMethod);

		methodsAdded.add(method.getName());
	}
}
 
Example 5
Source File: CountByExampleMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void addInterfaceElements(Interface interfaze) {
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(
            introspectedTable.getExampleType());

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

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

    addMapperAnnotations(interfaze, method);
    
    if (context.getPlugins().clientCountByExampleMethodGenerated(method,
            interfaze, introspectedTable)) {
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);
    }
}
 
Example 6
Source File: SelectListBusinessGenerator.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 paramType){
	List<Method> methodList = new ArrayList<Method>();
	
	// add get by key method
	Method method = new Method();
       method.setVisibility(JavaVisibility.PUBLIC);
       method.setReturnType(new FullyQualifiedJavaType("java.util.List<"+ beanName +">"));
       method.setName("select" + beanName + "List"); //$NON-NLS-1$
       method.addParameter(new Parameter(paramType, "param")); //$NON-NLS-1$
       method.addException(new FullyQualifiedJavaType(Exception.class.getName()));
       method.addBodyLine("return this."+ mapperFieldName +".selectByExample(param);");
       
       methodList.add(method);
       // add new method here
       
       return methodList;
}
 
Example 7
Source File: ProviderDeleteByExampleMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 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.DELETE_FROM"); //$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(
            introspectedTable.getDeleteByExampleStatementId());
    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$
    method.addBodyLine(String.format("DELETE_FROM(\"%s\");", //$NON-NLS-1$
            escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    method.addBodyLine("applyWhere(example, false);"); //$NON-NLS-1$
    method.addBodyLine("return SQL();"); //$NON-NLS-1$
    
    
    if (context.getPlugins().providerDeleteByExampleMethodGenerated(method, topLevelClass,
            introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 8
Source File: RowBoundsPlugin.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
/**
 * Use the method copy constructor to create a new method, then
 * add the rowBounds parameter.
 * 
 * @param fullyQualifiedTable the table
 * @param method the method
 */
private void copyAndAddMethod(Method method, Interface interfaze) {
    Method newMethod = new Method(method);
    newMethod.setName(method.getName() + "WithRowbounds");
    newMethod.addParameter(new Parameter(rowBounds, "rowBounds"));
    interfaze.addMethod(newMethod);
    interfaze.addImportedType(rowBounds);
}
 
Example 9
Source File: AbstractUpsertPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

/*-----------------------------添加单个upsert的接口方法-----------------------------------*/
  Method upsert = new Method(UPSERT);
  upsert.setReturnType(FullyQualifiedJavaType.getIntInstance());

  Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();

  FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
  upsert.addParameter(new Parameter(parameterType, "record", "@Param(\"record\")"));
  importedTypes.add(parameterType);

  FullyQualifiedJavaType arrayType = new FullyQualifiedJavaType((new String[]{}).getClass().getCanonicalName());
  upsert.addParameter(new Parameter(arrayType, "array", "@Param(\"array\")"));
  importedTypes.add(arrayType);

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

  interfaze.addMethod(upsert);

  /*-----------------------------添加单个upsertSelective的接口方法-----------------------------------*/
  Method upsertSelective = new Method(UPSERT_SELECTIVE);
  upsertSelective.setReturnType(FullyQualifiedJavaType.getIntInstance());
  upsertSelective.addParameter(new Parameter(parameterType, "record", "@Param(\"record\")"));
  upsertSelective.addParameter(new Parameter(arrayType, "array", "@Param(\"array\")"));

  interfaze.addMethod(upsertSelective);

  interfaze.addImportedTypes(importedTypes);
  return true;
}
 
Example 10
Source File: DeleteByPrimaryKeyMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public MethodAndImports generateMethodAndImports() {
    if (!introspectedTable.getRules().generateDeleteByPrimaryKey()) {
        return null;
    }

    Set<FullyQualifiedJavaType> imports = new HashSet<>();
    Set<String> staticImports = new HashSet<>();
    
    imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.delete.DeleteDSL"));
    staticImports.add("org.mybatis.dynamic.sql.SqlBuilder.*");
    
    Method method = new Method("deleteByPrimaryKey");
    method.setDefault(true);
    context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    
    method.addBodyLine("return DeleteDSL.deleteFromWithMapper(this::delete, "
            + tableFieldName + ")");
    
    MethodParts methodParts = fragmentGenerator.getPrimaryKeyWhereClauseAndParameters();
    for (Parameter parameter : methodParts.getParameters()) {
        method.addParameter(parameter);
    }
    method.addBodyLines(methodParts.getBodyLines());
    imports.addAll(methodParts.getImports());
    
    method.addBodyLine("        .build()");
    method.addBodyLine("        .execute();");
    
    return MethodAndImports.withMethod(method)
            .withImports(imports)
            .withStaticImports(staticImports)
            .build();
}
 
Example 11
Source File: GetByKeyControllerGenerator.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,FullyQualifiedJavaType keyType, String keyFiledName){
	List<Method> methodList = new ArrayList<Method>();
	
	Method method = new Method();
       method.addAnnotation("@RequestMapping(value = \"get" + beanName + "ByKey.do\")");
       method.addAnnotation("@ResponseBody");
       method.setVisibility(JavaVisibility.PUBLIC);
       method.setReturnType(new FullyQualifiedJavaType(AjaxResponseBean.class.getName()));
       method.setName("get" + beanName + "ByKey");
       Parameter param = new Parameter(keyType, keyFiledName);
       param.addAnnotation("@RequestParam(\"" + keyFiledName + "\") ");
       method.addParameter(param); 
       // 方法body
       method.addBodyLine("try {");
       method.addBodyLine("if(" + keyFiledName + " == null || " + keyFiledName + " < 0){");
       method.addBodyLine("return AjaxResponseBean.Const.PARAMETER_INVALID_ERROR_RESPONSE_BEAN; ");
       method.addBodyLine("}");
       method.addBodyLine(beanName + " result = this."+ businessFieldName +".get"+ beanName +"ByKey(" + keyFiledName + ");");
       method.addBodyLine("return AjaxResponseBean.getReturnValueResponseBean(result);");
       method.addBodyLine("} catch (Exception e) {");
       method.addBodyLine("logger.error(\"主键获取详情异常;key=\"+" + keyFiledName + " + e.getMessage());");
       method.addBodyLine("return AjaxResponseBean.getErrorResponseBean(\"主键获取详情异常;key=\"+" + keyFiledName + " + e.getMessage());");
       method.addBodyLine("}");
	
	methodList.add(method);
       
       return methodList;
}
 
Example 12
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 13
Source File: ProviderSelectByExampleWithoutBLOBsMethodGenerator.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.SELECT");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.FROM");
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY");
        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(getMethodName());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addParameter(new Parameter(fqjt, "example"));
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);
    
    if (useLegacyBuilder) {
        method.addBodyLine("BEGIN();");
    } else {
        method.addBodyLine("SQL sql = new SQL();");
    }

    boolean distinctCheck = true;
    for (IntrospectedColumn introspectedColumn : getColumns()) {
        if (distinctCheck) {
            method.addBodyLine("if (example != null && example.isDistinct()) {");
            method.addBodyLine(String.format("%sSELECT_DISTINCT(\"%s\");",
                    builderPrefix,
                    escapeStringForJava(getSelectListPhrase(introspectedColumn))));
            method.addBodyLine("} else {");
            method.addBodyLine(String.format("%sSELECT(\"%s\");",
                    builderPrefix,
                    escapeStringForJava(getSelectListPhrase(introspectedColumn))));
            method.addBodyLine("}");
        } else {
            method.addBodyLine(String.format("%sSELECT(\"%s\");",
                    builderPrefix,
                    escapeStringForJava(getSelectListPhrase(introspectedColumn))));
        }

        distinctCheck = false;
    }

    method.addBodyLine(String.format("%sFROM(\"%s\");",
            builderPrefix,
            escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    if (useLegacyBuilder) {
        method.addBodyLine("applyWhere(example, false);");
    } else {
        method.addBodyLine("applyWhere(sql, example, false);");
    }

    method.addBodyLine("");
    method.addBodyLine("if (example != null && example.getOrderByClause() != null) {");
    method.addBodyLine(String.format("%sORDER_BY(example.getOrderByClause());", builderPrefix));
    method.addBodyLine("}");

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

    if (callPlugins(method, topLevelClass)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 14
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(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 15
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 16
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.addAnnotation("@ResponseBody");
       method.setVisibility(JavaVisibility.PUBLIC);
       method.setReturnType(new FullyQualifiedJavaType(AjaxResponseBean.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 ");
       method.addParameter(param1); 
       method.addParameter(param2); 
       method.addParameter(param3); 
       // 方法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("return AjaxResponseBean.Const.PARAMETER_INVALID_ERROR_RESPONSE_BEAN; ");
       method.addBodyLine("}");
       method.addBodyLine("if(currentPage<1){");
       method.addBodyLine("return AjaxResponseBean.Const.PARAMETER_INVALID_ERROR_RESPONSE_BEAN; ");
       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("return AjaxResponseBean.getNoDataReturnValueResponseBean();");
       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("return AjaxResponseBean.getReturnValueResponseBean(paginationBean);");
       method.addBodyLine("} catch (Exception e) {");
       method.addBodyLine("logger.error(\"查询异常\" + e.getMessage());");
       method.addBodyLine("return AjaxResponseBean.getErrorResponseBean(\"查询异常\" + e.getMessage());");
       method.addBodyLine("}"); 
	
	methodList.add(method);
       
       return methodList;
}
 
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(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 18
Source File: BasicSelectManyMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
@Override
public MethodAndImports generateMethodAndImports() {
    if (!introspectedTable.getRules().generateSelectByExampleWithBLOBs()
            && !introspectedTable.getRules().generateSelectByExampleWithoutBLOBs()) {
        return null;
    }
    
    Set<FullyQualifiedJavaType> imports = new HashSet<>();
    
    FullyQualifiedJavaType parameterType =
            new FullyQualifiedJavaType(
                    "org.mybatis.dynamic.sql.select.render.SelectStatementProvider");
    FullyQualifiedJavaType adapter =
            new FullyQualifiedJavaType("org.mybatis.dynamic.sql.util.SqlProviderAdapter");
    FullyQualifiedJavaType annotation =
            new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectProvider");
    
    imports.add(parameterType);
    imports.add(adapter);
    imports.add(annotation);
    
    imports.add(FullyQualifiedJavaType.getNewListInstance());
    
    imports.add(recordType);
    FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
    returnType.addTypeArgument(recordType);
    Method method = new Method("selectMany");
    method.setAbstract(true);
    method.setReturnType(returnType);
    method.addParameter(new Parameter(parameterType, "selectStatement"));
    context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
    method.addAnnotation("@SelectProvider(type=SqlProviderAdapter.class, method=\"select\")");

    MethodAndImports.Builder builder = MethodAndImports.withMethod(method)
            .withImports(imports);

    MethodParts methodParts;
    if (introspectedTable.isConstructorBased()) {
        methodParts = fragmentGenerator.getAnnotatedConstructorArgs();
    } else {
        methodParts = fragmentGenerator.getAnnotatedResults();
    }
    acceptParts(builder, method, methodParts);
    
    return builder.build();
}
 
Example 19
Source File: ProviderSelectByExampleWithoutBLOBsMethodGenerator.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.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$
    } else {
    	importedTypes.add(NEW_BUILDER_IMPORT);
    }
    
    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);
    
    if (useLegacyBuilder) {
    	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
    } else {
    	method.addBodyLine("SQL sql = new SQL();"); //$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("%sSELECT_DISTINCT(\"%s\");", //$NON-NLS-1$
            	builderPrefix,
                escapeStringForJava(getSelectListPhrase(introspectedColumn))));
            method.addBodyLine("} else {"); //$NON-NLS-1$
            method.addBodyLine(String.format("%sSELECT(\"%s\");", //$NON-NLS-1$
            	builderPrefix,
                escapeStringForJava(getSelectListPhrase(introspectedColumn))));
            method.addBodyLine("}"); //$NON-NLS-1$
        } else {
            method.addBodyLine(String.format("%sSELECT(\"%s\");", //$NON-NLS-1$
            	builderPrefix,
                escapeStringForJava(getSelectListPhrase(introspectedColumn))));
        }
        
        distinctCheck = false;
    }

    method.addBodyLine(String.format("%sFROM(\"%s\");", //$NON-NLS-1$
    		builderPrefix,
            escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    if (useLegacyBuilder) {
    	method.addBodyLine("applyWhere(example, false);"); //$NON-NLS-1$
    } else {
    	method.addBodyLine("applyWhere(sql, example, false);"); //$NON-NLS-1$
    }
    
    method.addBodyLine(""); //$NON-NLS-1$
    method.addBodyLine("if (example != null && example.getOrderByClause() != null) {"); //$NON-NLS-1$
    method.addBodyLine(String.format("%sORDER_BY(example.getOrderByClause());", builderPrefix)); //$NON-NLS-1$
    method.addBodyLine("}"); //$NON-NLS-1$
    
    method.addBodyLine(""); //$NON-NLS-1$
    if (useLegacyBuilder) {
    	method.addBodyLine("return SQL();"); //$NON-NLS-1$
    } else {
    	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: CaseInsensitiveLikePlugin.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
        IntrospectedTable introspectedTable) {

    InnerClass criteria = null;
    // first, find the Criteria inner class
    for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
        if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
            criteria = innerClass;
            break;
        }
    }

    if (criteria == null) {
        // can't find the inner class for some reason, bail out.
        return true;
    }

    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getNonBLOBColumns()) {
        if (!introspectedColumn.isJdbcCharacterColumn()
                || !introspectedColumn.isStringColumn()) {
            continue;
        }

        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("LikeInsensitive"); //$NON-NLS-1$
        method.setName(sb.toString());
        method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());

        sb.setLength(0);
        sb.append("addCriterion(\"upper("); //$NON-NLS-1$
        sb.append(Ibatis2FormattingUtilities
                .getAliasedActualColumnName(introspectedColumn));
        sb.append(") like\", value.toUpperCase(), \""); //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty());
        sb.append("\");"); //$NON-NLS-1$
        method.addBodyLine(sb.toString());
        method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$

        criteria.addMethod(method);
    }

    return true;
}