Java Code Examples for org.mybatis.generator.api.IntrospectedTable#getGeneratedKey()

The following examples show how to use org.mybatis.generator.api.IntrospectedTable#getGeneratedKey() . 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: 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 2
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 3
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);
}