Mybatis代码生成增加分页参数

自定生成代码分页插件

public class PaginationSupportedPlugin extends PluginAdapter {
    private String customOffsetName = "offset";
    private String customRowsName = "rows";
    private String[] includes = new String[0];
    private String[] excludes = new String[0];

    public PaginationSupportedPlugin() {
    }

    public void setProperties(Properties properties) {
        super.setProperties(properties);
        if (properties.containsKey("customOffsetName")) {
            this.customOffsetName = properties.getProperty("customOffsetName");
        }

        if (properties.containsKey("customRowsName")) {
            this.customRowsName = properties.getProperty("customRowsName");
        }

        if (properties.containsKey("includes")) {
            this.includes = properties.getProperty("includes").split(",");
        }

        if (properties.containsKey("excludes")) {
            this.excludes = properties.getProperty("excludes").split(",");
        }

    }

    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (!this.shouldAppendLimit(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())) {
            return true;
        } else {
            this.addLimit(topLevelClass, introspectedTable, this.customOffsetName);
            this.addLimit(topLevelClass, introspectedTable, this.customRowsName);
            return true;
        }
    }

    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        return this.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
    }

    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        if (!this.shouldAppendLimit(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())) {
            return true;
        } else {
            StringBuilder limitBuilder = new StringBuilder();
            limitBuilder.append("limit ${").append(this.customOffsetName).append("}, ${").append(this.customRowsName).append('}');
            XmlElement isParameterPresenteElemen = (XmlElement)element.getElements().get(element.getElements().size() - 1);
            XmlElement isNotNullElement = new XmlElement("if");
            isNotNullElement.addAttribute(new Attribute("test", "offset!=-1"));
            isNotNullElement.addElement(new TextElement(limitBuilder.toString()));
            element.addElement(isNotNullElement);
            return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
        }
    }

    public boolean providerSelectByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        return !this.shouldAppendLimit(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()) || this.appendLimitForProvider(method);
    }

    public boolean providerSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        return !this.shouldAppendLimit(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()) || this.appendLimitForProvider(method);
    }

    private boolean appendLimitForProvider(Method method) {
        List<String> lines = method.getBodyLines();
        lines.remove(lines.size() - 1);
        StringBuilder line1 = new StringBuilder();
        line1.append("if(example.get").append(Character.toUpperCase(this.customOffsetName.charAt(0))).append(this.customOffsetName.substring(1)).append("() > -1 && example.get").append(Character.toUpperCase(this.customRowsName.charAt(0))).append(this.customRowsName.substring(1)).append("() > 0) {");
        StringBuilder line2 = new StringBuilder();
        line2.append("return SQL() + \" limit \" + example.get").append(Character.toUpperCase(this.customOffsetName.charAt(0))).append(this.customOffsetName.substring(1)).append("()+ \",\" + example.get").append(Character.toUpperCase(this.customRowsName.charAt(0))).append(this.customRowsName.substring(1)).append("();");
        lines.add(line1.toString());
        lines.add(line2.toString());
        lines.add("}");
        lines.add("return SQL();");
        return true;
    }

    private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
        CommentGenerator commentGenerator = this.context.getCommentGenerator();
        Field field = new Field();
        field.setVisibility(JavaVisibility.PROTECTED);
        field.setType(FullyQualifiedJavaType.getIntInstance());
        field.setName(name);
        field.setInitializationString("-1");
        commentGenerator.addFieldComment(field, introspectedTable);
        topLevelClass.addField(field);
        char c = name.charAt(0);
        String camel = Character.toUpperCase(c) + name.substring(1);
        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setName("set" + camel);
        method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), name));
        method.addBodyLine("this." + name + "=" + name + ";");
        commentGenerator.addGeneralMethodComment(method, introspectedTable);
        topLevelClass.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.setName("get" + camel);
        method.addBodyLine("return " + name + ";");
        commentGenerator.addGeneralMethodComment(method, introspectedTable);
        topLevelClass.addMethod(method);
    }

    private boolean shouldAppendLimit(String tableName) {
        return !this.checkExists(tableName, this.excludes) && (this.includes.length < 1 || this.checkExists(tableName, this.includes));
    }

    private boolean checkExists(String tableName, String[] collection) {
        String[] arr$ = collection;
        int len$ = collection.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String i = arr$[i$];
            if (i.equalsIgnoreCase(tableName)) {
                return true;
            }
        }

        return false;
    }

    public boolean validate(List<String> warnings) {
        return true;
    }

    public static void generate() {
        String config = PaginationSupportedPlugin.class.getClassLoader().getResource("././resources/db/mybatis-generator-config.xml").getFile();
        String[] arg = new String[]{"-configfile", config, "-overwrite"};
        ShellRunner.main(arg);
    }

    public static void main(String[] args) {
        generate();
    }
}

猜你喜欢

转载自blog.csdn.net/JunglerOfChina/article/details/80371376