Search in sources :

Example 61 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class AbstractJavaMapperMethodGenerator method addGeneratedKeyAnnotation.

protected void addGeneratedKeyAnnotation(Method method, GeneratedKey gk) {
    StringBuilder sb = new StringBuilder();
    IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
    if (introspectedColumn != null) {
        if (gk.isJdbcStandard()) {
            //$NON-NLS-1$
            sb.append("@Options(useGeneratedKeys=true,keyProperty=\"");
            sb.append(introspectedColumn.getJavaProperty());
            //$NON-NLS-1$
            sb.append("\")");
            method.addAnnotation(sb.toString());
        } else {
            FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
            //$NON-NLS-1$
            sb.append("@SelectKey(statement=\"");
            sb.append(gk.getRuntimeSqlStatement());
            //$NON-NLS-1$
            sb.append("\", keyProperty=\"");
            sb.append(introspectedColumn.getJavaProperty());
            //$NON-NLS-1$
            sb.append("\", before=");
            //$NON-NLS-1$ //$NON-NLS-2$
            sb.append(gk.isIdentity() ? "false" : "true");
            //$NON-NLS-1$
            sb.append(", resultType=");
            sb.append(fqjt.getShortName());
            //$NON-NLS-1$
            sb.append(".class)");
            method.addAnnotation(sb.toString());
        }
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 62 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class AbstractJavaMapperMethodGenerator method addGeneratedKeyImports.

protected void addGeneratedKeyImports(Interface interfaze, GeneratedKey gk) {
    IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
    if (introspectedColumn != null) {
        if (gk.isJdbcStandard()) {
            //$NON-NLS-1$
            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Options"));
        } else {
            //$NON-NLS-1$
            interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectKey"));
            FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
            interfaze.addImportedType(fqjt);
        }
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 63 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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 64 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class UpdateByPrimaryKeySelectiveElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("update");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getUpdateByPrimaryKeySelectiveStatementId()));
    String parameterType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        parameterType = introspectedTable.getRecordWithBLOBsType();
    } else {
        parameterType = introspectedTable.getBaseRecordType();
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterType", parameterType));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("update ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    //$NON-NLS-1$
    XmlElement dynamicElement = new XmlElement("set");
    answer.addElement(dynamicElement);
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())) {
        //$NON-NLS-1$
        XmlElement isNotNullElement = new XmlElement("if");
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" != null");
        //$NON-NLS-1$
        isNotNullElement.addAttribute(new Attribute("test", sb.toString()));
        dynamicElement.addElement(isNotNullElement);
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        sb.append(',');
        isNotNullElement.addElement(new TextElement(sb.toString()));
    }
    boolean and = false;
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        sb.setLength(0);
        if (and) {
            //$NON-NLS-1$
            sb.append("  and ");
        } else {
            //$NON-NLS-1$
            sb.append("where ");
            and = true;
        }
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        answer.addElement(new TextElement(sb.toString()));
    }
    if (context.getPlugins().sqlMapUpdateByPrimaryKeySelectiveElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 65 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class UpdateByPrimaryKeyWithoutBLOBsElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("update");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getUpdateByPrimaryKeyStatementId()));
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterType", introspectedTable.getBaseRecordType()));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("update ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    // set up for first column
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("set ");
    Iterator<IntrospectedColumn> iter;
    if (isSimple) {
        iter = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns()).iterator();
    } else {
        iter = ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getBaseColumns()).iterator();
    }
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        if (iter.hasNext()) {
            sb.append(',');
        }
        answer.addElement(new TextElement(sb.toString()));
        // set up for the next column
        if (iter.hasNext()) {
            sb.setLength(0);
            OutputUtilities.xmlIndent(sb, 1);
        }
    }
    boolean and = false;
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        sb.setLength(0);
        if (and) {
            //$NON-NLS-1$
            sb.append("  and ");
        } else {
            //$NON-NLS-1$
            sb.append("where ");
            and = true;
        }
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        answer.addElement(new TextElement(sb.toString()));
    }
    if (context.getPlugins().sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Aggregations

IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)82 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)34 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)32 Attribute (org.mybatis.generator.api.dom.xml.Attribute)30 Method (org.mybatis.generator.api.dom.java.Method)29 TextElement (org.mybatis.generator.api.dom.xml.TextElement)27 ArrayList (java.util.ArrayList)17 Parameter (org.mybatis.generator.api.dom.java.Parameter)17 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)12 TreeSet (java.util.TreeSet)9 Field (org.mybatis.generator.api.dom.java.Field)9 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)8 CommentGenerator (org.mybatis.generator.api.CommentGenerator)7 Plugin (org.mybatis.generator.api.Plugin)7 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)7 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 GeneratedKey (org.mybatis.generator.config.GeneratedKey)6 HashMap (java.util.HashMap)5 List (java.util.List)5