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

The following examples show how to use org.mybatis.generator.api.IntrospectedColumn#isIdentity() . 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: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 是否存在自增或者生成的column
 * @param columns
 * @return
 */
private static boolean hasIdentityAndGeneratedAlwaysColumns(List<IntrospectedColumn> columns) {
    for (IntrospectedColumn ic : columns) {
        if (ic.isGeneratedAlways() || ic.isIdentity()) {
            return true;
        }
    }
    return false;
}
 
Example 2
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 3
Source File: OracleBatchUpdatePlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 4 votes vote down vote up
public void addSqlMapper(Document document, IntrospectedTable introspectedTable) {
    String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
    List<IntrospectedColumn> columnList = introspectedTable.getAllColumns();
    //primaryKey的JDBC名字
    String primaryKeyName = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();
    //primaryKey的JAVA名字
    String primaryKeyJavaName = introspectedTable.getPrimaryKeyColumns().get(0).getJavaProperty();

    String keyParameterClause = MyBatis3FormattingUtilities.getParameterClause(introspectedTable.getPrimaryKeyColumns().get(0), "item.");
    XmlElement baseElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.UPDATE,
            BATCH_UPDATE,
            FullyQualifiedJavaType.getNewListInstance());

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

    foreachElement.addElement(new TextElement( String.format("update %s", tableName)));

    XmlElement setElement = new XmlElement("set");

    StringBuilder columnInfo = new StringBuilder();
    StringBuilder valuesInfo = new StringBuilder();

    for (int i = 0; i < columnList.size(); i++) {

        IntrospectedColumn introspectedColumn = columnList.get(i);
        if (introspectedColumn.isIdentity()) {
            continue;
        }
        columnInfo.append(introspectedColumn.getActualColumnName());
        valuesInfo.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item."));
        if (i != (columnList.size() - 1)) {
            valuesInfo.append(",");
        }
        String setSql = String.format(" %s = %s" ,columnInfo,valuesInfo);

        setElement.addElement(new TextElement(setSql));

        valuesInfo.delete(0, valuesInfo.length());
        columnInfo.delete(0, columnInfo.length());

    }

    foreachElement.addElement(setElement);

    String whereSql = String.format("where %s = %s",primaryKeyName,keyParameterClause);

    foreachElement.addElement(new TextElement(whereSql));

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

    baseElement.addElement(foreachElement);

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

    //3.parent Add
    document.getRootElement().addElement(baseElement);
}
 
Example 4
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 5
Source File: PostgreBatchUpdatePlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 4 votes vote down vote up
private void addSqlMapper(Document document, IntrospectedTable introspectedTable){
    String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
    List<IntrospectedColumn> columnList = introspectedTable.getAllColumns();

    String primaryKeyName = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();
    //primaryKey的JAVA名字
    String primaryKeyJavaName = introspectedTable.getPrimaryKeyColumns().get(0).getJavaProperty();

    XmlElement updateElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.UPDATE,
            BATCH_UPDATE,
            FullyQualifiedJavaType.getNewListInstance());

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

    String baseSql = String.format("update %s", tableName);

    updateElement.addElement(new TextElement(baseSql));

    XmlElement setElement = new XmlElement("set");


    StringBuilder columnInfo = new StringBuilder();
    StringBuilder valuesInfo = new StringBuilder();

    StringBuilder columnInfoTotal = new StringBuilder();
    for (int i = 0; i < columnList.size(); i++) {

        IntrospectedColumn introspectedColumn = columnList.get(i);

        columnInfo.append(introspectedColumn.getActualColumnName());
        columnInfoTotal.append(introspectedColumn.getActualColumnName());
        if (introspectedColumn.getFullyQualifiedJavaType().equals(FullyQualifiedJavaType.getDateInstance())){
            valuesInfo.append("to_timestamp(");
            valuesInfo.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item."));
            valuesInfo.append(",'yyyy-MM-dd hh24:mi:ss')");
        }else {
            valuesInfo.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item."));
        }

        String setSql = String.format(" %s = %s," ,columnInfo,"temp."+columnInfo);
        if (i == (columnList.size() - 1)) {
            setSql = setSql.substring(0, setSql.length() - 1);
        }
        if (!introspectedColumn.isIdentity() && !introspectedColumn.getActualColumnName().equals(primaryKeyName)) {
            setElement.addElement(new TextElement(setSql));
        }

        if (i != (columnList.size() - 1)) {
            valuesInfo.append(",");
            columnInfo.append(",");
            columnInfoTotal.append(",");
        }

        columnInfo.delete(0, valuesInfo.length());
    }

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

    foreachElement.addElement(new TextElement(valuesInfo.toString()));

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

    updateElement.addElement(setElement);

    updateElement.addElement(new TextElement("from (values"));

    updateElement.addElement(foreachElement);

    updateElement.addElement(new TextElement(String.format(") as temp (%s) where %s.%s=temp.%s;",columnInfoTotal,tableName,primaryKeyName,primaryKeyName)));

    //3.parent Add
    document.getRootElement().addElement(updateElement);
}
 
Example 6
Source File: ProviderInsertSelectiveMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    
    if (useLegacyBuilder) {
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO"); //$NON-NLS-1$
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
    	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.VALUES"); //$NON-NLS-1$
    } else {
    	importedTypes.add(NEW_BUILDER_IMPORT);
    }

    FullyQualifiedJavaType fqjt = introspectedTable.getRules()
        .calculateAllFieldsClass();
    importedTypes.add(fqjt);

    Method method = new Method(
            introspectedTable.getInsertSelectiveStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addParameter(new Parameter(fqjt, "record")); //$NON-NLS-1$
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    if (useLegacyBuilder) {
    	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
    } else {
    	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
    }

	method.addBodyLine(String.format("%sINSERT_INTO(\"%s\");", //$NON-NLS-1$
            builderPrefix,
			escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())));
	
    for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }
        
        method.addBodyLine(""); //$NON-NLS-1$
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
                && !introspectedColumn.isSequenceColumn()) {
            method.addBodyLine(String.format("if (record.%s() != null) {", //$NON-NLS-1$
                getGetterMethodName(introspectedColumn.getJavaProperty(),
                        introspectedColumn.getFullyQualifiedJavaType())));
        }
      	method.addBodyLine(String.format("%sVALUES(\"%s\", \"%s\");", //$NON-NLS-1$
      			builderPrefix,
      			escapeStringForJava(getEscapedColumnName(introspectedColumn)),
                getParameterClause(introspectedColumn)));

      	if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
                && !introspectedColumn.isSequenceColumn()) {
            method.addBodyLine("}"); //$NON-NLS-1$
        }
    }
    
    method.addBodyLine(""); //$NON-NLS-1$
    if (useLegacyBuilder) {
    	method.addBodyLine("return SQL();"); //$NON-NLS-1$
    } else {
    	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
    }
    
    if (context.getPlugins().providerInsertSelectiveMethodGenerated(method, topLevelClass,
            introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 7
Source File: AnnotatedInsertMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addMapperAnnotations(Interface interfaze, Method method) {
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert")); //$NON-NLS-1$
    
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    
    method.addAnnotation("@Insert({"); //$NON-NLS-1$
    StringBuilder insertClause = new StringBuilder();
    StringBuilder valuesClause = new StringBuilder();
    
    javaIndent(insertClause, 1);
    javaIndent(valuesClause, 1);

    insertClause.append("\"insert into "); //$NON-NLS-1$
    insertClause.append(escapeStringForJava(introspectedTable
            .getFullyQualifiedTableNameAtRuntime()));
    insertClause.append(" ("); //$NON-NLS-1$

    valuesClause.append("\"values ("); //$NON-NLS-1$

    List<String> valuesClauses = new ArrayList<String>();
    Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns()
            .iterator();
    boolean hasFields = false;
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }

        insertClause.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
        valuesClause.append(getParameterClause(introspectedColumn));
        hasFields = true;
        if (iter.hasNext()) {
            insertClause.append(", "); //$NON-NLS-1$
            valuesClause.append(", "); //$NON-NLS-1$
        }

        if (valuesClause.length() > 60) {
            if (!iter.hasNext()) {
                insertClause.append(')');
                valuesClause.append(')');
            }
            insertClause.append("\","); //$NON-NLS-1$
            valuesClause.append('\"');
            if (iter.hasNext()) {
                valuesClause.append(',');
            }
            
            method.addAnnotation(insertClause.toString());
            insertClause.setLength(0);
            javaIndent(insertClause, 1);
            insertClause.append('\"');
            
            valuesClauses.add(valuesClause.toString());
            valuesClause.setLength(0);
            javaIndent(valuesClause, 1);
            valuesClause.append('\"');
            hasFields = false;
        }
    }
    
    if (hasFields) {
        insertClause.append(")\","); //$NON-NLS-1$
        method.addAnnotation(insertClause.toString());

        valuesClause.append(")\""); //$NON-NLS-1$
        valuesClauses.add(valuesClause.toString());
    }

    for (String clause : valuesClauses) {
        method.addAnnotation(clause);
    }
    
    method.addAnnotation("})"); //$NON-NLS-1$

    if (gk != null) {
        addGeneratedKeyAnnotation(interfaze, method, gk);
    }
}
 
Example 8
Source File: ProviderInsertSelectiveMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
    staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.VALUES"); //$NON-NLS-1$

    FullyQualifiedJavaType fqjt = introspectedTable.getRules()
        .calculateAllFieldsClass();
    importedTypes.add(fqjt);

    Method method = new Method(
            introspectedTable.getInsertSelectiveStatementId());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.addParameter(new Parameter(fqjt, "record")); //$NON-NLS-1$
    
    context.getCommentGenerator().addGeneralMethodComment(method,
            introspectedTable);

    method.addBodyLine("BEGIN();"); //$NON-NLS-1$
    method.addBodyLine(String.format("INSERT_INTO(\"%s\");", //$NON-NLS-1$
            escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())));

    for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }
        
        method.addBodyLine(""); //$NON-NLS-1$
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
                && !introspectedColumn.isSequenceColumn()) {
            method.addBodyLine(String.format("if (record.%s() != null) {", //$NON-NLS-1$
                getGetterMethodName(introspectedColumn.getJavaProperty(),
                        introspectedColumn.getFullyQualifiedJavaType())));
        }
        method.addBodyLine(String.format("VALUES(\"%s\", \"%s\");", //$NON-NLS-1$
                escapeStringForJava(getEscapedColumnName(introspectedColumn)),
                getParameterClause(introspectedColumn)));
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
                && !introspectedColumn.isSequenceColumn()) {
            method.addBodyLine("}"); //$NON-NLS-1$
        }
    }
    
    method.addBodyLine(""); //$NON-NLS-1$
    method.addBodyLine("return SQL();"); //$NON-NLS-1$
    
    if (context.getPlugins().providerInsertSelectiveMethodGenerated(method, topLevelClass,
            introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
 
Example 9
Source File: AnnotatedInsertMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
@Override
public void addMapperAnnotations(Interface interfaze, Method method) {
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert")); //$NON-NLS-1$
    
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    
    method.addAnnotation("@Insert({"); //$NON-NLS-1$
    StringBuilder insertClause = new StringBuilder();
    StringBuilder valuesClause = new StringBuilder();
    
    javaIndent(insertClause, 1);
    javaIndent(valuesClause, 1);

    insertClause.append("\"insert into "); //$NON-NLS-1$
    insertClause.append(escapeStringForJava(introspectedTable
            .getFullyQualifiedTableNameAtRuntime()));
    insertClause.append(" ("); //$NON-NLS-1$

    valuesClause.append("\"values ("); //$NON-NLS-1$

    List<String> valuesClauses = new ArrayList<String>();
    Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns()
            .iterator();
    boolean hasFields = false;
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }

        insertClause.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
        valuesClause.append(getParameterClause(introspectedColumn));
        hasFields = true;
        if (iter.hasNext()) {
            insertClause.append(", "); //$NON-NLS-1$
            valuesClause.append(", "); //$NON-NLS-1$
        }

        if (valuesClause.length() > 60) {
            if (!iter.hasNext()) {
                insertClause.append(')');
                valuesClause.append(')');
            }
            insertClause.append("\","); //$NON-NLS-1$
            valuesClause.append('\"');
            if (iter.hasNext()) {
                valuesClause.append(',');
            }
            
            method.addAnnotation(insertClause.toString());
            insertClause.setLength(0);
            javaIndent(insertClause, 1);
            insertClause.append('\"');
            
            valuesClauses.add(valuesClause.toString());
            valuesClause.setLength(0);
            javaIndent(valuesClause, 1);
            valuesClause.append('\"');
            hasFields = false;
        }
    }
    
    if (hasFields) {
        insertClause.append(")\","); //$NON-NLS-1$
        method.addAnnotation(insertClause.toString());

        valuesClause.append(")\""); //$NON-NLS-1$
        valuesClauses.add(valuesClause.toString());
    }

    for (String clause : valuesClauses) {
        method.addAnnotation(clause);
    }
    
    method.addAnnotation("})"); //$NON-NLS-1$

    if (gk != null) {
        addGeneratedKeyAnnotation(interfaze, method, gk);
    }
}