org.mybatis.generator.config.GeneratedKey Java Examples

The following examples show how to use org.mybatis.generator.config.GeneratedKey. 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: DatabaseIntrospector.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private void calculateIdentityColumns(TableConfiguration tc,
        Map<ActualTableName, List<IntrospectedColumn>> columns) {
    GeneratedKey gk = tc.getGeneratedKey();
    if (gk == null) {
        // no generated key, then no identity or sequence columns
        return;
    }

    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns
            .entrySet()) {
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            if (isMatchedColumn(introspectedColumn, gk)) {
                if (gk.isIdentity() || gk.isJdbcStandard()) {
                    introspectedColumn.setIdentity(true);
                    introspectedColumn.setSequenceColumn(false);
                } else {
                    introspectedColumn.setIdentity(false);
                    introspectedColumn.setSequenceColumn(true);
                }
            }
        }
    }
}
 
Example #2
Source File: AbstractXmlElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
/**
 * This method should return an XmlElement for the select key used to
 * automatically generate keys.
 * 
 * @param introspectedColumn
 *            the column related to the select key statement
 * @param generatedKey
 *            the generated key for the current table
 * @return the selectKey element
 */
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
        GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultType", identityColumnType)); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
    answer.addAttribute(new Attribute("order", //$NON-NLS-1$
            generatedKey.getMyBatis3Order())); 
    
    answer.addElement(new TextElement(generatedKey
                    .getRuntimeSqlStatement()));

    return answer;
}
 
Example #3
Source File: AnnotatedInsertSelectiveMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void addMapperAnnotations(Interface interfaze, Method method) {
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.InsertProvider")); //$NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append("@InsertProvider(type="); //$NON-NLS-1$
    sb.append(fqjt.getShortName());
    sb.append(".class, method=\""); //$NON-NLS-1$
    sb.append(introspectedTable.getInsertSelectiveStatementId());
    sb.append("\")"); //$NON-NLS-1$
    
    method.addAnnotation(sb.toString());

    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyAnnotation(interfaze, method, gk);
    }
}
 
Example #4
Source File: AbstractXmlElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
/**
 * This method should return an XmlElement for the select key used to
 * automatically generate keys.
 * 
 * @param introspectedColumn
 *            the column related to the select key statement
 * @param generatedKey
 *            the generated key for the current table
 * @return the selectKey element
 */
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
        GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultClass", identityColumnType)); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
    if (stringHasValue(generatedKey.getType())) {
        answer.addAttribute(new Attribute("type", generatedKey.getType())); //$NON-NLS-1$  
    }
    answer
            .addElement(new TextElement(generatedKey
                    .getRuntimeSqlStatement()));

    return answer;
}
 
Example #5
Source File: AbstractXmlElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
/**
 * This method should return an XmlElement for the select key used to
 * automatically generate keys.
 * 
 * @param introspectedColumn
 *            the column related to the select key statement
 * @param generatedKey
 *            the generated key for the current table
 * @return the selectKey element
 */
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
        GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultClass", identityColumnType)); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
    if (stringHasValue(generatedKey.getType())) {
        answer.addAttribute(new Attribute("type", generatedKey.getType())); //$NON-NLS-1$  
    }
    answer
            .addElement(new TextElement(generatedKey
                    .getRuntimeSqlStatement()));

    return answer;
}
 
Example #6
Source File: AbstractJavaMapperMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
protected void addGeneratedKeyAnnotation(Method method, GeneratedKey gk) {
    StringBuilder sb = new StringBuilder();
    introspectedTable.getColumn(gk.getColumn()).ifPresent(introspectedColumn -> {
        if (gk.isJdbcStandard()) {
            sb.append("@Options(useGeneratedKeys=true,keyProperty=\"");
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\")");
            method.addAnnotation(sb.toString());
        } else {
            FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
            sb.append("@SelectKey(statement=\"");
            sb.append(gk.getRuntimeSqlStatement());
            sb.append("\", keyProperty=\"");
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\", before=");
            sb.append(gk.isIdentity() ? "false" : "true"); //$NON-NLS-2$
            sb.append(", resultType=");
            sb.append(fqjt.getShortName());
            sb.append(".class)");
            method.addAnnotation(sb.toString());
        }
    });
}
 
Example #7
Source File: AnnotatedInsertSelectiveMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addMapperAnnotations(Interface interfaze, Method method) {
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.InsertProvider")); //$NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append("@InsertProvider(type="); //$NON-NLS-1$
    sb.append(fqjt.getShortName());
    sb.append(".class, method=\""); //$NON-NLS-1$
    sb.append(introspectedTable.getInsertSelectiveStatementId());
    sb.append("\")"); //$NON-NLS-1$
    
    method.addAnnotation(sb.toString());

    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyAnnotation(interfaze, method, gk);
    }
}
 
Example #8
Source File: AbstractXmlElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
/**
 * This method should return an XmlElement for the select key used to
 * automatically generate keys.
 * 
 * @param introspectedColumn
 *            the column related to the select key statement
 * @param generatedKey
 *            the generated key for the current table
 * @return the selectKey element
 */
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
        GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultType", identityColumnType)); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
    answer.addAttribute(new Attribute("order", //$NON-NLS-1$
            generatedKey.getMyBatis3Order())); 
    
    answer.addElement(new TextElement(generatedKey
                    .getRuntimeSqlStatement()));

    return answer;
}
 
Example #9
Source File: AnnotatedInsertSelectiveMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
@Override
public void addMapperAnnotations(Method method) {
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
    StringBuilder sb = new StringBuilder();
    sb.append("@InsertProvider(type=");
    sb.append(fqjt.getShortName());
    sb.append(".class, method=\"");
    sb.append(introspectedTable.getInsertSelectiveStatementId());
    sb.append("\")");

    method.addAnnotation(sb.toString());

    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyAnnotation(method, gk);
    }
}
 
Example #10
Source File: DatabaseIntrospector.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
private void calculateIdentityColumns(TableConfiguration tc,
        Map<ActualTableName, List<IntrospectedColumn>> columns) {
    GeneratedKey gk = tc.getGeneratedKey();
    if (gk == null) {
        // no generated key, then no identity or sequence columns
        return;
    }
    
    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns
            .entrySet()) {
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            if (isMatchedColumn(introspectedColumn, gk)) {
                if (gk.isIdentity() || gk.isJdbcStandard()) {
                    introspectedColumn.setIdentity(true);
                    introspectedColumn.setSequenceColumn(false);
                } else {
                    introspectedColumn.setIdentity(false);
                    introspectedColumn.setSequenceColumn(true);
                }
            }
        }
    }
}
 
Example #11
Source File: AbstractXmlElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
/**
 * This method should return an XmlElement for the select key used to
 * automatically generate keys.
 *
 * @param introspectedColumn the column related to the select key statement
 * @param generatedKey       the generated key for the current table
 * @return the selectKey element
 */
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
                                  GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey");
    answer.addAttribute(new Attribute("resultType", identityColumnType));
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty()));
    answer.addAttribute(new Attribute("order",
            generatedKey.getMyBatis3Order()));

    answer.addElement(new TextElement(generatedKey
            .getRuntimeSqlStatement()));

    return answer;
}
 
Example #12
Source File: AbstractXmlElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
/**
 * -----
 * 插入时返回生成主键
 *
 * @param introspectedTable introspectedTable
 * @param answer            answer
 */
protected void generateKey(IntrospectedTable introspectedTable, XmlElement answer) {
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        introspectedTable.getColumn(gk.getColumn()).ifPresent(introspectedColumn -> {
            // if the column is null, then it's a configuration error. The
            // warning has already been reported
            if (gk.isJdbcStandard()) {
                answer.addAttribute(new Attribute("useGeneratedKeys", "true")); //$NON-NLS-2$
                answer.addAttribute(
                        new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
                answer.addAttribute(
                        new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
            } else {
                answer.addElement(getSelectKey(introspectedColumn, gk));
            }
        });
    }
}
 
Example #13
Source File: BatchInsertPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 6 votes vote down vote up
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
                                  GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey");
    answer.addAttribute(new Attribute("resultType", identityColumnType));
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty()));
    answer.addAttribute(new Attribute("order",
            generatedKey.getMyBatis3Order()));

    answer.addElement(new TextElement(generatedKey
            .getRuntimeSqlStatement()));

    return answer;
}
 
Example #14
Source File: MyBatisGeneratorConfigurationParser.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
private void parseGeneratedKey(TableConfiguration tc, Node node) {
    Properties attributes = parseAttributes(node);

    String column = attributes.getProperty("column"); //$NON-NLS-1$
    boolean identity = isTrue(attributes
            .getProperty("identity")); //$NON-NLS-1$
    String sqlStatement = attributes.getProperty("sqlStatement"); //$NON-NLS-1$
    String type = attributes.getProperty("type"); //$NON-NLS-1$

    GeneratedKey gk = new GeneratedKey(column, sqlStatement, identity, type);

    tc.setGeneratedKey(gk);
}
 
Example #15
Source File: DatabaseIntrospector.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
private void reportIntrospectionWarnings(
        IntrospectedTable introspectedTable,
        TableConfiguration tableConfiguration, FullyQualifiedTable table) {
    // make sure that every column listed in column overrides
    // actually exists in the table
    for (ColumnOverride columnOverride : tableConfiguration
            .getColumnOverrides()) {
        if (!introspectedTable.getColumn(columnOverride.getColumnName()).isPresent()) {
            warnings.add(getString("Warning.3",
                    columnOverride.getColumnName(), table.toString()));
        }
    }

    // make sure that every column listed in ignored columns
    // actually exists in the table
    for (String string : tableConfiguration.getIgnoredColumnsInError()) {
        warnings.add(getString("Warning.4",
                string, table.toString()));
    }

    GeneratedKey generatedKey = tableConfiguration.getGeneratedKey();
    if (generatedKey != null
            && !introspectedTable.getColumn(generatedKey.getColumn()).isPresent()) {
        if (generatedKey.isIdentity()) {
            warnings.add(getString("Warning.5",
                    generatedKey.getColumn(), table.toString()));
        } else {
            warnings.add(getString("Warning.6",
                    generatedKey.getColumn(), table.toString()));
        }
    }

    for (IntrospectedColumn ic : introspectedTable.getAllColumns()) {
        if (JavaReservedWords.containsWord(ic.getJavaProperty())) {
            warnings.add(getString("Warning.26",
                    ic.getActualColumnName(), table.toString()));
        }
    }
}
 
Example #16
Source File: AbstractJavaMapperMethodGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
protected void addGeneratedKeyAnnotation(Interface interfaze, Method method,
        GeneratedKey gk) {
    StringBuilder sb = new StringBuilder();
    IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
    if (introspectedColumn != null) {
        if (gk.isJdbcStandard()) {
            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Options")); //$NON-NLS-1$
            sb.append("@Options(useGeneratedKeys=true,keyProperty=\""); //$NON-NLS-1$
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\")"); //$NON-NLS-1$
            method.addAnnotation(sb.toString());
        } else {
            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectKey")); //$NON-NLS-1$
            FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
            interfaze.addImportedType(fqjt);
            sb.append("@SelectKey(statement=\""); //$NON-NLS-1$
            sb.append(gk.getRuntimeSqlStatement());
            sb.append("\", keyProperty=\""); //$NON-NLS-1$
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\", before="); //$NON-NLS-1$
            sb.append(gk.isIdentity() ? "false" : "true"); //$NON-NLS-1$ //$NON-NLS-2$
            sb.append(", resultType="); //$NON-NLS-1$
            sb.append(fqjt.getShortName());
            sb.append(".class)"); //$NON-NLS-1$
            method.addAnnotation(sb.toString());
        }
    }
}
 
Example #17
Source File: DatabaseIntrospector.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
private void reportIntrospectionWarnings(
        IntrospectedTable introspectedTable,
        TableConfiguration tableConfiguration, FullyQualifiedTable table) {
    // make sure that every column listed in column overrides
    // actually exists in the table
    for (ColumnOverride columnOverride : tableConfiguration
            .getColumnOverrides()) {
        if (introspectedTable.getColumn(columnOverride.getColumnName()) == null) {
            warnings.add(getString("Warning.3", //$NON-NLS-1$
                    columnOverride.getColumnName(), table.toString()));
        }
    }

    // make sure that every column listed in ignored columns
    // actually exists in the table
    for (String string : tableConfiguration.getIgnoredColumnsInError()) {
        warnings.add(getString("Warning.4", //$NON-NLS-1$
                string, table.toString()));
    }

    GeneratedKey generatedKey = tableConfiguration.getGeneratedKey();
    if (generatedKey != null
            && introspectedTable.getColumn(generatedKey.getColumn()) == null) {
        if (generatedKey.isIdentity()) {
            warnings.add(getString("Warning.5", //$NON-NLS-1$
                    generatedKey.getColumn(), table.toString()));
        } else {
            warnings.add(getString("Warning.6", //$NON-NLS-1$
                    generatedKey.getColumn(), table.toString()));
        }
    }
}
 
Example #18
Source File: DatabaseIntrospector.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
private boolean isMatchedColumn(IntrospectedColumn introspectedColumn, GeneratedKey gk) {
    if (introspectedColumn.isColumnNameDelimited()) {
        return introspectedColumn.getActualColumnName().equals(gk.getColumn());
    } else {
        return introspectedColumn.getActualColumnName().equalsIgnoreCase(gk.getColumn());
    }
}
 
Example #19
Source File: IbatorConfigurationParser.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
private void parseGeneratedKey(TableConfiguration tc, Node node) {
    Properties attributes = parseAttributes(node);

    String column = attributes.getProperty("column"); //$NON-NLS-1$
    boolean identity = isTrue(attributes
            .getProperty("identity")); //$NON-NLS-1$
    String sqlStatement = attributes.getProperty("sqlStatement"); //$NON-NLS-1$
    String type = attributes.getProperty("type"); //$NON-NLS-1$

    GeneratedKey gk = new GeneratedKey(column, sqlStatement, identity, type);

    tc.setGeneratedKey(gk);
}
 
Example #20
Source File: MyBatisGeneratorConfigurationParser.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
private void parseGeneratedKey(TableConfiguration tc, Node node) {
    Properties attributes = parseAttributes(node);

    String column = attributes.getProperty("column"); //$NON-NLS-1$
    boolean identity = isTrue(attributes
            .getProperty("identity")); //$NON-NLS-1$
    String sqlStatement = attributes.getProperty("sqlStatement"); //$NON-NLS-1$
    String type = attributes.getProperty("type"); //$NON-NLS-1$

    GeneratedKey gk = new GeneratedKey(column, sqlStatement, identity, type);

    tc.setGeneratedKey(gk);
}
 
Example #21
Source File: AbstractJavaMapperMethodGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
protected void addGeneratedKeyAnnotation(Interface interfaze, Method method,
        GeneratedKey gk) {
    StringBuilder sb = new StringBuilder();
    IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
    if (introspectedColumn != null) {
        if (gk.isJdbcStandard()) {
            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Options")); //$NON-NLS-1$
            sb.append("@Options(useGeneratedKeys=true,keyProperty=\""); //$NON-NLS-1$
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\")"); //$NON-NLS-1$
            method.addAnnotation(sb.toString());
        } else {
            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectKey")); //$NON-NLS-1$
            FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
            interfaze.addImportedType(fqjt);
            sb.append("@SelectKey(statement=\""); //$NON-NLS-1$
            sb.append(gk.getRuntimeSqlStatement());
            sb.append("\", keyProperty=\""); //$NON-NLS-1$
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\", before="); //$NON-NLS-1$
            sb.append(gk.isIdentity() ? "false" : "true"); //$NON-NLS-1$ //$NON-NLS-2$
            sb.append(", resultType="); //$NON-NLS-1$
            sb.append(fqjt.getShortName());
            sb.append(".class)"); //$NON-NLS-1$
            method.addAnnotation(sb.toString());
        }
    }
}
 
Example #22
Source File: BatchInsertPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
  String identityColumnType = introspectedColumn
      .getFullyQualifiedJavaType().getFullyQualifiedName();

  XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
  answer.addAttribute(new Attribute("resultType", identityColumnType)); //$NON-NLS-1$
  answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
  answer.addAttribute(new Attribute("order", //$NON-NLS-1$
      generatedKey.getMyBatis3Order()));

  answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement()));

  return answer;
}
 
Example #23
Source File: IbatorConfigurationParser.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
private void parseGeneratedKey(TableConfiguration tc, Node node) {
    Properties attributes = parseAttributes(node);

    String column = attributes.getProperty("column"); //$NON-NLS-1$
    boolean identity = isTrue(attributes
            .getProperty("identity")); //$NON-NLS-1$
    String sqlStatement = attributes.getProperty("sqlStatement"); //$NON-NLS-1$
    String type = attributes.getProperty("type"); //$NON-NLS-1$

    GeneratedKey gk = new GeneratedKey(column, sqlStatement, identity, type);

    tc.setGeneratedKey(gk);
}
 
Example #24
Source File: SelectiveEnhancedPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * insertSelective
 * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
 * @param element
 * @param introspectedTable
 * @return
 */
@Override
public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
    // 清空
    XmlElement answer = new XmlElement("insert");
    answer.addAttribute(new Attribute("id", introspectedTable.getInsertSelectiveStatementId()));
    answer.addAttribute(new Attribute("parameterType", "map"));

    commentGenerator.addComment(answer);

    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // if the column is null, then it's a configuration error. The
        // warning has already been reported
        if (introspectedColumn != null) {
            if (gk.isJdbcStandard()) {
                XmlElementGeneratorTools.useGeneratedKeys(answer, introspectedTable, "record.");
            } else {
                answer.addElement(XmlElementGeneratorTools.getSelectKey(introspectedColumn, gk, "record."));
            }
        }
    }

    StringBuilder sb = new StringBuilder();

    sb.append("insert into ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));

    // columns
    answer.addElement(this.generateInsertColumnSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())));
    // values
    answer.addElement(new TextElement("values"));
    answer.addElement(this.generateInsertValuesSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())));

    XmlElementTools.replaceXmlElement(element, answer);

    return super.sqlMapInsertSelectiveElementGenerated(element, introspectedTable);
}
 
Example #25
Source File: DatabaseIntrospector.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
private boolean isMatchedColumn(IntrospectedColumn introspectedColumn, GeneratedKey gk) {
    if (introspectedColumn.isColumnNameDelimited()) {
        return introspectedColumn.getActualColumnName().equals(gk.getColumn());
    } else {
        return introspectedColumn.getActualColumnName().equalsIgnoreCase(gk.getColumn());
    }
}
 
Example #26
Source File: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
 * @param element
 * @param introspectedTable
 * @param prefix
 */
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix) {
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, gk.getColumn());
        // if the column is null, then it's a configuration error. The
        // warning has already been reported
        if (introspectedColumn != null) {
            // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
            element.addAttribute(new Attribute("useGeneratedKeys", "true"));
            element.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
            element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
        }
    }
}
 
Example #27
Source File: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey, String prefix) {
    String identityColumnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey");
    answer.addAttribute(new Attribute("resultType", identityColumnType));
    answer.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
    answer.addAttribute(new Attribute("order", generatedKey.getMyBatis3Order()));

    answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement()));

    return answer;
}
 
Example #28
Source File: FragmentGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
public MethodParts getGeneratedKeyAnnotation(GeneratedKey gk) {
    MethodParts.Builder builder = new MethodParts.Builder();
    
    StringBuilder sb = new StringBuilder();
    introspectedTable.getColumn(gk.getColumn()).ifPresent(introspectedColumn -> {
        if (gk.isJdbcStandard()) {
            builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Options"));
            sb.append("@Options(useGeneratedKeys=true,keyProperty=\"record.");
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\")");
            builder.withAnnotation(sb.toString());
        } else {
            builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectKey"));
            FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
            sb.append("@SelectKey(statement=\"");
            sb.append(gk.getRuntimeSqlStatement());
            sb.append("\", keyProperty=\"record.");
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\", before=");
            sb.append(gk.isIdentity() ? "false" : "true"); //$NON-NLS-2$
            sb.append(", resultType=");
            sb.append(fqjt.getShortName());
            sb.append(".class)");
            builder.withAnnotation(sb.toString());
        }
    });
    
    return builder.build();
}
 
Example #29
Source File: AnnotatedInsertSelectiveMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addExtraImports(Interface interfaze) {
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyImports(interfaze, gk);
    }
    interfaze.addImportedType(
            new FullyQualifiedJavaType("org.apache.ibatis.annotations.InsertProvider"));
}
 
Example #30
Source File: AnnotatedInsertMethodGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addExtraImports(Interface interfaze) {
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyImports(interfaze, gk);
    }
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert"));
}