org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities Java Examples

The following examples show how to use org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities. 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: AbstractXmbgPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateParametersSeparateByComma(String fieldPrefix, boolean ifNullCheck, boolean withParenthesis, List<IntrospectedColumn> columns, XmlElement parent) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));
  if (withParenthesis) {
    trimElement.addAttribute(new Attribute("prefix", "("));
    trimElement.addAttribute(new Attribute("suffix", ")"));
  }

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : columns) {
    sb.setLength(0);
    sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, fieldPrefix));
    sb.append(",");

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }
  parent.addElement(trimElement);
}
 
Example #2
Source File: ResultMapWithBLOBsElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
private void addResultMapElements(XmlElement answer) {
    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getBLOBColumns()) {
        XmlElement resultElement = new XmlElement("result"); //$NON-NLS-1$

        resultElement
                .addAttribute(new Attribute(
                        "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
        resultElement.addAttribute(new Attribute(
                "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
        resultElement.addAttribute(new Attribute(
                "jdbcType", introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$

        if (stringHasValue(introspectedColumn
                .getTypeHandler())) {
            resultElement.addAttribute(new Attribute(
                    "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
        }

        answer.addElement(resultElement);
    }
}
 
Example #3
Source File: OptimisticLockerPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 生成版本号set节点
 * @param selective
 * @return
 */
private TextElement generateVersionSetEle(boolean selective) {
    if (this.customizedNextVersion) {
        return new TextElement(
                (selective ? "" : "set ")
                        + MyBatis3FormattingUtilities.getEscapedColumnName(this.versionColumn)
                        + " = "
                        + MyBatis3FormattingUtilities.getParameterClause(this.versionColumn, "record.")
                        + ","
        );
    } else {
        return new TextElement(
                (selective ? "" : "set ")
                        + MyBatis3FormattingUtilities.getEscapedColumnName(this.versionColumn)
                        + " = "
                        + MyBatis3FormattingUtilities.getEscapedColumnName(this.versionColumn)
                        + " + 1,"
        );
    }
}
 
Example #4
Source File: CaseInsensitiveLikePlugin.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private Method toMethod(IntrospectedColumn introspectedColumn) {
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and");
    sb.append("LikeInsensitive");
    Method method = new Method(sb.toString());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(new Parameter(introspectedColumn
            .getFullyQualifiedJavaType(), "value"));

    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());

    sb.setLength(0);
    sb.append("addCriterion(\"upper(");
    sb.append(MyBatis3FormattingUtilities
            .getAliasedActualColumnName(introspectedColumn));
    sb.append(") like\", value.toUpperCase(), \"");
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\");");
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;");
    return method;
}
 
Example #5
Source File: ExampleGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 生成
 * @param element
 * @param introspectedColumn
 * @param prefix
 * @param type               1:key,2:value,3:set
 */
private static void generateSelectiveCommColumnTo(XmlElement element, IntrospectedColumn introspectedColumn, String prefix, int type) {
    switch (type) {
        case 3:
            List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, prefix, true);
            if (!incrementEles.isEmpty()) {
                // 增量插件支持
                for (Element ele : incrementEles) {
                    element.addElement(ele);
                }
            } else {
                element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
            }
            break;
        case 2:
            element.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
            break;
        case 1:
            element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ","));
            break;
    }
}
 
Example #7
Source File: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 生成 xxxByPrimaryKey 的where 语句
 * @param element
 * @param primaryKeyColumns
 * @param prefix
 * @return
 */
public static void generateWhereByPrimaryKeyTo(XmlElement element, List<IntrospectedColumn> primaryKeyColumns, String prefix) {
    StringBuilder sb = new StringBuilder();
    boolean and = false;
    for (IntrospectedColumn introspectedColumn : primaryKeyColumns) {
        sb.setLength(0);
        if (and) {
            sb.append("  and ");
        } else {
            sb.append("where ");
            and = true;
        }

        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
        element.addElement(new TextElement(sb.toString()));
    }
}
 
Example #8
Source File: AbstractXmbgPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateParameterForSet(String fieldPrefix, boolean ifNullCheck, List<IntrospectedColumn> columns, XmlElement dynamicElement) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : columns) {
    sb.setLength(0);
    sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
    sb.append(" = ");
    sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, fieldPrefix));
    sb.append(',');

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  dynamicElement.addElement(trimElement);
}
 
Example #9
Source File: AbstractXmbgPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateActualColumnNamesWithParenthesis(String fieldPrefix, String columnPrefix, boolean ifNullCheck, List<IntrospectedColumn> columns, XmlElement parent) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));
  trimElement.addAttribute(new Attribute("prefix", "("));
  trimElement.addAttribute(new Attribute("suffix", ")"));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : columns) {
    sb.setLength(0);
    sb.append((columnPrefix == null ? "" : columnPrefix) + MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
    sb.append(",");

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  parent.addElement(trimElement);
}
 
Example #10
Source File: AbstractXmbgPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateWhereConditions(String fieldPrefix, String columnPrefix, boolean ifNullCheck, List<IntrospectedColumn> columns, XmlElement parent) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : columns) {
    sb.setLength(0);
    sb.append((columnPrefix == null ? "" : columnPrefix) + MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
    sb.append(" = ");
    sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, fieldPrefix));
    sb.append(",");

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  XmlElement where = new XmlElement("where");
  where.addElement(trimElement);
  parent.addElement(where);
}
 
Example #11
Source File: HsqldbUpsertPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateCopyForSetByPrefix(String fieldPrefix, String leftPrefix, String rightPrefix, boolean ifNullCheck, IntrospectedTable introspectedTable, XmlElement dynamicElement) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {

    sb.setLength(0);
    String columnName = MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn);
    sb.append(leftPrefix + columnName);
    sb.append(" = ");
    sb.append(rightPrefix + columnName);
    sb.append(',');

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  dynamicElement.addElement(trimElement);
}
 
Example #12
Source File: ExampleGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #13
Source File: ResultMapWithBLOBsElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
private void addResultMapElements(XmlElement answer) {
    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getBLOBColumns()) {
        XmlElement resultElement = new XmlElement("result"); //$NON-NLS-1$

        resultElement
                .addAttribute(new Attribute(
                        "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
        resultElement.addAttribute(new Attribute(
                "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
        resultElement.addAttribute(new Attribute(
                "jdbcType", introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$

        if (stringHasValue(introspectedColumn
                .getTypeHandler())) {
            resultElement.addAttribute(new Attribute(
                    "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
        }

        answer.addElement(resultElement);
    }
}
 
Example #14
Source File: ExampleGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private Method getNoValueMethod(IntrospectedColumn introspectedColumn,
                                String nameFragment, String operator) {
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and");
    sb.append(nameFragment);
    Method method = new Method(sb.toString());

    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());

    sb.setLength(0);
    sb.append("addCriterion(\"");
    sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    sb.append(' ');
    sb.append(operator);
    sb.append("\");");
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;");

    return method;
}
 
Example #15
Source File: DB2UpsertPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateCopyForSetByPrefix(String fieldPrefix, String leftPrefix, String rightPrefix, boolean ifNullCheck, IntrospectedTable introspectedTable, XmlElement dynamicElement) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {

    sb.setLength(0);
    String columnName = MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn);
    sb.append(leftPrefix + columnName);
    sb.append(" = ");
    sb.append(rightPrefix + columnName);
    sb.append(',');

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  dynamicElement.addElement(trimElement);
}
 
Example #16
Source File: PostgisGeoPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void checkAndReplaceInput(List<IntrospectedColumn> columns, TextElement te) {
  String sql = te.getContent();
  for(IntrospectedColumn column : columns){
  	if(column.getFullyQualifiedJavaType().getShortName().equals("Geometry")){
  		String paramStr = MyBatis3FormattingUtilities.getParameterClause(column);
  		sql = StringUtils.replace(sql, paramStr, "ST_GeomFromText(" + paramStr + ","+srid+")"); //replace no prefix geo relate column
  		paramStr = MyBatis3FormattingUtilities.getParameterClause(column, "record.");
  		sql = StringUtils.replace(sql, paramStr, "ST_GeomFromText(" + paramStr + ","+srid+")"); //replace mbg generate prefix geo relate column
  		paramStr = MyBatis3FormattingUtilities.getParameterClause(column, "item.");
  		sql = StringUtils.replace(sql, paramStr, "ST_GeomFromText(" + paramStr + ","+srid+")"); //replace mbg batch plugin generate prefix geo relate column				
//				System.out.println();
//				System.out.println(sql);
  	}
  }
  try {
    FieldUtils.writeDeclaredField(te, "content", sql, true);
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  }
}
 
Example #17
Source File: OptimisticLockerPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 生成版本判断节点
 * @return
 */
private String generateVersionEleStr() {
    StringBuilder sb = new StringBuilder();
    sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(this.versionColumn));
    sb.append(" = ");
    sb.append("#{version,jdbcType=");
    sb.append(this.versionColumn.getJdbcTypeName());
    if (StringUtility.stringHasValue(this.versionColumn.getTypeHandler())) {
        sb.append(",typeHandler=");
        sb.append(this.versionColumn.getTypeHandler());
    }
    sb.append("}");
    return sb.toString();
}
 
Example #18
Source File: AbstractUpsertPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
/**
 * 创建根据传入的Array数组进行判断的sql语句
 *
 * @param introspectedTable The metadata for database table
 * @return generated xml element for check input array params
 */
protected XmlElement buildSqlClause(IntrospectedTable introspectedTable) {

  XmlElement sql = new XmlElement("sql");
  sql.addAttribute(new Attribute("id", IDENTIFIERS_ARRAY_CONDITIONS));

  XmlElement foreach = new XmlElement("foreach");
  foreach.addAttribute(new Attribute("collection", "array"));
  foreach.addAttribute(new Attribute("item", "item"));
  foreach.addAttribute(new Attribute("index", "index"));
  foreach.addAttribute(new Attribute("separator", " and "));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
    XmlElement isEqualElement = new XmlElement("if");
    sb.setLength(0);
    sb.append("item == \'");
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\'");
    isEqualElement.addAttribute(new Attribute("test", sb.toString()));
    foreach.addElement(isEqualElement);

    sb.setLength(0);
    sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
    sb.append(" = ");
    sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, PROPERTY_PREFIX));

    isEqualElement.addElement(new TextElement(sb.toString()));
  }

  sql.addElement(foreach);
  return sql;
}
 
Example #19
Source File: BlobColumnListElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql");

    answer.addAttribute(new Attribute("id",
            introspectedTable.getBlobColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();

    Iterator<IntrospectedColumn> iter = introspectedTable.getBLOBColumns()
            .iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", ");
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement(new TextElement(sb.toString()));
    }

    if (context.getPlugins().sqlMapBlobColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example #20
Source File: BaseColumnListElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$

    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getBaseColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iter = introspectedTable
            .getNonBLOBColumns().iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", "); //$NON-NLS-1$
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement((new TextElement(sb.toString())));
    }

    if (context.getPlugins().sqlMapBaseColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example #21
Source File: BaseColumnListElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql");

    answer.addAttribute(new Attribute("id",
            introspectedTable.getBaseColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iter = introspectedTable
            .getNonBLOBColumns().iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", ");
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement(new TextElement(sb.toString()));
    }

    if (context.getPlugins().sqlMapBaseColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example #22
Source File: HsqldbUpsertPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override
protected XmlElement buildSqlClause(IntrospectedTable introspectedTable) {
  XmlElement sql = new XmlElement("sql");
  sql.addAttribute(new Attribute("id", IDENTIFIERS_ARRAY_CONDITIONS));

  XmlElement foreach = new XmlElement("foreach");
  foreach.addAttribute(new Attribute("collection", "array"));
  foreach.addAttribute(new Attribute("item", "item"));
  foreach.addAttribute(new Attribute("index", "index"));
  foreach.addAttribute(new Attribute("separator", " and "));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
    XmlElement isEqualElement = new XmlElement("if");
    sb.setLength(0);
    sb.append("item == \'");
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\'");
    isEqualElement.addAttribute(new Attribute("test", sb.toString()));
    foreach.addElement(isEqualElement);

    sb.setLength(0);

    String columnName = MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn);

    sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + "." + columnName);
    sb.append(" = ");
    sb.append("temp." + columnName);

    isEqualElement.addElement(new TextElement(sb.toString()));
  }

  sql.addElement(foreach);

  return sql;
}
 
Example #23
Source File: SQLServerPaginationPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
private XmlElement buildInner(XmlElement orderBy, IntrospectedTable introspectedTable) {
  // generate order by clause, first check if user provided order by clause, if provided just use it
  // otherwise use the default primary key for order by clause
  XmlElement newOrderBy = new XmlElement("choose");
  XmlElement when = new XmlElement("when");
  when.addAttribute(new Attribute("test", "orderByClause != null"));
  for (Element e : orderBy.getElements()) {
    when.addElement(e);
  }
  newOrderBy.addElement(when);

  XmlElement otherwise = new XmlElement("otherwise");
  StringBuilder sb = new StringBuilder();
  sb.append(" order by ");
  List<IntrospectedColumn> columns = introspectedTable.getPrimaryKeyColumns();
  for (IntrospectedColumn column : columns) {
    sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(column)).append(", ");
  }
  sb.setLength(sb.length() - 2);
  otherwise.addElement(new TextElement(sb.toString()));
  newOrderBy.addElement(otherwise);

  XmlElement inner = new XmlElement("if");
  inner.addAttribute(new Attribute("test", "limit != null and limit>=0 and offset != null"));
  inner.addElement(new TextElement(" , ROW_NUMBER() over ( "));
  inner.addElement(newOrderBy);
  inner.addElement(new TextElement(" ) as row_num "));
  return inner;
}
 
Example #24
Source File: ExampleEnhancedPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 生成column操作的具体方法
 * @param introspectedTable
 * @param introspectedColumn
 * @param nameFragment
 * @param operator
 * @return
 */
private Method generateSingleValueMethod(IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, String nameFragment, String operator) {
    // 方法名
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and");
    sb.append(nameFragment);
    sb.append("Column");

    Method method = JavaElementGeneratorTools.generateMethod(
            sb.toString(),
            JavaVisibility.PUBLIC,
            FullyQualifiedJavaType.getCriteriaInstance(),
            new Parameter(
                    new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName() + "." + ModelColumnPlugin.ENUM_NAME),
                    "column"
            )
    );

    // 方法体
    sb.setLength(0);
    sb.append("addCriterion(");
    sb.append("new StringBuilder(\"");
    sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    sb.append(" ");
    sb.append(operator);
    sb.append(" \").append(");
    sb.append("column.");
    sb.append(ModelColumnPlugin.METHOD_GET_ESCAPED_COLUMN_NAME);
    sb.append("()).toString());");

    JavaElementGeneratorTools.generateMethodBody(
            method,
            sb.toString(),
            "return (Criteria) this;"
    );

    return method;
}
 
Example #25
Source File: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public XmlElement generateIncrementSet(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
    if (this.supportIncrement(introspectedColumn)) {
        // 条件
        XmlElement choose = new XmlElement("choose");

        // 启用增量操作
        String columnMap = (prefix != null ? prefix : "_parameter.") + FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName());
        XmlElement whenIncEle = new XmlElement("when");
        whenIncEle.addAttribute(new Attribute("test", columnMap + " != null"));
        TextElement spec = new TextElement(
                MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " +
                        MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)
                        + " ${" + columnMap + "." + FIELD_OPERATE_FOR_CLASS_INCREMENT + "} "
                        + XmlElementGeneratorTools.getParameterClause(columnMap + "." + FIELD_VALUE_FOR_CLASS_INCREMENT, introspectedColumn)
                        + (hasComma ? "," : ""));
        whenIncEle.addElement(spec);
        choose.addElement(whenIncEle);

        // 没有启用增量操作
        XmlElement otherwiseEle = new XmlElement("otherwise");
        otherwiseEle.addElement(new TextElement(
                MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix)
                        + (hasComma ? "," : "")
        ));
        choose.addElement(otherwiseEle);

        return choose;
    }
    return null;
}
 
Example #26
Source File: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public XmlElement generateIncrementSetSelective(IntrospectedColumn introspectedColumn, String prefix) {
    if (this.supportIncrement(introspectedColumn)) {
        // 条件
        XmlElement choose = new XmlElement("choose");

        // 启用增量操作
        String columnMap = (prefix != null ? prefix : "_parameter.") + FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName());
        XmlElement whenIncEle = new XmlElement("when");
        whenIncEle.addAttribute(new Attribute("test", columnMap + " != null"));
        TextElement spec = new TextElement(
                MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " +
                        MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)
                        + " ${" + columnMap + "." + FIELD_OPERATE_FOR_CLASS_INCREMENT + "} "
                        + XmlElementGeneratorTools.getParameterClause(columnMap + "." + FIELD_VALUE_FOR_CLASS_INCREMENT, introspectedColumn)
                        + ",");
        whenIncEle.addElement(spec);
        choose.addElement(whenIncEle);

        // 没有启用增量操作
        XmlElement whenEle = new XmlElement("when");
        whenEle.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null"));
        whenEle.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
        choose.addElement(whenEle);

        return choose;
    }
    return null;
}
 
Example #27
Source File: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 生成增量操作节点(SelectiveEnhancedPlugin)
 * @param columns
 * @return
 * @see SelectiveEnhancedPlugin#generateSetsSelective(List, IntrospectedColumn)
 */
@Override
public List<XmlElement> generateIncrementSetForSelectiveEnhancedPlugin(List<IntrospectedColumn> columns) {
    if (this.support()) {
        List<XmlElement> results = new ArrayList<>();
        for (IntrospectedColumn incColumn : this.incColumns) {
            // !!! 不能用contains,IntrospectedColumn对象不同
            for (IntrospectedColumn column : columns) {
                if (incColumn.getActualColumnName().equals(column.getActualColumnName())) {
                    XmlElement when = new XmlElement("when");

                    // 需要 inc 的列
                    String columnMap = "record." + FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(incColumn.getActualColumnName());

                    when.addAttribute(new Attribute("test", "'" + column.getActualColumnName() + "'.toString() == column.value"));
                    when.addElement(new TextElement("${column.escapedColumnName} = ${column.escapedColumnName} "
                            + "${" + columnMap + "." + FIELD_OPERATE_FOR_CLASS_INCREMENT + "} "
                            + XmlElementGeneratorTools.getParameterClause(columnMap + "." + FIELD_VALUE_FOR_CLASS_INCREMENT, incColumn))
                    );

                    results.add(when);
                }
            }
        }
        return results.isEmpty() ? null : results;

    }
    return null;
}
 
Example #28
Source File: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 生成resultMap的result 节点
 * @param name
 * @param introspectedColumn
 * @return
 */
public static XmlElement generateResultMapResultElement(String name, IntrospectedColumn introspectedColumn) {
    XmlElement resultElement = new XmlElement(name);

    resultElement.addAttribute(new Attribute("column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
    resultElement.addAttribute(new Attribute("property", introspectedColumn.getJavaProperty()));
    resultElement.addAttribute(new Attribute("jdbcType", introspectedColumn.getJdbcTypeName()));

    if (stringHasValue(introspectedColumn.getTypeHandler())) {
        resultElement.addAttribute(new Attribute("typeHandler", introspectedColumn.getTypeHandler()));
    }
    return resultElement;
}
 
Example #29
Source File: BaseColumnListElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$

    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getBaseColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iter = introspectedTable
            .getNonBLOBColumns().iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", "); //$NON-NLS-1$
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement((new TextElement(sb.toString())));
    }

    if (context.getPlugins().sqlMapBaseColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example #30
Source File: BlobColumnListElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$

    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getBlobColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();

    Iterator<IntrospectedColumn> iter = introspectedTable.getBLOBColumns()
            .iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", "); //$NON-NLS-1$
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement((new TextElement(sb.toString())));
    }

    if (context.getPlugins().sqlMapBlobColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}