Java Code Examples for org.mybatis.generator.api.IntrospectedColumn#getActualColumnName()

The following examples show how to use org.mybatis.generator.api.IntrospectedColumn#getActualColumnName() . 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: DefaultCommentGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
        IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
    imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
    String comment = "Source field: "
            + introspectedTable.getFullyQualifiedTable().toString()
            + "."
            + introspectedColumn.getActualColumnName();
    field.addAnnotation(getGeneratedAnnotation(comment));
    
    if (!suppressAllComments && addRemarkComments) {
        String remarks = introspectedColumn.getRemarks();
        if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * Database Column Remarks:");
            String[] remarkLines = remarks.split(System.getProperty("line.separator")); 
            for (String remarkLine : remarkLines) {
                field.addJavaDocLine(" *   " + remarkLine); 
            }
            field.addJavaDocLine(" */");
        }
    }
}
 
Example 2
Source File: CustomCommentGenerator.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
private void resolvePrimaryKey(Field field, IntrospectedTable table, IntrospectedColumn column) {
	String columnName = column.getActualColumnName();
	List<IntrospectedColumn> primaryKey = table.getPrimaryKeyColumns();
	String remark = column.getRemarks();
	for (IntrospectedColumn pk : primaryKey) {
		if (columnName.equals(pk.getActualColumnName())) {
			field.addAnnotation("@Id");
			String tableRemark = table.getRemarks();
			if (tableRemark.endsWith("表")) {
				tableRemark = tableRemark.substring(0, tableRemark.length() - 1);
			}
			remark = tableRemark + remark;
			break;
		}
	}
}
 
Example 3
Source File: PostgisGeoPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void checkAndReplaceOutput(List<IntrospectedColumn> columns, TextElement te) {
    String sql = te.getContent();
		for(IntrospectedColumn column : columns){
			if(column.getFullyQualifiedJavaType().getShortName().equals("Geometry")){
				String columnStr = null;
				if(column.isColumnNameDelimited()){
					columnStr = "\""+column.getActualColumnName()+"\"";
				}else{
					columnStr = column.getActualColumnName();
				}
				sql = StringUtils.replaceOnce(sql, columnStr, "ST_AsText("+columnStr+") as " + columnStr);
				//sql = sql.replace(column.getActualColumnName(), "ST_AsText("+column.getActualColumnName()+")");
//				System.out.println();
//				System.out.println(sql);
			}
		}
    try {
      FieldUtils.writeDeclaredField(te, "content", sql, true);
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }		
  }
 
Example 4
Source File: DefaultCommentGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
        IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
    imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
    String comment = "Source field: "
            + introspectedTable.getFullyQualifiedTable().toString()
            + "."
            + introspectedColumn.getActualColumnName();
    method.addAnnotation(getGeneratedAnnotation(comment));
}
 
Example 5
Source File: MyBatis3FormattingUtilities.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
/**
 * The renamed column name for a select statement. If there is a table
 * alias, the value will be alias_columnName. This is appropriate for use in
 * a result map.
 * 
 * @return the renamed column name
 */
public static String getRenamedColumnNameForResultMap(
        IntrospectedColumn introspectedColumn) {
    if (stringHasValue(introspectedColumn.getTableAlias())) {
        StringBuilder sb = new StringBuilder();

        sb.append(introspectedColumn.getTableAlias());
        sb.append('_');
        sb.append(introspectedColumn.getActualColumnName());
        return sb.toString();
    } else {
        return introspectedColumn.getActualColumnName();
    }
}
 
Example 6
Source File: Ibatis2FormattingUtilities.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
/**
 * The renamed column name for a select statement. If there is a table
 * alias, the value will be alias_columnName. This is appropriate for use in
 * a result map.
 * 
 * @return the renamed column name
 */
public static String getRenamedColumnNameForResultMap(
        IntrospectedColumn introspectedColumn) {
    if (stringHasValue(introspectedColumn.getTableAlias())) {
        StringBuilder sb = new StringBuilder();

        sb.append(introspectedColumn.getTableAlias());
        sb.append('_');
        sb.append(introspectedColumn.getActualColumnName());
        return sb.toString();
    } else {
        return introspectedColumn.getActualColumnName();
    }
}
 
Example 7
Source File: MyBatis3FormattingUtilities.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
/**
 * The renamed column name for a select statement. If there is a table alias, the value will be alias_columnName.
 * This is appropriate for use in a result map.
 *
 * @param introspectedColumn
 *            the introspected column
 * @return the renamed column name
 */
public static String getRenamedColumnNameForResultMap(
        IntrospectedColumn introspectedColumn) {
    if (stringHasValue(introspectedColumn.getTableAlias())) {
        StringBuilder sb = new StringBuilder();

        sb.append(introspectedColumn.getTableAlias());
        sb.append('_');
        sb.append(introspectedColumn.getActualColumnName());
        return sb.toString();
    } else {
        return introspectedColumn.getActualColumnName();
    }
}
 
Example 8
Source File: Ibatis2FormattingUtilities.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
/**
 * The renamed column name for a select statement. If there is a table alias, the value will be alias_columnName.
 * This is appropriate for use in a result map.
 *
 * @param introspectedColumn
 *            the introspected column
 * @return the renamed column name
 */
public static String getRenamedColumnNameForResultMap(
        IntrospectedColumn introspectedColumn) {
    if (stringHasValue(introspectedColumn.getTableAlias())) {
        StringBuilder sb = new StringBuilder();

        sb.append(introspectedColumn.getTableAlias());
        sb.append('_');
        sb.append(introspectedColumn.getActualColumnName());
        return sb.toString();
    } else {
        return introspectedColumn.getActualColumnName();
    }
}
 
Example 9
Source File: TemplateCommentGenerator.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
                               IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
    imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
    String comment = "Source field: "
            + introspectedTable.getFullyQualifiedTable().toString()
            + "."
            + introspectedColumn.getActualColumnName();
    field.addAnnotation(getGeneratedAnnotation(comment));
}
 
Example 10
Source File: TemplateCommentGenerator.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
                                       IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
    imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
    String comment = "Source field: "
            + introspectedTable.getFullyQualifiedTable().toString()
            + "."
            + introspectedColumn.getActualColumnName();
    method.addAnnotation(getGeneratedAnnotation(comment));
}
 
Example 11
Source File: CustomCommentGenerator.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
private void resolveColumn(Field field, IntrospectedTable table, IntrospectedColumn column) {
	String columnAnnotation = "@Column(name = \"" + column.getActualColumnName() + "\"";
	String typeName = field.getType().getShortName();
	if (typeName.equals("String") || typeName.equals("BigDecimal")) {
		if (column.getLength() != 0) {
			columnAnnotation = columnAnnotation + ", length = " + column.getLength();
		}
	}
	if (column.getScale() != 0) {
		columnAnnotation = columnAnnotation + ", scale = " + column.getScale();
	}
	columnAnnotation = columnAnnotation + ")";
	field.addAnnotation(columnAnnotation);
}
 
Example 12
Source File: MyBatis3FormattingUtilities.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
/**
 * The renamed column name for a select statement. If there is a table alias, the value will be alias_columnName.
 * This is appropriate for use in a result map.
 *
 * @param introspectedColumn
 *            the introspected column
 * @return the renamed column name
 */
public static String getRenamedColumnNameForResultMap(
        IntrospectedColumn introspectedColumn) {
    if (stringHasValue(introspectedColumn.getTableAlias())) {
        StringBuilder sb = new StringBuilder();

        sb.append(introspectedColumn.getTableAlias());
        sb.append('_');
        sb.append(introspectedColumn.getActualColumnName());
        return sb.toString();
    } else {
        return introspectedColumn.getActualColumnName();
    }
}
 
Example 13
Source File: OracleBatchInsertPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 4 votes vote down vote up
private void addSqlMapper(Document document, IntrospectedTable introspectedTable) {
    //1.Batchinsert
    XmlElement baseElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.INSERT,
            BATCH_INSERT,
            FullyQualifiedJavaType.getNewListInstance());

    XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,
            "item",
            "index",
            "union all");


    //tableName
    baseElement.addElement(new TextElement(String.format("insert into %s (", introspectedTable.getFullyQualifiedTableNameAtRuntime())));

    foreachElement.addElement(new TextElement("("));
    foreachElement.addElement(new TextElement("select"));

    for (int i = 0; i < introspectedTable.getAllColumns().size(); i++) {
        //column信息
        IntrospectedColumn introspectedColumn = introspectedTable.getAllColumns().get(i);

        String columnInfo = "";
        String valueInfo = "";

        columnInfo = introspectedColumn.getActualColumnName();
        valueInfo = MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.");
        if (introspectedColumn.isIdentity()) {
            String nextval = introspectedTable.getFullyQualifiedTableNameAtRuntime()+"_SEQUENCE.nextval" ;
            valueInfo = nextval;
        }
        if (i != (introspectedTable.getAllColumns().size() - 1)) {
            columnInfo += (",");
            valueInfo += ",";
        }
        baseElement.addElement(new TextElement(columnInfo));
        foreachElement.addElement(new TextElement(valueInfo));
    }
    foreachElement.addElement(new TextElement("from dual"));
    foreachElement.addElement(new TextElement(")"));


    baseElement.addElement(new TextElement(")"));

    baseElement.addElement(new TextElement("("));
    baseElement.addElement(foreachElement);
    baseElement.addElement(new TextElement(")"));

    //3.parent Add
    document.getRootElement().addElement(baseElement);
}
 
Example 14
Source File: BatchInsertPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 4 votes vote down vote up
/**
 * batchInsert和batchInsertSelective的SQL生成
 *
 * @param document
 * @param introspectedTable
 */
private void addSqlMapper(Document document, IntrospectedTable introspectedTable) {
    //table名名字
    String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
    //column信息
    List<IntrospectedColumn> columnList = introspectedTable.getAllColumns();

    XmlElement baseElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.INSERT,
            BATCH_INSERT,
            FullyQualifiedJavaType.getNewListInstance());

    XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,
            "item",
            "index",
            ",");

    baseElement.addElement(new TextElement(String.format("insert into %s (", tableName)));

    foreachElement.addElement(new TextElement("("));

    for (int i = 0; i < columnList.size(); i++) {
        String columnInfo = "";
        String valueInfo = "";
        IntrospectedColumn introspectedColumn = columnList.get(i);
        if (introspectedColumn.isIdentity()) {
            continue;
        }
        columnInfo = introspectedColumn.getActualColumnName();
        valueInfo = MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.");
        if (i != (columnList.size() - 1)) {
            columnInfo += (",");
            valueInfo += (",");
        }
        baseElement.addElement(new TextElement(columnInfo));
        foreachElement.addElement(new TextElement(valueInfo));

    }
    foreachElement.addElement(new TextElement(")"));

    baseElement.addElement(new TextElement(")"));

    baseElement.addElement(new TextElement("values"));

    baseElement.addElement(foreachElement);

    //3.parent Add
    document.getRootElement().addElement(baseElement);
}
 
Example 15
Source File: HySwaggerMapperPlugin.java    From jvue-admin with MIT License 4 votes vote down vote up
private void addFieldEnum(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
	if (this.modelFieldEnum) {
		String enumName = "FieldEnum";
		String javaFieldName = "javaFieldName";
		String dbFieldName = "dbFieldName";
		InnerEnum enum1 = new InnerEnum(new FullyQualifiedJavaType(enumName));
		enum1.setVisibility(JavaVisibility.PUBLIC);

		StringBuilder enumConstant = new StringBuilder();
		List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();
		int index = 0;
		for (IntrospectedColumn column : allColumns) {
			String dbName = column.getActualColumnName();
			String javaName = column.getJavaProperty();
			enumConstant.append(dbName.toUpperCase()).append("(\"").append(javaName).append("\",\"").append(dbName).append("\")");
			if (++index < allColumns.size()) {
				enumConstant.append(",\n\t\t");
			}
		}
		enum1.addEnumConstant(enumConstant.toString());
		
		//java字段
		Field field = new Field();
		field.setVisibility(JavaVisibility.PRIVATE);
		field.setStatic(false);
		field.setType(new FullyQualifiedJavaType("String"));
		field.setName(javaFieldName);
		enum1.addField(field);
		//db字段
		Field field1 = new Field();
		field1.setVisibility(JavaVisibility.PRIVATE);
		field1.setStatic(false);
		field1.setType(new FullyQualifiedJavaType("String"));
		field1.setName(dbFieldName);
		enum1.addField(field1);
		
		//构造器
		Method method = new Method();
		method.setConstructor(true);
		method.setVisibility(JavaVisibility.DEFAULT);
		method.setStatic(false);
		method.setName(enumName);
		method.addParameter(new Parameter(new FullyQualifiedJavaType("String"), javaFieldName));
		method.addParameter(new Parameter(new FullyQualifiedJavaType("String"), dbFieldName));
		method.addBodyLine("this."+javaFieldName+" = "+javaFieldName+";");
		method.addBodyLine("this."+dbFieldName+" = "+dbFieldName+";");
		enum1.addMethod(method);
		
		
		//方法
		Method getMethod = new Method();
		getMethod.setConstructor(false);
		getMethod.setVisibility(JavaVisibility.PUBLIC);
		getMethod.setStatic(false);
		getMethod.setName(javaFieldName);
		getMethod.addBodyLine("return "+javaFieldName+";");
		getMethod.setReturnType(new FullyQualifiedJavaType("String"));
		enum1.addMethod(getMethod);
		
		Method getMethod1 = new Method();
		getMethod1.setConstructor(false);
		getMethod1.setVisibility(JavaVisibility.PUBLIC);
		getMethod1.setStatic(false);
		getMethod1.setName(dbFieldName);
		getMethod1.addBodyLine("return "+dbFieldName+";");
		getMethod1.setReturnType(new FullyQualifiedJavaType("String"));
		enum1.addMethod(getMethod1);
		
		topLevelClass.addInnerEnum(enum1);
	}
}
 
Example 16
Source File: DatabaseIntrospector.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
private void calculateExtraColumnInformation(TableConfiguration tc,
        Map<ActualTableName, List<IntrospectedColumn>> columns) {
    StringBuilder sb = new StringBuilder();
    Pattern pattern = null;
    String replaceString = null;
    if (tc.getColumnRenamingRule() != null) {
        pattern = Pattern.compile(tc.getColumnRenamingRule()
                .getSearchString());
        replaceString = tc.getColumnRenamingRule().getReplaceString();
        replaceString = replaceString == null ? "" : replaceString; //$NON-NLS-1$
    }

    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns
            .entrySet()) {
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            String calculatedColumnName;
            if (pattern == null) {
                calculatedColumnName = introspectedColumn
                        .getActualColumnName();
            } else {
                Matcher matcher = pattern.matcher(introspectedColumn
                        .getActualColumnName());
                calculatedColumnName = matcher.replaceAll(replaceString);
            }

            if (isTrue(tc
                    .getProperty(PropertyRegistry.TABLE_USE_ACTUAL_COLUMN_NAMES))) {
                introspectedColumn.setJavaProperty(
                        getValidPropertyName(calculatedColumnName));
            } else if (isTrue(tc
                            .getProperty(PropertyRegistry.TABLE_USE_COMPOUND_PROPERTY_NAMES))) {
                sb.setLength(0);
                sb.append(calculatedColumnName);
                sb.append('_');
                sb.append(getCamelCaseString(
                        introspectedColumn.getRemarks(), true));
                introspectedColumn.setJavaProperty(
                        getValidPropertyName(sb.toString()));
            } else {
                introspectedColumn.setJavaProperty(
                        getCamelCaseString(calculatedColumnName, false));
            }

            FullyQualifiedJavaType fullyQualifiedJavaType = javaTypeResolver
                    .calculateJavaType(introspectedColumn);

            if (fullyQualifiedJavaType != null) {
                introspectedColumn
                        .setFullyQualifiedJavaType(fullyQualifiedJavaType);
                introspectedColumn.setJdbcTypeName(javaTypeResolver
                        .calculateJdbcTypeName(introspectedColumn));
            } else {
                // type cannot be resolved. Check for ignored or overridden
                boolean warn = true;
                if (tc.isColumnIgnored(introspectedColumn
                        .getActualColumnName())) {
                    warn = false;
                }

                ColumnOverride co = tc.getColumnOverride(introspectedColumn
                        .getActualColumnName());
                if (co != null) {
                    if (stringHasValue(co.getJavaType())
                            && stringHasValue(co.getJavaType())) {
                        warn = false;
                    }
                }

                // if the type is not supported, then we'll report a warning
                if (warn) {
                    introspectedColumn
                            .setFullyQualifiedJavaType(FullyQualifiedJavaType
                                    .getObjectInstance());
                    introspectedColumn.setJdbcTypeName("OTHER"); //$NON-NLS-1$

                    String warning = getString("Warning.14", //$NON-NLS-1$
                            Integer.toString(introspectedColumn.getJdbcType()),
                            entry.getKey().toString(),
                            introspectedColumn.getActualColumnName());

                    warnings.add(warning);
                }
            }

            if (context.autoDelimitKeywords()) {
                if (SqlReservedWords.containsWord(introspectedColumn
                        .getActualColumnName())) {
                    introspectedColumn.setColumnNameDelimited(true);
                }
            }

            if (tc.isAllColumnDelimitingEnabled()) {
                introspectedColumn.setColumnNameDelimited(true);
            }
        }
    }
}
 
Example 17
Source File: DatabaseIntrospector.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
private void calculateExtraColumnInformation(TableConfiguration tc,
        Map<ActualTableName, List<IntrospectedColumn>> columns) {
    StringBuilder sb = new StringBuilder();
    Pattern pattern = null;
    String replaceString = null;
    if (tc.getColumnRenamingRule() != null) {
        pattern = Pattern.compile(tc.getColumnRenamingRule()
                .getSearchString());
        replaceString = tc.getColumnRenamingRule().getReplaceString();
        replaceString = replaceString == null ? "" : replaceString;
    }

    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns
            .entrySet()) {
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            String calculatedColumnName;
            if (pattern == null) {
                calculatedColumnName = introspectedColumn
                        .getActualColumnName();
            } else {
                Matcher matcher = pattern.matcher(introspectedColumn
                        .getActualColumnName());
                calculatedColumnName = matcher.replaceAll(replaceString);
            }

            if (isTrue(tc
                    .getProperty(PropertyRegistry.TABLE_USE_ACTUAL_COLUMN_NAMES))) {
                introspectedColumn.setJavaProperty(
                        JavaBeansUtil.getValidPropertyName(calculatedColumnName));
            } else if (isTrue(tc
                            .getProperty(PropertyRegistry.TABLE_USE_COMPOUND_PROPERTY_NAMES))) {
                sb.setLength(0);
                sb.append(calculatedColumnName);
                sb.append('_');
                sb.append(JavaBeansUtil.getCamelCaseString(
                        introspectedColumn.getRemarks(), true));
                introspectedColumn.setJavaProperty(
                        JavaBeansUtil.getValidPropertyName(sb.toString()));
            } else {
                introspectedColumn.setJavaProperty(
                        JavaBeansUtil.getCamelCaseString(calculatedColumnName, false));
            }

            FullyQualifiedJavaType fullyQualifiedJavaType = javaTypeResolver
                    .calculateJavaType(introspectedColumn);

            if (fullyQualifiedJavaType != null) {
                introspectedColumn
                        .setFullyQualifiedJavaType(fullyQualifiedJavaType);
                introspectedColumn.setJdbcTypeName(javaTypeResolver
                        .calculateJdbcTypeName(introspectedColumn));
            } else {
                // type cannot be resolved. Check for ignored or overridden
                boolean warn = true;
                if (tc.isColumnIgnored(introspectedColumn
                        .getActualColumnName())) {
                    warn = false;
                }

                ColumnOverride co = tc.getColumnOverride(introspectedColumn
                        .getActualColumnName());
                if (co != null
                        && stringHasValue(co.getJavaType())) {
                    warn = false;
                }

                // if the type is not supported, then we'll report a warning
                if (warn) {
                    introspectedColumn
                            .setFullyQualifiedJavaType(FullyQualifiedJavaType
                                    .getObjectInstance());
                    introspectedColumn.setJdbcTypeName("OTHER");

                    String warning = getString("Warning.14",
                            Integer.toString(introspectedColumn.getJdbcType()),
                            entry.getKey().toString(),
                            introspectedColumn.getActualColumnName());

                    warnings.add(warning);
                }
            }

            if (context.autoDelimitKeywords()
                    && SqlReservedWords.containsWord(introspectedColumn
                        .getActualColumnName())) {
                introspectedColumn.setColumnNameDelimited(true);
            }

            if (tc.isAllColumnDelimitingEnabled()) {
                introspectedColumn.setColumnNameDelimited(true);
            }
        }
    }
}
 
Example 18
Source File: OracleSupport.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
/**
 * 生成批量插入的动态sql代码
 *
 * @author 吴帅
 * @parameter @param document
 * @parameter @param introspectedTable
 * @createDate 2015年8月9日 下午6:57:43
 */
@Override
public void addBatchInsertXml(Document document, IntrospectedTable introspectedTable) {
    List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
    //获得要自增的列名
    String incrementField = introspectedTable.getTableConfiguration().getProperties().getProperty("incrementField");
    if (incrementField != null) {
        incrementField = incrementField.toUpperCase();
    }

    StringBuilder dbcolumnsName = new StringBuilder();
    StringBuilder javaPropertyAndDbType = new StringBuilder();
    for (IntrospectedColumn introspectedColumn : columns) {
        String columnName = introspectedColumn.getActualColumnName();
        dbcolumnsName.append(columnName + ",");
        if (!columnName.toUpperCase().equals(incrementField)) {// 不设置id
            javaPropertyAndDbType.append("#{item." + introspectedColumn.getJavaProperty() + ",jdbcType=" + introspectedColumn.getJdbcTypeName() + "},");
        }
    }

    XmlElement insertBatchElement = new XmlElement("insert");
    insertBatchElement.addAttribute(new Attribute("id", "insertBatch"));
    insertBatchElement.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
    insertBatchElement.addElement(new TextElement("insert into " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));

    XmlElement trim1Element = new XmlElement("trim");
    trim1Element.addAttribute(new Attribute("prefix", "("));
    trim1Element.addAttribute(new Attribute("suffix", ")"));
    trim1Element.addAttribute(new Attribute("suffixOverrides", ","));
    trim1Element.addElement(new TextElement(dbcolumnsName.toString()));
    insertBatchElement.addElement(trim1Element);

    insertBatchElement.addElement(new TextElement("select " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + "_SEQUENCE.nextval,A.* from("));

    XmlElement foreachElement = new XmlElement("foreach");
    foreachElement.addAttribute(new Attribute("collection", "list"));
    foreachElement.addAttribute(new Attribute("index", "index"));
    foreachElement.addAttribute(new Attribute("item", "item"));
    foreachElement.addAttribute(new Attribute("separator", "UNION ALL"));
    foreachElement.addElement(new TextElement("SELECT"));
    XmlElement trim2Element = new XmlElement("trim");
    trim2Element.addAttribute(new Attribute("suffixOverrides", ","));
    trim2Element.addElement(new TextElement(javaPropertyAndDbType.toString()));
    foreachElement.addElement(trim2Element);
    foreachElement.addElement(new TextElement("from dual"));
    insertBatchElement.addElement(foreachElement);

    insertBatchElement.addElement(new TextElement(") A"));

    document.getRootElement().addElement(insertBatchElement);
}
 
Example 19
Source File: MysqlSupport.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
/**
 * 增加批量插入的xml配置
 *
 * @author 吴帅
 * @parameter @param document
 * @parameter @param introspectedTable
 * @createDate 2015年8月9日 下午6:57:43
 */
@Override
public void addBatchInsertXml(Document document, IntrospectedTable introspectedTable) {
    List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
    //获得要自增的列名
    String incrementField = introspectedTable.getTableConfiguration().getProperties().getProperty("incrementField");
    if (incrementField != null) {
        incrementField = incrementField.toUpperCase();
    }
    StringBuilder dbcolumnsName = new StringBuilder();
    StringBuilder javaPropertyAndDbType = new StringBuilder();
    for (IntrospectedColumn introspectedColumn : columns) {
        String columnName = introspectedColumn.getActualColumnName();
        if (!columnName.toUpperCase().equals(incrementField)) {//不是自增字段的才会出现在批量插入中
            dbcolumnsName.append(columnName + ",");
            javaPropertyAndDbType.append("#{item." + introspectedColumn.getJavaProperty() + ",jdbcType=" + introspectedColumn.getJdbcTypeName() + "},");
        }
    }

    XmlElement insertBatchElement = new XmlElement("insert");
    insertBatchElement.addAttribute(new Attribute("id", "insertBatch"));
    insertBatchElement.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
    insertBatchElement.addElement(new TextElement("insert into " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));

    XmlElement trim1Element = new XmlElement("trim");
    trim1Element.addAttribute(new Attribute("prefix", "("));
    trim1Element.addAttribute(new Attribute("suffix", ")"));
    trim1Element.addAttribute(new Attribute("suffixOverrides", ","));
    trim1Element.addElement(new TextElement(dbcolumnsName.toString()));
    insertBatchElement.addElement(trim1Element);

    insertBatchElement.addElement(new TextElement("values"));

    XmlElement foreachElement = new XmlElement("foreach");
    foreachElement.addAttribute(new Attribute("collection", "list"));
    foreachElement.addAttribute(new Attribute("index", "index"));
    foreachElement.addAttribute(new Attribute("item", "item"));
    foreachElement.addAttribute(new Attribute("separator", ","));
    foreachElement.addElement(new TextElement("("));
    XmlElement trim2Element = new XmlElement("trim");
    trim2Element.addAttribute(new Attribute("suffixOverrides", ","));
    trim2Element.addElement(new TextElement(javaPropertyAndDbType.toString()));
    foreachElement.addElement(trim2Element);
    foreachElement.addElement(new TextElement(")"));
    insertBatchElement.addElement(foreachElement);

    document.getRootElement().addElement(insertBatchElement);
}
 
Example 20
Source File: SqlServerSupport.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
/**
 * 增加批量插入的xml配置
 *
 * @author 吴帅
 * @parameter @param document
 * @parameter @param introspectedTable
 * @createDate 2015年8月9日 下午6:57:43
 */
@Override
public void addBatchInsertXml(Document document, IntrospectedTable introspectedTable) {
    List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
    //获得要自增的列名
    String incrementField = introspectedTable.getTableConfiguration().getProperties().getProperty("incrementField");
    if (incrementField != null) {
        incrementField = incrementField.toUpperCase();
    }
    StringBuilder dbcolumnsName = new StringBuilder();
    StringBuilder javaPropertyAndDbType = new StringBuilder();
    for (IntrospectedColumn introspectedColumn : columns) {
        String columnName = introspectedColumn.getActualColumnName();
        if (!columnName.toUpperCase().equals(incrementField)) {//不是自增字段的才会出现在批量插入中
            dbcolumnsName.append(columnName + ",");
            javaPropertyAndDbType.append("#{item." + introspectedColumn.getJavaProperty() + ",jdbcType=" + introspectedColumn.getJdbcTypeName() + "},");
        }
    }

    XmlElement insertBatchElement = new XmlElement("insert");
    insertBatchElement.addAttribute(new Attribute("id", "insertBatch"));
    insertBatchElement.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
    insertBatchElement.addElement(new TextElement("insert into " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));

    XmlElement trim1Element = new XmlElement("trim");
    trim1Element.addAttribute(new Attribute("prefix", "("));
    trim1Element.addAttribute(new Attribute("suffix", ")"));
    trim1Element.addAttribute(new Attribute("suffixOverrides", ","));
    trim1Element.addElement(new TextElement(dbcolumnsName.toString()));
    insertBatchElement.addElement(trim1Element);

    insertBatchElement.addElement(new TextElement("values"));

    XmlElement foreachElement = new XmlElement("foreach");
    foreachElement.addAttribute(new Attribute("collection", "list"));
    foreachElement.addAttribute(new Attribute("index", "index"));
    foreachElement.addAttribute(new Attribute("item", "item"));
    foreachElement.addAttribute(new Attribute("separator", ","));
    foreachElement.addElement(new TextElement("("));
    XmlElement trim2Element = new XmlElement("trim");
    trim2Element.addAttribute(new Attribute("suffixOverrides", ","));
    trim2Element.addElement(new TextElement(javaPropertyAndDbType.toString()));
    foreachElement.addElement(trim2Element);
    foreachElement.addElement(new TextElement(")"));
    insertBatchElement.addElement(foreachElement);

    document.getRootElement().addElement(insertBatchElement);
}