private void addCriteriaMethods(TopLevelClass topLevelClass, int newMethodsStart) {
        if (!generateCriteriaMethods) return;
        InnerClass criteria = null;
        for (InnerClass c : topLevelClass.getInnerClasses()) {
            if (c.getType().getShortName().equals("Criteria")) criteria = c;
        }
        if (criteria == null) return;
        boolean owner = false;
        for (Field f : criteria.getFields()) if (ExampleMethodsChainPlugin.OWNER.equals(f.getName())) owner = true;
        if (!owner) return;

        for (ListIterator<Method> methods = topLevelClass.getMethods().listIterator(newMethodsStart); methods.hasNext(); ) {
            Method base = methods.next();
            if (base.getVisibility() != PUBLIC || base.isStatic() || base.isConstructor()) continue;
            Method m = method(PUBLIC, base.getReturnType(), base.getName());
            StringBuilder sb = new StringBuilder();
            sb.append("return ").append(ExampleMethodsChainPlugin.OWNER).append(".").append(base.getName()).append("(");
            for (ListIterator<Parameter> params = base.getParameters().listIterator(); params.hasNext(); ) {
                if (params.hasPrevious()) sb.append(", ");
                Parameter p = params.next();
                m.addParameter(new Parameter(p.getType(), p.getName()));
                sb.append(p.getName());
            }
            sb.append(");");
            m.addBodyLine(sb.toString());
            criteria.addMethod(m);
        }
    }
  @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.getSelectByExampleStatementId(),
            "example")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return list;"); // $NON-NLS-1$

    if (context
        .getPlugins()
        .clientSelectByExampleWithoutBLOBsMethodGenerated(
            method, topLevelClass, introspectedTable)) {
      topLevelClass.addImportedTypes(importedTypes);
      topLevelClass.addMethod(method);
    }
  }
 /** 添加方法 */
 protected Method getOtherInteger(
     String methodName, IntrospectedTable introspectedTable, String tableName, int type) {
   Method method = new Method();
   method.setName(methodName);
   method.setReturnType(FullyQualifiedJavaType.getIntInstance());
   String params = addParams(introspectedTable, method, type);
   method.setVisibility(JavaVisibility.PUBLIC);
   StringBuilder sb = new StringBuilder();
   // method.addBodyLine("try {");
   sb.append("return this.");
   sb.append(getDaoShort());
   if (introspectedTable.hasBLOBColumns()
       && (!"updateByPrimaryKeySelective".equals(methodName)
           && !"deleteByPrimaryKey".equals(methodName)
           && !"deleteByExample".equals(methodName)
           && !"updateByExampleSelective".equals(methodName))) {
     sb.append(methodName + "WithoutBLOBs");
   } else {
     sb.append(methodName);
   }
   sb.append("(");
   sb.append(params);
   sb.append(");");
   method.addBodyLine(sb.toString());
   return method;
 }
  @Override
  public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = new Method();

    FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
    importedTypes.add(returnType);
    FullyQualifiedJavaType listType;
    listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

    importedTypes.add(listType);
    returnType.addTypeArgument(listType);
    method.setReturnType(returnType);
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName(introspectedTable.getSelectSelectiveStatementId());

    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();

    importedTypes.add(parameterType);
    method.addParameter(new Parameter(parameterType, "record")); // $NON-NLS-1$

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

    addMapperAnnotations(interfaze, method);

    if (context
        .getPlugins()
        .clientSelectSelectiveMethodGenerated(method, interfaze, introspectedTable)) {
      interfaze.addImportedTypes(importedTypes);
      interfaze.addMethod(method);
    }
  }
예제 #5
0
 private void addLimit(
     TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
   CommentGenerator commentGenerator = context.getCommentGenerator();
   FullyQualifiedJavaType limitType = new FullyQualifiedJavaType("com.tommy.common.Limit");
   topLevelClass.addImportedType(limitType);
   Field field = new Field();
   field.setVisibility(JavaVisibility.PROTECTED);
   field.setType(limitType);
   field.setName(name);
   commentGenerator.addFieldComment(field, introspectedTable);
   topLevelClass.addField(field);
   char c = name.charAt(0);
   String camel = Character.toUpperCase(c) + name.substring(1);
   Method method = new Method();
   method.setVisibility(JavaVisibility.PUBLIC);
   method.setName("set" + camel);
   method.addParameter(new Parameter(limitType, name));
   method.addBodyLine("this." + name + "=" + name + ";");
   commentGenerator.addGeneralMethodComment(method, introspectedTable);
   topLevelClass.addMethod(method);
   method = new Method();
   method.setVisibility(JavaVisibility.PUBLIC);
   method.setReturnType(limitType);
   method.setName("get" + camel);
   method.addBodyLine("return " + name + ";");
   commentGenerator.addGeneralMethodComment(method, introspectedTable);
   topLevelClass.addMethod(method);
 }
  @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);
    }
  }
    private void addUserDefinedMethods(TopLevelClass exampleClass, Interface mapperClass, IntrospectedTable introspectedTable, MyBatisClasses cls) {
        for (Method action : mapperClass.getMethods()) {
            if (!userDefinedMethods.matcher(action.getName()).matches()) continue;
            StringBuilder args = new StringBuilder();
            List<Parameter> params = new ArrayList<Parameter>();
            boolean example = false;
            if (action.getParameters() != null)
                for (Parameter param : action.getParameters()) {
                    String name;
                    if (Objects.equals(param.getType(), exampleClass.getType())) {
                        example = true;
                        name = "this";
                    } else {
                        name = param.getName();
                        params.add(new Parameter(param.getType(), name));
                    }
                    if (args.length() > 0)
                        args.append(", ");
                    args.append(name);
                }
            if (!example) {
                //System.err.println("Invalid user-defined mapper method: "+action.getName());
                continue;
            }

            exampleClass.addMethod(method(
                PUBLIC, INT, action.getName(), _(sqlSession, "sql"), params.toArray(new Parameter[params.size()]), __(
                    "return sql.getMapper(" + cls.names.mapper + ".class)."+action.getName()+"("+args+");"
            )));
            exampleClass.addMethod(method(
                PUBLIC, INT, action.getName(), _(cls.types.mapper, "mapper"), params.toArray(new Parameter[params.size()]), __(
                    "return mapper."+action.getName()+"("+args+");"
            )));
        }
    }
  protected void addDefaultNoArgConstructor(
      TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
    Method defaultNoArgConstructor = new Method();
    defaultNoArgConstructor.setVisibility(JavaVisibility.PUBLIC);
    defaultNoArgConstructor.setName(topLevelClass.getType().getShortName());
    defaultNoArgConstructor.setConstructor(true);
    defaultNoArgConstructor.addBodyLine("//Needed For JAXB serialization.");

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

    topLevelClass.addMethod(defaultNoArgConstructor);
  }
 /** 添加方法 */
 protected Method selectByPrimaryKey(IntrospectedTable introspectedTable, String tableName) {
   Method method = new Method();
   method.setName("selectByPrimaryKey");
   method.setReturnType(pojoType);
   if (introspectedTable.getRules().generatePrimaryKeyClass()) {
     FullyQualifiedJavaType type =
         new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
     method.addParameter(new Parameter(type, "key"));
   } else {
     for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
       FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
       method.addParameter(new Parameter(type, introspectedColumn.getJavaProperty()));
     }
   }
   method.setVisibility(JavaVisibility.PUBLIC);
   StringBuilder sb = new StringBuilder();
   // method.addBodyLine("try {");
   sb.append("return this.");
   sb.append(getDaoShort());
   sb.append("selectByPrimaryKey");
   sb.append("(");
   for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
     sb.append(introspectedColumn.getJavaProperty());
     sb.append(",");
   }
   sb.setLength(sb.length() - 1);
   sb.append(");");
   method.addBodyLine(sb.toString());
   // method.addBodyLine("} catch (Exception e) {");
   // method.addBodyLine("logger.error(\"Exception: \", e);");
   // method.addBodyLine("return null;");
   // method.addBodyLine("}");
   return method;
 }
예제 #10
0
  public void addSetterComment(
      Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
    if (suppressAllComments) {
      return;
    }

    StringBuilder sb = new StringBuilder();

    method.addJavaDocLine("/**"); // $NON-NLS-1$
    method.addJavaDocLine(" * This method was generated by MyBatis Generator."); // $NON-NLS-1$

    sb.append(" * This method sets the value of the database column "); // $NON-NLS-1$
    sb.append(introspectedTable.getFullyQualifiedTable());
    sb.append('.');
    sb.append(introspectedColumn.getActualColumnName());
    method.addJavaDocLine(sb.toString());

    method.addJavaDocLine(" *"); // $NON-NLS-1$

    Parameter parm = method.getParameters().get(0);
    sb.setLength(0);
    sb.append(" * @param "); // $NON-NLS-1$
    sb.append(parm.getName());
    sb.append(" the value for "); // $NON-NLS-1$
    sb.append(introspectedTable.getFullyQualifiedTable());
    sb.append('.');
    sb.append(introspectedColumn.getActualColumnName());
    method.addJavaDocLine(sb.toString());

    addJavadocTag(method, false);

    method.addJavaDocLine(" */"); // $NON-NLS-1$
  }
예제 #11
0
  public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
    if (suppressAllComments) {
      return;
    }

    StringBuilder sb = new StringBuilder();

    method.addJavaDocLine("/**"); // $NON-NLS-1$
    method.addJavaDocLine(" * This method was generated by MyBatis Generator."); // $NON-NLS-1$

    sb.append(" * This method corresponds to the database table "); // $NON-NLS-1$
    sb.append(introspectedTable.getFullyQualifiedTable());
    method.addJavaDocLine(sb.toString());

    addJavadocTag(method, false);

    method.addJavaDocLine(" */"); // $NON-NLS-1$
  }
예제 #12
0
 /**
  * getter方法注释
  *
  * @param method
  * @param introspectedTable
  * @param introspectedColumn
  */
 public void addGetterComment(
     Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
   StringBuilder sb = new StringBuilder();
   method.addJavaDocLine("/**");
   if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
     sb.append(" * 获取");
     sb.append(introspectedColumn.getRemarks());
     method.addJavaDocLine(sb.toString());
     method.addJavaDocLine(" *");
   }
   sb.setLength(0);
   sb.append(" * @return ");
   sb.append(introspectedColumn.getActualColumnName());
   if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
     sb.append(" - ");
     sb.append(introspectedColumn.getRemarks());
   }
   method.addJavaDocLine(sb.toString());
   method.addJavaDocLine(" */");
 }
  private InnerClass getCriteriaInnerClass(TopLevelClass topLevelClass) {
    Method method;

    InnerClass answer = new InnerClass(FullyQualifiedJavaType.getCriteriaInstance());

    answer.setVisibility(JavaVisibility.PUBLIC);
    answer.setStatic(true);
    answer.setSuperClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());

    context.getCommentGenerator().addClassComment(answer, introspectedTable, true);

    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    method.setName("Criteria"); // $NON-NLS-1$
    method.setConstructor(true);
    method.addBodyLine("super();"); // $NON-NLS-1$
    answer.addMethod(method);

    return answer;
  }
  @Override
  public void addMapperAnnotations(Interface interfaze, Method method) {
    interfaze.addImportedType(
        new FullyQualifiedJavaType("org.apache.ibatis.annotations.Delete")); // $NON-NLS-1$

    method.addAnnotation("@Delete({"); // $NON-NLS-1$

    StringBuilder sb = new StringBuilder();
    javaIndent(sb, 1);
    sb.append("\"delete from "); // $NON-NLS-1$
    sb.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime()));
    sb.append("\","); // $NON-NLS-1$
    method.addAnnotation(sb.toString());

    boolean and = false;
    Iterator<IntrospectedColumn> iter = introspectedTable.getPrimaryKeyColumns().iterator();
    while (iter.hasNext()) {
      IntrospectedColumn introspectedColumn = iter.next();
      sb.setLength(0);
      javaIndent(sb, 1);
      if (and) {
        sb.append("  \"and "); // $NON-NLS-1$
      } else {
        sb.append("\"where "); // $NON-NLS-1$
        and = true;
      }

      sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
      sb.append(" = "); // $NON-NLS-1$
      sb.append(getParameterClause(introspectedColumn));
      sb.append('\"');
      if (iter.hasNext()) {
        sb.append(',');
      }

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

    method.addAnnotation("})"); // $NON-NLS-1$
  }
  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().getUpdateByPrimaryKeyWithBLOBsMethodName(introspectedTable));
    method.addParameter(new Parameter(parameterType, "record")); // $NON-NLS-1$

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

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

    return method;
  }
 /** 添加方法 */
 protected void addMethod(TopLevelClass topLevelClass, String tableName) {
   Method method2 = new Method();
   for (int i = 0; i < methods.size(); i++) {
     Method method = new Method();
     method2 = methods.get(i);
     method = method2;
     method.removeAllBodyLines();
     method.removeAnnotation();
     StringBuilder sb = new StringBuilder();
     sb.append("return this.");
     sb.append(getDaoShort());
     sb.append(method.getName());
     sb.append("(");
     List<Parameter> list = method.getParameters();
     for (int j = 0; j < list.size(); j++) {
       sb.append(list.get(j).getName());
       sb.append(",");
     }
     sb.setLength(sb.length() - 1);
     sb.append(");");
     method.addBodyLine(sb.toString());
     topLevelClass.addMethod(method);
   }
   methods.clear();
 }
  @Override
  public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(type);
    importedTypes.add(FullyQualifiedJavaType.getNewListInstance());

    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);

    FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
    FullyQualifiedJavaType listType;
    if (introspectedTable.getRules().generateBaseRecordClass()) {
      listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    } else if (introspectedTable.getRules().generatePrimaryKeyClass()) {
      listType = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
    } else {
      throw new RuntimeException(getString("RuntimeError.12")); // $NON-NLS-1$
    }

    importedTypes.add(listType);
    returnType.addTypeArgument(listType);
    method.setReturnType(returnType);

    method.setName(introspectedTable.getSelectByExampleStatementId());
    method.addParameter(new Parameter(type, "example")); // $NON-NLS-1$

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

    addMapperAnnotations(interfaze, method);

    if (context
        .getPlugins()
        .clientSelectByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) {
      interfaze.addImportedTypes(importedTypes);
      interfaze.addMethod(method);
    }
  }
  @Override
  public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(type);

    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setName(introspectedTable.getDeleteByExampleStatementId());
    method.addParameter(new Parameter(type, "condition")); // $NON-NLS-1$

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

    addMapperAnnotations(interfaze, method);

    if (context
        .getPlugins()
        .clientDeleteByExampleMethodGenerated(method, interfaze, introspectedTable)) {
      interfaze.addImportedTypes(importedTypes);
      interfaze.addMethod(method);
    }
  }
  /**
   * @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());
    }

    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(MyBatis3FormattingUtilities.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;
  }
  @Override
  public void addMapperAnnotations(Interface interfaze, Method method) {
    FullyQualifiedJavaType fqjt =
        new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
    interfaze.addImportedType(
        new FullyQualifiedJavaType("org.apache.ibatis.annotations.DeleteProvider")); // $NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append("@DeleteProvider(type="); // $NON-NLS-1$
    sb.append(fqjt.getShortName());
    sb.append(".class, method=\""); // $NON-NLS-1$
    sb.append(introspectedTable.getDeleteByExampleStatementId());
    sb.append("\")"); // $NON-NLS-1$

    method.addAnnotation(sb.toString());
  }
  @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.getUpdateMethod(
            introspectedTable.getIbatis2SqlMapNamespace(),
            introspectedTable.getUpdateByPrimaryKeyWithBLOBsStatementId(),
            "record")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

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

    if (context
        .getPlugins()
        .clientUpdateByPrimaryKeyWithBLOBsMethodGenerated(
            method, topLevelClass, introspectedTable)) {
      topLevelClass.addImportedTypes(importedTypes);
      topLevelClass.addMethod(method);
    }
  }
  private Method getSingleValueMethod(
      IntrospectedColumn introspectedColumn, String nameFragment, String operator) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(
        new Parameter(introspectedColumn.getFullyQualifiedJavaType(), "value")); // $NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); // $NON-NLS-1$
    sb.append(nameFragment);
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);

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

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

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

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

    return method;
  }
 /** 添加方法 */
 protected Method countByExample(IntrospectedTable introspectedTable, String tableName) {
   Method method = new Method();
   method.setName("countByExample");
   method.setReturnType(FullyQualifiedJavaType.getIntInstance());
   method.addParameter(new Parameter(pojoCriteriaType, "example"));
   method.setVisibility(JavaVisibility.PUBLIC);
   StringBuilder sb = new StringBuilder();
   sb.append("int count = this.");
   sb.append(getDaoShort());
   sb.append("countByExample");
   sb.append("(");
   sb.append("example");
   sb.append(");");
   method.addBodyLine(sb.toString());
   method.addBodyLine("logger.debug(\"count: {}\", count);");
   method.addBodyLine("return count;");
   return method;
 }
 /** type 的意义 pojo 1 key 2 example 3 pojo+example 4 */
 protected String addParams(IntrospectedTable introspectedTable, Method method, int type1) {
   switch (type1) {
     case 1:
       method.addParameter(new Parameter(pojoType, "record"));
       return "record";
     case 2:
       if (introspectedTable.getRules().generatePrimaryKeyClass()) {
         FullyQualifiedJavaType type =
             new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
         method.addParameter(new Parameter(type, "key"));
       } else {
         for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
           FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
           method.addParameter(new Parameter(type, introspectedColumn.getJavaProperty()));
         }
       }
       StringBuffer sb = new StringBuffer();
       for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
         sb.append(introspectedColumn.getJavaProperty());
         sb.append(",");
       }
       sb.setLength(sb.length() - 1);
       return sb.toString();
     case 3:
       method.addParameter(new Parameter(pojoCriteriaType, "example"));
       return "example";
     case 4:
       method.addParameter(0, new Parameter(pojoType, "record"));
       method.addParameter(1, new Parameter(pojoCriteriaType, "example"));
       if (method.getName().equals("updateByExampleSelective")
           || method.getName().equals("updateByExample")) {
         return "record, example.getCondition()";
       }
       return "record, example";
     default:
       break;
   }
   return null;
 }
  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);

    for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
      method.addParameter(
          new Parameter(
              introspectedColumn.getFullyQualifiedJavaType(),
              introspectedColumn.getJavaProperty()));
    }

    boolean comma = false;
    StringBuilder sb = new StringBuilder();
    sb.append("super("); // $NON-NLS-1$
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
      if (comma) {
        sb.append(", "); // $NON-NLS-1$
      } else {
        comma = true;
      }
      sb.append(introspectedColumn.getJavaProperty());
    }
    sb.append(");"); // $NON-NLS-1$
    method.addBodyLine(sb.toString());

    for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) {
      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);
  }
 /** 添加方法 */
 protected Method selectByExample(IntrospectedTable introspectedTable, String tableName) {
   Method method = new Method();
   method.setName("selectByExample");
   method.setReturnType(new FullyQualifiedJavaType("List<" + tableName + ">"));
   method.addParameter(new Parameter(pojoCriteriaType, "example"));
   method.setVisibility(JavaVisibility.PUBLIC);
   StringBuilder sb = new StringBuilder();
   sb.append("return this.");
   sb.append(getDaoShort());
   if (introspectedTable.hasBLOBColumns()) {
     sb.append("selectByExampleWithoutBLOBs");
   } else {
     sb.append("selectByExample");
   }
   sb.append("(");
   sb.append("example");
   sb.append(");");
   method.addBodyLine(sb.toString());
   return method;
 }
예제 #27
0
 /**
  * setter方法注释
  *
  * @param method
  * @param introspectedTable
  * @param introspectedColumn
  */
 public void addSetterComment(
     Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
   StringBuilder sb = new StringBuilder();
   method.addJavaDocLine("/**");
   if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
     sb.append(" * 设置");
     sb.append(introspectedColumn.getRemarks());
     method.addJavaDocLine(sb.toString());
     method.addJavaDocLine(" *");
   }
   Parameter parm = method.getParameters().get(0);
   sb.setLength(0);
   sb.append(" * @param ");
   sb.append(parm.getName());
   if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
     sb.append(" ");
     sb.append(introspectedColumn.getRemarks());
   }
   method.addJavaDocLine(sb.toString());
   method.addJavaDocLine(" */");
 }
 /** 添加方法 */
 protected Method getOtherInsertboolean(
     String methodName, IntrospectedTable introspectedTable, String tableName) {
   Method method = new Method();
   method.setName(methodName);
   method.setReturnType(returnType);
   method.addParameter(new Parameter(pojoType, "record"));
   method.setVisibility(JavaVisibility.PUBLIC);
   StringBuilder sb = new StringBuilder();
   if (returnType == null) {
     sb.append("this.");
   } else {
     sb.append("return this.");
   }
   sb.append(getDaoShort());
   sb.append(methodName);
   sb.append("(");
   sb.append("record");
   sb.append(");");
   method.addBodyLine(sb.toString());
   return method;
 }
  private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(type);
    importedTypes.add(FullyQualifiedJavaType.getNewListInstance());

    Method method = new Method();
    method.setVisibility(getExampleMethodVisibility());

    FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
    ;
    if (generateForJava5) {
      FullyQualifiedJavaType fqjt;
      if (introspectedTable.getRules().generateBaseRecordClass()) {
        fqjt = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
      } else if (introspectedTable.getRules().generatePrimaryKeyClass()) {
        fqjt = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
      } else {
        throw new RuntimeException(getString("RuntimeError.12")); // $NON-NLS-1$
      }

      importedTypes.add(fqjt);
      returnType.addTypeArgument(fqjt);
    }

    method.setReturnType(returnType);

    method.setName(
        getDAOMethodNameCalculator().getSelectByExampleWithoutBLOBsMethodName(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;
  }
  private Method getNoValueMethod(
      IntrospectedColumn introspectedColumn, String nameFragment, String operator) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); // $NON-NLS-1$
    sb.append(nameFragment);
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);
    sb.append("addCriterion(\""); // $NON-NLS-1$
    sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    sb.append(' ');
    sb.append(operator);
    sb.append("\");"); // $NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); // $NON-NLS-1$

    return method;
  }