Search in sources :

Example 21 with Parameter

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

the class ExampleGenerator method addtypeHandledObjectsAndMethods.

/**
     * This method adds all the extra methods and fields required to support a
     * user defined type handler on some column.
     * 
     * @param introspectedColumn
     * @param constructor
     * @param innerClass
     * @return the name of the List added to the class by this method
     */
private String addtypeHandledObjectsAndMethods(IntrospectedColumn introspectedColumn, Method constructor, InnerClass innerClass) {
    String answer;
    StringBuilder sb = new StringBuilder();
    // add new private field and public accessor in the class
    sb.setLength(0);
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("Criteria");
    answer = sb.toString();
    Field field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    //$NON-NLS-1$
    field.setType(new FullyQualifiedJavaType("java.util.List<Criterion>"));
    field.setName(answer);
    innerClass.addField(field);
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(field.getType());
    method.setName(getGetterMethodName(field.getName(), field.getType()));
    //$NON-NLS-1$
    sb.insert(0, "return ");
    sb.append(';');
    method.addBodyLine(sb.toString());
    innerClass.addMethod(method);
    // add constructor initialization
    sb.setLength(0);
    sb.append(field.getName());
    //$NON-NLS-1$;
    sb.append(" = new ArrayList<Criterion>();");
    constructor.addBodyLine(sb.toString());
    // now add the methods for simplifying the individual field set methods
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("add");
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
    //$NON-NLS-1$
    sb.append("Criterion");
    method.setName(sb.toString());
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "value"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    //$NON-NLS-1$
    method.addBodyLine("if (value == null) {");
    method.addBodyLine(//$NON-NLS-1$
    "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");");
    //$NON-NLS-1$
    method.addBodyLine("}");
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%s.add(new Criterion(condition, value, \"%s\"));", field.getName(), introspectedColumn.getTypeHandler()));
    //$NON-NLS-1$
    method.addBodyLine("allCriteria = null;");
    innerClass.addMethod(method);
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("add");
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
    //$NON-NLS-1$
    sb.append("Criterion");
    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    method.setName(sb.toString());
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "condition"));
    method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
    "value1"));
    method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
    "value2"));
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
    "property"));
    if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
        //$NON-NLS-1$
        method.addBodyLine("if (value1 == null || value2 == null) {");
        method.addBodyLine(//$NON-NLS-1$
        "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");");
        //$NON-NLS-1$
        method.addBodyLine("}");
    }
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%s.add(new Criterion(condition, value1, value2, \"%s\"));", field.getName(), introspectedColumn.getTypeHandler()));
    //$NON-NLS-1$
    method.addBodyLine("allCriteria = null;");
    innerClass.addMethod(method);
    return answer;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method)

Example 22 with Parameter

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

the class ExampleGenerator method getSetInOrNotInMethod.

/**
     * 
     * @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 (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
        type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
    } else {
        type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType());
    }
    //$NON-NLS-1$
    method.addParameter(new Parameter(type, "values"));
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    //$NON-NLS-1$
    sb.insert(0, "and");
    if (inMethod) {
        //$NON-NLS-1$
        sb.append("In");
    } else {
        //$NON-NLS-1$
        sb.append("NotIn");
    }
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);
    if (introspectedColumn.isJDBCDateColumn()) {
        //$NON-NLS-1$
        sb.append("addCriterionForJDBCDate(\"");
    } else if (introspectedColumn.isJDBCTimeColumn()) {
        //$NON-NLS-1$
        sb.append("addCriterionForJDBCTime(\"");
    } else if (stringHasValue(introspectedColumn.getTypeHandler())) {
        //$NON-NLS-1$
        sb.append("add");
        sb.append(introspectedColumn.getJavaProperty());
        sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
        //$NON-NLS-1$
        sb.append("Criterion(\"");
    } else {
        //$NON-NLS-1$
        sb.append("addCriterion(\"");
    }
    sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    if (inMethod) {
        //$NON-NLS-1$
        sb.append(" in");
    } else {
        //$NON-NLS-1$
        sb.append(" not in");
    }
    //$NON-NLS-1$
    sb.append("\", values, \"");
    sb.append(introspectedColumn.getJavaProperty());
    //$NON-NLS-1$
    sb.append("\");");
    method.addBodyLine(sb.toString());
    //$NON-NLS-1$
    method.addBodyLine("return (Criteria) this;");
    return method;
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 23 with Parameter

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

the class PrimaryKeyGenerator method addParameterizedConstructor.

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);
        //$NON-NLS-1$
        sb.append("this.");
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(introspectedColumn.getJavaProperty());
        sb.append(';');
        method.addBodyLine(sb.toString());
    }
    topLevelClass.addMethod(method);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 24 with Parameter

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

the class Test1Generator method generate.

public List<CompilationUnit> generate() {
    FullyQualifiedJavaType cls = new FullyQualifiedJavaType(BASE_PACKAGE + ".SomeClass");
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    TopLevelClass tlcMain = generateFieldTypeMain();
    TopLevelClass tlcSub1 = generateFieldTypeSub1();
    TopLevelClass tlcTcSub1 = generateTestClassSub1();
    TopLevelClass tlcSub2 = generateFieldTypeSub2();
    answer.add(tlcMain);
    answer.add(tlcSub1);
    answer.add(tlcTcSub1);
    answer.add(tlcSub2);
    TopLevelClass topLvlClass = new TopLevelClass(cls);
    topLvlClass.setVisibility(JavaVisibility.PUBLIC);
    topLvlClass.addImportedType(tlcTcSub1.getType());
    Field field = new Field("main", tlcMain.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    field = new Field("tcSub1", tlcTcSub1.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    field = new Field("sub1", tlcSub1.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    field = new Field("sub2", tlcSub2.getType());
    field.setVisibility(JavaVisibility.PRIVATE);
    topLvlClass.addField(field);
    Method m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("executeMain");
    m.addBodyLine("main.mainMethod();");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("setMain");
    m.addParameter(new Parameter(tlcMain.getType(), "main"));
    m.addBodyLine("this.main = main;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("getMain");
    m.setReturnType(tlcMain.getType());
    m.addBodyLine("return main;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("executeSub1");
    m.addBodyLine("sub1.sub1Method();");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("setSub1");
    m.addParameter(new Parameter(tlcSub1.getType(), "sub1"));
    m.addBodyLine("this.sub1 = sub1;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("getSub1");
    m.setReturnType(tlcSub1.getType());
    m.addBodyLine("return sub1;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("executeSub2");
    m.addBodyLine("sub2.sub2Method();");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("setSub2");
    m.addParameter(new Parameter(tlcSub2.getType(), "sub2"));
    m.addBodyLine("this.sub2 = sub2;");
    topLvlClass.addMethod(m);
    m = new Method();
    m.setVisibility(JavaVisibility.PUBLIC);
    m.setName("getSub2");
    m.setReturnType(tlcSub2.getType());
    m.addBodyLine("return sub2;");
    topLvlClass.addMethod(m);
    answer.add(topLvlClass);
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 25 with Parameter

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

the class UpdateByPrimaryKeySelectiveMethodGenerator method getMethodShell.

private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    FullyQualifiedJavaType parameterType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        parameterType = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType());
    } else {
        parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    }
    importedTypes.add(parameterType);
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setName(getDAOMethodNameCalculator().getUpdateByPrimaryKeySelectiveMethodName(introspectedTable));
    //$NON-NLS-1$
    method.addParameter(new Parameter(parameterType, "record"));
    for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
        method.addException(fqjt);
        importedTypes.add(fqjt);
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    return method;
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Aggregations

Parameter (org.mybatis.generator.api.dom.java.Parameter)67 Method (org.mybatis.generator.api.dom.java.Method)66 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)54 TreeSet (java.util.TreeSet)20 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)17 Field (org.mybatis.generator.api.dom.java.Field)11 ArrayList (java.util.ArrayList)7 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)5 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)5 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)4 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)3 CommentGenerator (org.mybatis.generator.api.CommentGenerator)2 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)2 Interface (org.mybatis.generator.api.dom.java.Interface)2 DAOMethodNameCalculator (org.mybatis.generator.api.DAOMethodNameCalculator)1 Rules (org.mybatis.generator.internal.rules.Rules)1