Mybatis Generator 分页查询插件开发

官方的 Mybatis Generator 工具生成的代码中没有分页查询的功能,如果想为生成的代码添加分页查询功能,我们可以为 Mybatis Generator 开发插件,使生成的代码中添加我们想要的 Method 与 SQL 语句.

在学习开发插件的过程中,我主要参考了如下文章.想要理解自定义插件的开发,建议先看看以下文章

  1. 理解MyBatis Generator Plugin
  2. 自定义MyBatis Generator Plugin
  3. Mybatis Generator Plugin 定制我需要的DAO
  4. Mybatis学习笔记九:自定义Generator Plugin

目标

本项目主要对 Mybatis Generator 生成的默认代码中添加分页查询一个方法与一个SQL语句.是对默认代码文件的扩展.
生成的结果如下:

分页查询方法
分页查询SQL语句

步骤

  1. 我们使用 Maven(version=3.5.0) 构建项目.使用如下命令
mvn archetype:generate -DgroupId=项目的groupId -DartifactId=项目的artifactId -DarchetypeArtifactId=maven-archetype-quickstart
  1. 为项目添加 Mybatis Generator 依赖.完整的项目依赖如下

    4.0.0
    org.linweiyu
    PaginationPlugin
    jar
    1.0-SNAPSHOT
    PaginationPlugin
    http://maven.apache.org

    
        UTF-8
    

    
        
            junit
            junit
            3.8.1
            test
        

        
        
            ch.qos.logback
            logback-classic
            [1.1.1,)
        

        
            org.mybatis.generator
            mybatis-generator-core
            [1.3.5,)
        
    

    
        mybatis-pagination-plugin-test
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.6.1
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

  1. 继承 org.mybatis.generator.api.PluginAdapter 类.通过重写其中的方法,即可完成我们需要功能.
    这个类的方法有很多,下面说说笔者学习过程中观察到的几个关键的方法.
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable)方法:通过重写此方法,可以在生成 Mapper.java 类时添加额外的方法.
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable)方法:通过重写此方法,可以在生成 Mapper.xml 文件时添加额外的 SQL 语句.
    public List contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable)方法:通过重写此方法,可以生成额外的 Java 类文件
    根据我们现在的需求,我们只需要重写 clientGenerated() , sqlMapDocumentGenerated() 两个方法即可.
  2. 重写 clientGenerated() 方法,生成额外的 Java 方法
    /**
     * 生成 List<实体类> selectByPage(@Param("offset") Long offset,@Param("limit") Long limit); 方法
     */
    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        boolean super_result = super.clientGenerated(interfaze, topLevelClass, introspectedTable);

            // 生成方法
            Method newMethod = new Method("selectByPage");
            // 设置方法类型
            newMethod.setVisibility(JavaVisibility.PUBLIC);

            // 设置方法返回值类型
            FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("List<" + introspectedTable.getTableConfiguration().getDomainObjectName() + ">");
            newMethod.setReturnType(returnType);

            // 设置方法参数
            FullyQualifiedJavaType offsetJavaType = new FullyQualifiedJavaType("Long");
            Parameter offsetParameter = new Parameter(offsetJavaType, "offset");
            offsetParameter.addAnnotation("@Param(\"offset\")");
            newMethod.addParameter(0, offsetParameter);

            FullyQualifiedJavaType limitJavaType = new FullyQualifiedJavaType("Long");
            Parameter limitParameter = new Parameter(limitJavaType, "limit");
            limitParameter.addAnnotation("@Param(\"limit\")");
            newMethod.addParameter(1, limitParameter);

            // 添加相应的包
            interfaze.addImportedType(new FullyQualifiedJavaType(("org.apache.ibatis.annotations.Param")));
            interfaze.addImportedType(new FullyQualifiedJavaType("java.util.List"));
            interfaze.addMethod(newMethod);

            return true;
        }
        return super_result;
    }
  1. 重写 sqlMapDocumentGenerated() 方法,生成额外的 SQL 语句
/**
     * 生成如下的分页查询sql语句
     * 
     */
    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        boolean super_result = super.sqlMapDocumentGenerated(document, introspectedTable);

        if (isGeneratePagination(introspectedTable) && super_result) {
            XmlElement select = new XmlElement("select");
            select.addAttribute(new Attribute("id", "selectByPage"));
            select.addAttribute(new Attribute("resultMap", "BaseResultMap"));
            select.addElement(new TextElement("SELECT  FROM " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " LIMIT #{offset},#{limit}"));
            XmlElement parentElement = document.getRootElement();
            parentElement.addElement(select);
            return true;
        }

        return super_result;
    }

说明

在以上两个方法中,关键的地方在于使用 Mybatis Generator 中定义的类.
如:org.mybatis.generator.api.dom.java.Method类,这个类代表了 Mapper.java 文件中的一个方法.
大家可以查考 MyBatis Generator 的文档与源码,查阅这些类的定义,这在开发生成额外 Java 文件时十分有用.

结束

通过重写以上两个方法,就可以实现我们此次的目标了.关于如何使用自己开发的插件与获取本项目的源码,可以在 Github 上查看本项目.
在 Github 上还有本人的另一个插件项目,可以实现生成额外的 Java 文件,参考这个插件,我们可以生成任何我们想要的 Java 类!
时间水平有限,如果不足或错误之处,请多多包涵!

你可能感兴趣的:(Mybatis Generator 分页查询插件开发)