Search in sources :

Example 11 with FullyQualifiedJavaType

use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType in project generator by mybatis.

the class AnnotatedInsertMethodGenerator 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.Insert"));
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 12 with FullyQualifiedJavaType

use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType in project generator by mybatis.

the class InsertSelectiveElementGenerator 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.getInsertSelectiveStatementId()));
    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 sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("insert into ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    //$NON-NLS-1$
    XmlElement insertElement = new XmlElement("dynamic");
    //$NON-NLS-1$ //$NON-NLS-2$
    insertElement.addAttribute(new Attribute("prepend", "("));
    answer.addElement(insertElement);
    //$NON-NLS-1$
    answer.addElement(new TextElement("values"));
    //$NON-NLS-1$
    XmlElement valuesElement = new XmlElement("dynamic");
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesElement.addAttribute(new Attribute("prepend", "("));
    answer.addElement(valuesElement);
    for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }
        //$NON-NLS-1$
        XmlElement insertNotNullElement = new XmlElement("isNotNull");
        //$NON-NLS-1$ //$NON-NLS-2$
        insertNotNullElement.addAttribute(new Attribute("prepend", ","));
        insertNotNullElement.addAttribute(new Attribute("property", //$NON-NLS-1$
        introspectedColumn.getJavaProperty()));
        insertNotNullElement.addElement(new TextElement(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn)));
        insertElement.addElement(insertNotNullElement);
        //$NON-NLS-1$
        XmlElement valuesNotNullElement = new XmlElement("isNotNull");
        //$NON-NLS-1$ //$NON-NLS-2$
        valuesNotNullElement.addAttribute(new Attribute("prepend", ","));
        valuesNotNullElement.addAttribute(new Attribute("property", //$NON-NLS-1$
        introspectedColumn.getJavaProperty()));
        valuesNotNullElement.addElement(new TextElement(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn)));
        valuesElement.addElement(valuesNotNullElement);
    }
    //$NON-NLS-1$
    insertElement.addElement(new TextElement(")"));
    //$NON-NLS-1$
    valuesElement.addElement(new TextElement(")"));
    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().sqlMapInsertSelectiveElementGenerated(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) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 13 with FullyQualifiedJavaType

use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType in project generator by mybatis.

the class InsertSelectiveElementGenerator 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.getInsertSelectiveStatementId()));
    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterType", parameterType.getFullyQualifiedName()));
    context.getCommentGenerator().addComment(answer);
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            if (gk.isJdbcStandard()) {
                //$NON-NLS-1$ //$NON-NLS-2$
                answer.addAttribute(new Attribute("useGeneratedKeys", "true"));
                //$NON-NLS-1$
                answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
                //$NON-NLS-1$
                answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
            } else {
                answer.addElement(getSelectKey(introspectedColumn, gk));
            }
        }
    }
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("insert into ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    //$NON-NLS-1$
    XmlElement insertTrimElement = new XmlElement("trim");
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("prefix", "("));
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("suffix", ")"));
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
    answer.addElement(insertTrimElement);
    //$NON-NLS-1$
    XmlElement valuesTrimElement = new XmlElement("trim");
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("prefix", "values ("));
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("suffix", ")"));
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
    answer.addElement(valuesTrimElement);
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
        if (introspectedColumn.isSequenceColumn() || introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            // if it is a sequence column, it is not optional
            // This is required for MyBatis3 because MyBatis3 parses
            // and calculates the SQL before executing the selectKey
            // if it is primitive, we cannot do a null check
            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
            sb.append(',');
            insertTrimElement.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
            sb.append(',');
            valuesTrimElement.addElement(new TextElement(sb.toString()));
            continue;
        }
        //$NON-NLS-1$
        XmlElement insertNotNullElement = new XmlElement("if");
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" != null");
        insertNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
        sb.toString()));
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        sb.append(',');
        insertNotNullElement.addElement(new TextElement(sb.toString()));
        insertTrimElement.addElement(insertNotNullElement);
        //$NON-NLS-1$
        XmlElement valuesNotNullElement = new XmlElement("if");
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" != null");
        valuesNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
        sb.toString()));
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        sb.append(',');
        valuesNotNullElement.addElement(new TextElement(sb.toString()));
        valuesTrimElement.addElement(valuesNotNullElement);
    }
    if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(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) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 14 with FullyQualifiedJavaType

use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType in project generator by mybatis.

the class AnnotatedSelectAllMethodGenerator method addExtraImports.

@Override
public void addExtraImports(Interface interfaze) {
    //$NON-NLS-1$
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.type.JdbcType"));
    if (introspectedTable.isConstructorBased()) {
        //$NON-NLS-1$
        interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Arg"));
        //$NON-NLS-1$
        interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ConstructorArgs"));
    } else {
        //$NON-NLS-1$
        interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Result"));
        //$NON-NLS-1$
        interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Results"));
    }
}
Also used : FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 15 with FullyQualifiedJavaType

use of org.mybatis.generator.api.dom.java.FullyQualifiedJavaType in project generator by mybatis.

the class AnnotatedSelectAllMethodGenerator method addMapperAnnotations.

@Override
public void addMapperAnnotations(Interface interfaze, Method method) {
    //$NON-NLS-1$
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Select"));
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    method.addAnnotation("@Select({");
    javaIndent(sb, 1);
    //$NON-NLS-1$
    sb.append("\"select\",");
    method.addAnnotation(sb.toString());
    Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().iterator();
    sb.setLength(0);
    javaIndent(sb, 1);
    sb.append('"');
    boolean hasColumns = false;
    while (iter.hasNext()) {
        sb.append(escapeStringForJava(getSelectListPhrase(iter.next())));
        hasColumns = true;
        if (iter.hasNext()) {
            //$NON-NLS-1$
            sb.append(", ");
        }
        if (sb.length() > 80) {
            //$NON-NLS-1$
            sb.append("\",");
            method.addAnnotation(sb.toString());
            sb.setLength(0);
            javaIndent(sb, 1);
            sb.append('"');
            hasColumns = false;
        }
    }
    if (hasColumns) {
        //$NON-NLS-1$
        sb.append("\",");
        method.addAnnotation(sb.toString());
    }
    String orderByClause = introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_SELECT_ALL_ORDER_BY_CLAUSE);
    boolean hasOrderBy = StringUtility.stringHasValue(orderByClause);
    sb.setLength(0);
    javaIndent(sb, 1);
    //$NON-NLS-1$
    sb.append("\"from ");
    sb.append(escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
    sb.append('\"');
    if (hasOrderBy) {
        sb.append(',');
    }
    method.addAnnotation(sb.toString());
    if (hasOrderBy) {
        sb.setLength(0);
        javaIndent(sb, 1);
        //$NON-NLS-1$
        sb.append("\"order by ");
        sb.append(orderByClause);
        sb.append('\"');
        method.addAnnotation(sb.toString());
    }
    //$NON-NLS-1$
    method.addAnnotation("})");
    addAnnotatedResults(interfaze, method);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Aggregations

FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)163 Method (org.mybatis.generator.api.dom.java.Method)97 Parameter (org.mybatis.generator.api.dom.java.Parameter)54 TreeSet (java.util.TreeSet)53 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)32 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)19 Field (org.mybatis.generator.api.dom.java.Field)19 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)19 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)14 CommentGenerator (org.mybatis.generator.api.CommentGenerator)11 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)11 Interface (org.mybatis.generator.api.dom.java.Interface)10 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)7 GeneratedKey (org.mybatis.generator.config.GeneratedKey)7 Plugin (org.mybatis.generator.api.Plugin)5 Attribute (org.mybatis.generator.api.dom.xml.Attribute)5 TextElement (org.mybatis.generator.api.dom.xml.TextElement)5 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)5 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)5