Search in sources :

Example 26 with IntrospectedColumn

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

the class AnnotatedSelectByExampleWithBLOBsMethodGenerator method addMapperAnnotations.

@Override
public void addMapperAnnotations(Interface interfaze, Method method) {
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("@SelectProvider(type=");
    sb.append(fqjt.getShortName());
    //$NON-NLS-1$
    sb.append(".class, method=\"");
    sb.append(introspectedTable.getSelectByExampleWithBLOBsStatementId());
    //$NON-NLS-1$
    sb.append("\")");
    method.addAnnotation(sb.toString());
    if (introspectedTable.isConstructorBased()) {
        //$NON-NLS-1$
        method.addAnnotation("@ConstructorArgs({");
    } else {
        //$NON-NLS-1$
        method.addAnnotation("@Results({");
    }
    Iterator<IntrospectedColumn> iterPk = introspectedTable.getPrimaryKeyColumns().iterator();
    Iterator<IntrospectedColumn> iterNonPk = introspectedTable.getNonPrimaryKeyColumns().iterator();
    while (iterPk.hasNext()) {
        IntrospectedColumn introspectedColumn = iterPk.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        sb.append(getResultAnnotation(interfaze, introspectedColumn, true, introspectedTable.isConstructorBased()));
        if (iterPk.hasNext() || iterNonPk.hasNext()) {
            sb.append(',');
        }
        method.addAnnotation(sb.toString());
    }
    while (iterNonPk.hasNext()) {
        IntrospectedColumn introspectedColumn = iterNonPk.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        sb.append(getResultAnnotation(interfaze, introspectedColumn, false, introspectedTable.isConstructorBased()));
        if (iterNonPk.hasNext()) {
            sb.append(',');
        }
        method.addAnnotation(sb.toString());
    }
    //$NON-NLS-1$
    method.addAnnotation("})");
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 27 with IntrospectedColumn

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

the class AnnotatedSelectByPrimaryKeyMethodGenerator method addAnnotatedResults.

private void addAnnotatedResults(Interface interfaze, Method method) {
    if (introspectedTable.isConstructorBased()) {
        //$NON-NLS-1$
        method.addAnnotation("@ConstructorArgs({");
    } else {
        //$NON-NLS-1$
        method.addAnnotation("@Results({");
    }
    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iterPk = introspectedTable.getPrimaryKeyColumns().iterator();
    Iterator<IntrospectedColumn> iterNonPk = introspectedTable.getNonPrimaryKeyColumns().iterator();
    while (iterPk.hasNext()) {
        IntrospectedColumn introspectedColumn = iterPk.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        sb.append(getResultAnnotation(interfaze, introspectedColumn, true, introspectedTable.isConstructorBased()));
        if (iterPk.hasNext() || iterNonPk.hasNext()) {
            sb.append(',');
        }
        method.addAnnotation(sb.toString());
    }
    while (iterNonPk.hasNext()) {
        IntrospectedColumn introspectedColumn = iterNonPk.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        sb.append(getResultAnnotation(interfaze, introspectedColumn, false, introspectedTable.isConstructorBased()));
        if (iterNonPk.hasNext()) {
            sb.append(',');
        }
        method.addAnnotation(sb.toString());
    }
    //$NON-NLS-1$
    method.addAnnotation("})");
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn)

Example 28 with IntrospectedColumn

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

the class DeleteByPrimaryKeyMethodGenerator method addInterfaceElements.

@Override
public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    method.setName(introspectedTable.getDeleteByPrimaryKeyStatementId());
    if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
        importedTypes.add(type);
        //$NON-NLS-1$
        method.addParameter(new Parameter(type, "key"));
    } else {
        // no primary key class - fields are in the base class
        // if more than one PK field, then we need to annotate the
        // parameters
        // for MyBatis
        List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
        boolean annotate = introspectedColumns.size() > 1;
        if (annotate) {
            importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
            "org.apache.ibatis.annotations.Param"));
        }
        StringBuilder sb = new StringBuilder();
        for (IntrospectedColumn introspectedColumn : introspectedColumns) {
            FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
            importedTypes.add(type);
            Parameter parameter = new Parameter(type, introspectedColumn.getJavaProperty());
            if (annotate) {
                sb.setLength(0);
                //$NON-NLS-1$
                sb.append("@Param(\"");
                sb.append(introspectedColumn.getJavaProperty());
                //$NON-NLS-1$
                sb.append("\")");
                parameter.addAnnotation(sb.toString());
            }
            method.addParameter(parameter);
        }
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    addMapperAnnotations(method);
    if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 29 with IntrospectedColumn

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

the class SelectByPrimaryKeyMethodGenerator method addInterfaceElements.

@Override
public void addInterfaceElements(Interface interfaze) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType returnType = introspectedTable.getRules().calculateAllFieldsClass();
    method.setReturnType(returnType);
    importedTypes.add(returnType);
    method.setName(introspectedTable.getSelectByPrimaryKeyStatementId());
    if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
        importedTypes.add(type);
        //$NON-NLS-1$
        method.addParameter(new Parameter(type, "key"));
    } else {
        // no primary key class - fields are in the base class
        // if more than one PK field, then we need to annotate the
        // parameters
        // for MyBatis3
        List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();
        boolean annotate = introspectedColumns.size() > 1;
        if (annotate) {
            importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
            "org.apache.ibatis.annotations.Param"));
        }
        StringBuilder sb = new StringBuilder();
        for (IntrospectedColumn introspectedColumn : introspectedColumns) {
            FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
            importedTypes.add(type);
            Parameter parameter = new Parameter(type, introspectedColumn.getJavaProperty());
            if (annotate) {
                sb.setLength(0);
                //$NON-NLS-1$
                sb.append("@Param(\"");
                sb.append(introspectedColumn.getJavaProperty());
                //$NON-NLS-1$
                sb.append("\")");
                parameter.addAnnotation(sb.toString());
            }
            method.addParameter(parameter);
        }
    }
    addMapperAnnotations(interfaze, method);
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable)) {
        addExtraImports(interfaze);
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 30 with IntrospectedColumn

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

the class BaseRecordGenerator method getCompilationUnits.

@Override
public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.8", //$NON-NLS-1$
    table.toString()));
    Plugin plugins = context.getPlugins();
    CommentGenerator commentGenerator = context.getCommentGenerator();
    TopLevelClass topLevelClass = new TopLevelClass(introspectedTable.getBaseRecordType());
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);
    FullyQualifiedJavaType superClass = getSuperClass();
    if (superClass != null) {
        topLevelClass.setSuperClass(superClass);
        topLevelClass.addImportedType(superClass);
    }
    List<IntrospectedColumn> introspectedColumns;
    if (includePrimaryKeyColumns()) {
        if (includeBLOBColumns()) {
            introspectedColumns = introspectedTable.getAllColumns();
        } else {
            introspectedColumns = introspectedTable.getNonBLOBColumns();
        }
    } else {
        if (includeBLOBColumns()) {
            introspectedColumns = introspectedTable.getNonPrimaryKeyColumns();
        } else {
            introspectedColumns = introspectedTable.getBaseColumns();
        }
    }
    String rootClass = getRootClass();
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        if (RootClassInfo.getInstance(rootClass, warnings).containsProperty(introspectedColumn)) {
            continue;
        }
        Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);
        if (plugins.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addField(field);
            topLevelClass.addImportedType(field.getType());
        }
        Method method = getJavaBeansGetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addMethod(method);
        }
        method = getJavaBeansSetter(introspectedColumn, context, introspectedTable);
        if (plugins.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, Plugin.ModelClassType.BASE_RECORD)) {
            topLevelClass.addMethod(method);
        }
    }
    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelBaseRecordClassGenerated(topLevelClass, introspectedTable)) {
        answer.add(topLevelClass);
    }
    return answer;
}
Also used : CompilationUnit(org.mybatis.generator.api.dom.java.CompilationUnit) CommentGenerator(org.mybatis.generator.api.CommentGenerator) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Method(org.mybatis.generator.api.dom.java.Method) Field(org.mybatis.generator.api.dom.java.Field) JavaBeansUtil.getJavaBeansField(org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) Plugin(org.mybatis.generator.api.Plugin)

Aggregations

IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)81 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