Package org.mybatis.generator.api.dom.java

Examples of org.mybatis.generator.api.dom.java.Method


    super();
  }

  @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);
  }
View Full Code Here


    addCheckedException(new FullyQualifiedJavaType("java.sql.SQLException")); //$NON-NLS-1$
  }

  @Override
  protected void configureConstructorTemplate() {
    Method method = new Method();
    method.setConstructor(true);
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addBodyLine("super();"); //$NON-NLS-1$
    setConstructorTemplate(method);
  }
View Full Code Here

    setInsertMethodTemplate("sqlMapClient.insert(\"{0}.{1}\", {2});"); //$NON-NLS-1$
  }

  @Override
  protected void configureMethods() {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setSqlMapClient"); //$NON-NLS-1$
    method.addParameter(new Parameter(sqlMapClientType, "sqlMapClient")); //$NON-NLS-1$
    method.addBodyLine("this.sqlMapClient = sqlMapClient;"); //$NON-NLS-1$
    addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("getSqlMapClient"); //$NON-NLS-1$
    method.setReturnType(sqlMapClientType);
    method.addBodyLine("return sqlMapClient;"); //$NON-NLS-1$
    addMethod(method);
  }
View Full Code Here

    super();
  }

  @Override
  protected void configureConstructorTemplate() {
    Method method = new Method();
    method.setConstructor(true);
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addBodyLine("super();"); //$NON-NLS-1$
    setConstructorTemplate(method);
  }
View Full Code Here

  }

  public final Method getConstructorClone(CommentGenerator commentGenerator, FullyQualifiedJavaType type,
      IntrospectedTable introspectedTable) {
    configure();
    Method answer = new Method();
    answer.setConstructor(true);
    answer.setName(type.getShortName());
    answer.setVisibility(constructorTemplate.getVisibility());
    for (Parameter parm : constructorTemplate.getParameters()) {
      answer.addParameter(parm);
    }

    for (String bodyLine : constructorTemplate.getBodyLines()) {
      answer.addBodyLine(bodyLine);
    }

    for (FullyQualifiedJavaType fqjt : constructorTemplate.getExceptions()) {
      answer.addException(fqjt);
    }

    commentGenerator.addGeneralMethodComment(answer, introspectedTable);

    return answer;
View Full Code Here

  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);
    }
View Full Code Here

    FullyQualifiedJavaType types = new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>");
    Rules rules = introspectedTable.getRules();
    if (rules.generateUpdateByExampleSelective() || rules.generateUpdateByExampleWithBLOBs()
        || rules.generateUpdateByExampleWithoutBLOBs()) {
      Method method = new Method();
      method.setVisibility(JavaVisibility.PROTECTED);
      method.setConstructor(true);
      method.setName(criteria.getShortName());
      method.addParameter(new Parameter(criteria, "example")); //$NON-NLS-1$
      method.addBodyLine("this.orderByClause = example.orderByClause;"); //$NON-NLS-1$
      method.addBodyLine("this.condition = example.condition;"); //$NON-NLS-1$
      method.addBodyLine("this.distinct = example.distinct;"); //$NON-NLS-1$
      method.addBodyLine("this.limit = example.limit;"); //$NON-NLS-1$
      method.addBodyLine("this.start = example.start;"); //$NON-NLS-1$
      topLevelClass.addMethod(method);
    }

    Field field = new Field();
    field.setVisibility(JavaVisibility.PRIVATE);
    field.setType(types);
    field.setName("condition"); //$NON-NLS-1$
    addFieldComment(field, "存放条件查询值");
    topLevelClass.addField(field);

    // add field, getter, setter for distinct
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    field.setName("distinct"); //$NON-NLS-1$
    addFieldComment(field, "是否相异");
    topLevelClass.addField(field);

    // add field, getter, setter for orderby clause
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getStringInstance());
    field.setName("orderByClause"); //$NON-NLS-1$
    addFieldComment(field, "排序字段");
    topLevelClass.addField(field);

    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName("Criteria"); //$NON-NLS-1$
    method.addBodyLine("this.condition = new HashMap<String, Object>();"); //$NON-NLS-1$
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("clear"); //$NON-NLS-1$
    method.addBodyLine("this.condition.clear();"); //$NON-NLS-1$
    method.addBodyLine("this.orderByClause = null;"); //$NON-NLS-1$
    method.addBodyLine("this.distinct = false;"); //$NON-NLS-1$
    method.addBodyLine("this.limit=null;"); //$NON-NLS-1$
    method.addBodyLine("this.start=null;"); //$NON-NLS-1$
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(criteria);
    method.setName("put"); //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value")); //$NON-NLS-1$
    method.addBodyLine("this.condition.put(condition, value);"); //$NON-NLS-1$
    method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
    addSetterComment(method, OutputUtilities.lineSeparator+"\t *            查询的条件名称"+OutputUtilities.lineSeparator+"\t * @param value"+OutputUtilities.lineSeparator+"\t *            查询的值", "condition");
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setOrderByClause"); //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClause")); //$NON-NLS-1$
    method.addBodyLine("this.orderByClause = orderByClause;"); //$NON-NLS-1$
    addSetterComment(method, OutputUtilities.lineSeparator+"\t *            排序字段", "orderByClause");
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setDistinct"); //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "distinct")); //$NON-NLS-1$
    method.addBodyLine("this.distinct = distinct;"); //$NON-NLS-1$
    addSetterComment(method, OutputUtilities.lineSeparator+"\t *            是否相异", "distinct");
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setCondition"); //$NON-NLS-1$
    method.addParameter(new Parameter(types, "condition")); //$NON-NLS-1$
    method.addBodyLine("this.condition = condition;"); //$NON-NLS-1$
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(types);
    method.setName("getCondition"); //$NON-NLS-1$
    method.addBodyLine("return condition;"); //$NON-NLS-1$
    topLevelClass.addMethod(method);

    field = new Field();
    field.setVisibility(JavaVisibility.PRIVATE);
    field.setType(FullyQualifiedJavaType.getInteger());
    field.setName("limit"); //$NON-NLS-1$
    addFieldComment(field, "每页大小,即mysql中的length");
    topLevelClass.addField(field);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setLimit"); //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getInteger(), "limit")); //$NON-NLS-1$
    method.addBodyLine("this.limit = limit;"); //$NON-NLS-1$
    addSetterComment(method, OutputUtilities.lineSeparator+"\t *            每页大小,即mysql中的length", "limit");
    topLevelClass.addMethod(method);

    field = new Field();
    field.setVisibility(JavaVisibility.PRIVATE);
    field.setType(FullyQualifiedJavaType.getInteger());
    field.setName("start"); //$NON-NLS-1$
    addFieldComment(field, "开始行数,即mysql中的offset");
    topLevelClass.addField(field);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setStart"); //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getInteger(), "start")); //$NON-NLS-1$
    method.addBodyLine("this.start = start;"); //$NON-NLS-1$
    addSetterComment(method, OutputUtilities.lineSeparator+"\t *            开始行数,即mysql中的offset", "start");
    topLevelClass.addMethod(method);

    GeneratedJavaFile file = new GeneratedJavaFile(topLevelClass, context.getJavaModelGeneratorConfiguration()
        .getTargetProject());
View Full Code Here

    Parameter parameter = new Parameter(types, "condition", "@Param(\"condition\")");
    Parameter parameter2 = new Parameter(criteria, "example");
    interfaze.addImportedType(FullyQualifiedJavaType.getNewMapInstance());
    boolean first = true;
    for (int i = 0; i < methods.size(); i++) {
      Method method = methods.get(i);
      if (method.getFormattedContent(0, true).contains("Example")) {
        int size = method.getParameters().size();
        if (first) {
          interfaze.removeImportedType(new FullyQualifiedJavaType(introspectedTable.getExampleType()));
          interfaze.addImportedType(criteria);
        }
        if (size == 1) {
          method.removeParameter(0);
          if (isAllInOne) {
            method.addParameter(parameter);
          } else {
            method.addParameter(parameter2);
          }
        } else if (size == 2) {
          method.removeParameter(1);
          method.addParameter(1, parameter);
        }
        first = false;
      }
    }
View Full Code Here

   * @param introspectedTable
   *            the table corresponding to this class
   */
  protected void generateEquals(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns,
      IntrospectedTable introspectedTable) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    method.setName("equals"); //$NON-NLS-1$
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), "that")); //$NON-NLS-1$
    if (introspectedTable.isJava5Targeted()) {
      method.addAnnotation("@Override"); //$NON-NLS-1$
    }

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

    method.addBodyLine("if (this == that) {"); //$NON-NLS-1$
    method.addBodyLine("return true;"); //$NON-NLS-1$
    method.addBodyLine("}"); //$NON-NLS-1$

    method.addBodyLine("if (that == null) {"); //$NON-NLS-1$
    method.addBodyLine("return false;"); //$NON-NLS-1$
    method.addBodyLine("}"); //$NON-NLS-1$

    method.addBodyLine("if (getClass() != that.getClass()) {"); //$NON-NLS-1$
    method.addBodyLine("return false;"); //$NON-NLS-1$
    method.addBodyLine("}"); //$NON-NLS-1$

    StringBuilder sb = new StringBuilder();
    sb.append(topLevelClass.getType().getShortName());
    sb.append(" other = ("); //$NON-NLS-1$
    sb.append(topLevelClass.getType().getShortName());
    sb.append(") that;"); //$NON-NLS-1$
    method.addBodyLine(sb.toString());

    boolean first = true;
    Iterator<IntrospectedColumn> iter = introspectedColumns.iterator();
    while (iter.hasNext()) {
      IntrospectedColumn introspectedColumn = iter.next();

      sb.setLength(0);

      if (first) {
        sb.append("return ("); //$NON-NLS-1$
        first = false;
      } else {
        OutputUtilities.javaIndent(sb, 1);
        sb.append("&& ("); //$NON-NLS-1$
      }

      String getterMethod = getGetterMethodName(introspectedColumn.getJavaProperty(),
          introspectedColumn.getFullyQualifiedJavaType());

      if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
        sb.append("this."); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("() == "); //$NON-NLS-1$
        sb.append("other."); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("())"); //$NON-NLS-1$
      } else {
        sb.append("this."); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("() == null ? other."); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("() == null : this."); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("().equals(other."); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("()))"); //$NON-NLS-1$
      }

      if (!iter.hasNext()) {
        sb.append(';');
      }

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

    topLevelClass.addMethod(method);
  }
View Full Code Here

   * @param introspectedTable
   *            the table corresponding to this class
   */
  protected void generateHashCode(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns,
      IntrospectedTable introspectedTable) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setName("hashCode"); //$NON-NLS-1$
    if (introspectedTable.isJava5Targeted()) {
      method.addAnnotation("@Override"); //$NON-NLS-1$
    }

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

    method.addBodyLine("final int prime = 31;"); //$NON-NLS-1$
    method.addBodyLine("int result = 1;"); //$NON-NLS-1$

    StringBuilder sb = new StringBuilder();
    boolean hasTemp = false;
    Iterator<IntrospectedColumn> iter = introspectedColumns.iterator();
    while (iter.hasNext()) {
      IntrospectedColumn introspectedColumn = iter.next();

      FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();

      String getterMethod = getGetterMethodName(introspectedColumn.getJavaProperty(), fqjt);

      sb.setLength(0);
      if (fqjt.isPrimitive()) {
        if ("boolean".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          sb.append("result = prime * result + ("); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("() ? 1231 : 1237);"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
        } else if ("byte".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          sb.append("result = prime * result + "); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("();"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
        } else if ("char".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          sb.append("result = prime * result + "); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("();"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
        } else if ("double".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          if (!hasTemp) {
            method.addBodyLine("long temp;"); //$NON-NLS-1$
            hasTemp = true;
          }
          sb.append("temp = Double.doubleToLongBits("); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("());"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
          method.addBodyLine("result = prime * result + (int) (temp ^ (temp >>> 32));"); //$NON-NLS-1$
        } else if ("float".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          sb.append("result = prime * result + Float.floatToIntBits("); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("());"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
        } else if ("int".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          sb.append("result = prime * result + "); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("();"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
        } else if ("long".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          sb.append("result = prime * result + (int) ("); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("() ^ ("); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("() >>> 32));"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
        } else if ("short".equals(fqjt.getFullyQualifiedName())) { //$NON-NLS-1$
          sb.append("result = prime * result + "); //$NON-NLS-1$
          sb.append(getterMethod);
          sb.append("();"); //$NON-NLS-1$
          method.addBodyLine(sb.toString());
        } else {
          // should never happen
          continue;
        }
      } else {
        sb.append("result = prime * result + (("); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("() == null) ? 0 : "); //$NON-NLS-1$
        sb.append(getterMethod);
        sb.append("().hashCode());"); //$NON-NLS-1$
        method.addBodyLine(sb.toString());
      }
    }

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

    topLevelClass.addMethod(method);
  }
View Full Code Here

TOP

Related Classes of org.mybatis.generator.api.dom.java.Method

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.