All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.revengemission.plugins.mybatis.DynamicSQLPlugin Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.revengemission.plugins.mybatis;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.config.PropertyRegistry;

import java.util.ArrayList;
import java.util.List;

/**
 * MyBatis Dynamic SQL
 * http://www.mybatis.org/mybatis-dynamic-sql/docs/introduction.html
 * targetRuntime="MyBatis3DynamicSQL"
 */
public class DynamicSQLPlugin extends AbstractXmbgPlugin {

    @Override
    public boolean validate(List warnings) {
        return true;
    }


    private String calculateClassName(IntrospectedTable introspectedTable) {
        FullyQualifiedJavaType mapperType = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
        FullyQualifiedJavaType recordType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        return mapperType.getPackageName() + "." + recordType.getShortNameWithoutTypeArguments() + "FieldHelper";
    }

    private String calculateSupportClassName(IntrospectedTable introspectedTable) {
        FullyQualifiedJavaType mapperType = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
        FullyQualifiedJavaType recordType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        return mapperType.getPackageName() + "." + recordType.getShortNameWithoutTypeArguments() + "DynamicSqlSupport";
    }

    @Override
    public List contextGenerateAdditionalJavaFiles(
        IntrospectedTable introspectedTable) {

        List answer = new ArrayList<>();


        Method existField = new Method("existField");
        existField.setVisibility(JavaVisibility.PUBLIC);
        existField.setStatic(true);
        existField.setReturnType(new FullyQualifiedJavaType("boolean"));

        Parameter fieldName = new Parameter(FullyQualifiedJavaType.getStringInstance(), "fieldName", false);
        existField.addParameter(fieldName);

        StringBuffer stringBufferGetField = new StringBuffer();
        stringBufferGetField.append("Map allColumns = new HashMap<>();\n");
        for (IntrospectedColumn field : introspectedTable.getAllColumns()) {
            stringBufferGetField.append("        allColumns.put(\"" + field.getJavaProperty() + "\", " + field.getJavaProperty() + ");\n");
        }
        stringBufferGetField.append("        if (allColumns.containsKey(fieldName)) {\n");
        stringBufferGetField.append("            return true;\n");
        stringBufferGetField.append("        } else {\n");
        stringBufferGetField.append("            return false;\n");
        stringBufferGetField.append("        }");
        existField.addBodyLine(stringBufferGetField.toString());

        Method getSortField = new Method("getSortField");
        getSortField.setVisibility(JavaVisibility.PUBLIC);
        getSortField.setStatic(true);
        getSortField.setReturnType(new FullyQualifiedJavaType("SortSpecification"));

        Parameter sortFieldParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "sortField", false);
        Parameter sortOrderParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "sortOrder", false);
        getSortField.addParameter(sortFieldParameter);
        getSortField.addParameter(sortOrderParameter);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Map allColumns = new HashMap<>();\n");
        for (IntrospectedColumn field : introspectedTable.getAllColumns()) {
            stringBuffer.append("        allColumns.put(\"" + field.getJavaProperty() + "\", " + field.getJavaProperty() + ");\n");
        }
        stringBuffer.append("        if (allColumns.containsKey(sortField)) {\n");
        stringBuffer.append("            if (\"asc\".equalsIgnoreCase(sortOrder))) {\n");
        stringBuffer.append("                return allColumns.get(sortField);\n");
        stringBuffer.append("            } else {\n");
        stringBuffer.append("                return allColumns.get(sortField).descending();\n");
        stringBuffer.append("            }\n");
        stringBuffer.append("        } else {\n");
        stringBuffer.append("            return id.descending();\n");
        stringBuffer.append("        }");
        getSortField.addBodyLine(stringBuffer.toString());

        TopLevelClass topLevelClass = new TopLevelClass(calculateClassName(introspectedTable));
        topLevelClass.setVisibility(JavaVisibility.PUBLIC);
        topLevelClass.setFinal(true);
        topLevelClass.addImportedType(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.SortSpecification"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.SqlColumn"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.HashMap"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.Map"));
        topLevelClass.addStaticImport(calculateSupportClassName(introspectedTable) + ".*");
        topLevelClass.addMethod(getSortField);
        topLevelClass.addMethod(existField);

        GeneratedJavaFile gjf = new GeneratedJavaFile(topLevelClass,
            context.getJavaClientGeneratorConfiguration()
                .getTargetProject(),
            context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
            context.getJavaFormatter());
        answer.add(gjf);

        return answer;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        String objectName = getEntityName(introspectedTable);

        Method selectUniqueByExample = new Method("selectUniqueByExample");
        selectUniqueByExample.setAbstract(true);
        selectUniqueByExample.addAnnotation("@Generated(\"org.mybatis.generator.api.MyBatisGenerator\")");
        selectUniqueByExample.setDefault(true);
        selectUniqueByExample.setReturnType(new FullyQualifiedJavaType("QueryExpressionDSL>"));
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("return SelectDSL.selectWithMapper(this::selectOne");
        for (IntrospectedColumn field : introspectedTable.getAllColumns()) {
            stringBuffer.append(", ");
            stringBuffer.append(field.getJavaProperty());
        }
        stringBuffer.append(")\n                .from(");
        stringBuffer.append(lowerCaseFirstChar(objectName));
        stringBuffer.append(");");
        selectUniqueByExample.addBodyLine(stringBuffer.toString());
        interfaze.addMethod(selectUniqueByExample);
        return true;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy