Search in sources :

Example 6 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey in project generator by mybatis.

the class AnnotatedInsertMethodGenerator method addMapperAnnotations.

@Override
public void addMapperAnnotations(Method method) {
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    //$NON-NLS-1$
    method.addAnnotation("@Insert({");
    StringBuilder insertClause = new StringBuilder();
    StringBuilder valuesClause = new StringBuilder();
    javaIndent(insertClause, 1);
    javaIndent(valuesClause, 1);
    //$NON-NLS-1$
    insertClause.append("\"insert into ");
    insertClause.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime()));
    //$NON-NLS-1$
    insertClause.append(" (");
    //$NON-NLS-1$
    valuesClause.append("\"values (");
    List<String> valuesClauses = new ArrayList<String>();
    Iterator<IntrospectedColumn> iter = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()).iterator();
    boolean hasFields = false;
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        insertClause.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
        valuesClause.append(getParameterClause(introspectedColumn));
        hasFields = true;
        if (iter.hasNext()) {
            //$NON-NLS-1$
            insertClause.append(", ");
            //$NON-NLS-1$
            valuesClause.append(", ");
        }
        if (valuesClause.length() > 60) {
            if (!iter.hasNext()) {
                insertClause.append(')');
                valuesClause.append(')');
            }
            //$NON-NLS-1$
            insertClause.append("\",");
            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) {
        //$NON-NLS-1$
        insertClause.append(")\",");
        method.addAnnotation(insertClause.toString());
        //$NON-NLS-1$
        valuesClause.append(")\"");
        valuesClauses.add(valuesClause.toString());
    }
    for (String clause : valuesClauses) {
        method.addAnnotation(clause);
    }
    //$NON-NLS-1$
    method.addAnnotation("})");
    if (gk != null) {
        addGeneratedKeyAnnotation(method, gk);
    }
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) ArrayList(java.util.ArrayList)

Example 7 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey in project generator by mybatis.

the class AnnotatedInsertSelectiveMethodGenerator method addMapperAnnotations.

@Override
public void addMapperAnnotations(Method method) {
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("@InsertProvider(type=");
    sb.append(fqjt.getShortName());
    //$NON-NLS-1$
    sb.append(".class, method=\"");
    sb.append(introspectedTable.getInsertSelectiveStatementId());
    //$NON-NLS-1$
    sb.append("\")");
    method.addAnnotation(sb.toString());
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyAnnotation(method, gk);
    }
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 8 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey in project generator by mybatis.

the class AnnotatedInsertSelectiveMethodGenerator method addExtraImports.

@Override
public void addExtraImports(Interface interfaze) {
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyImports(interfaze, gk);
    }
    //$NON-NLS-1$
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.InsertProvider"));
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 9 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey in project generator by mybatis.

the class MyBatisGeneratorConfigurationParser method parseGeneratedKey.

private void parseGeneratedKey(TableConfiguration tc, Node node) {
    Properties attributes = parseAttributes(node);
    //$NON-NLS-1$
    String column = attributes.getProperty("column");
    boolean identity = isTrue(attributes.getProperty(//$NON-NLS-1$
    "identity"));
    //$NON-NLS-1$
    String sqlStatement = attributes.getProperty("sqlStatement");
    //$NON-NLS-1$
    String type = attributes.getProperty("type");
    GeneratedKey gk = new GeneratedKey(column, sqlStatement, identity, type);
    tc.setGeneratedKey(gk);
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Properties(java.util.Properties)

Example 10 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey in project generator by mybatis.

the class InsertElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("insert");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getInsertStatementId()));
    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterClass", parameterType.getFullyQualifiedName()));
    context.getCommentGenerator().addComment(answer);
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            // pre-generated key
            answer.addElement(getSelectKey(introspectedColumn, gk));
        }
    }
    StringBuilder insertClause = new StringBuilder();
    StringBuilder valuesClause = new StringBuilder();
    //$NON-NLS-1$
    insertClause.append("insert into ");
    insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    //$NON-NLS-1$
    insertClause.append(" (");
    //$NON-NLS-1$
    valuesClause.append("values (");
    List<String> valuesClauses = new ArrayList<String>();
    Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }
        insertClause.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
        valuesClause.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
        if (iter.hasNext()) {
            //$NON-NLS-1$
            insertClause.append(", ");
            //$NON-NLS-1$
            valuesClause.append(", ");
        }
        if (valuesClause.length() > 80) {
            answer.addElement(new TextElement(insertClause.toString()));
            insertClause.setLength(0);
            OutputUtilities.xmlIndent(insertClause, 1);
            valuesClauses.add(valuesClause.toString());
            valuesClause.setLength(0);
            OutputUtilities.xmlIndent(valuesClause, 1);
        }
    }
    insertClause.append(')');
    answer.addElement(new TextElement(insertClause.toString()));
    valuesClause.append(')');
    valuesClauses.add(valuesClause.toString());
    for (String clause : valuesClauses) {
        answer.addElement(new TextElement(clause));
    }
    if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            // pre-generated key
            answer.addElement(getSelectKey(introspectedColumn, gk));
        }
    }
    if (context.getPlugins().sqlMapInsertElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Aggregations

GeneratedKey (org.mybatis.generator.config.GeneratedKey)10 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)7 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)6 ArrayList (java.util.ArrayList)4 Attribute (org.mybatis.generator.api.dom.xml.Attribute)4 TextElement (org.mybatis.generator.api.dom.xml.TextElement)4 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)4 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)1