zoukankan      html  css  js  c++  java
  • mybatis generator插件开发

    mybatis现在普遍使用的每一个人DAO框架。mybatis generator它可以基于数据库中的表结构,生成自己主动mybatis代码和配置文件,方便使用,当然,实际的使用过程中。generator当然,也有很多不尽人意的地方,幸运的是,他提供了一种机制插头,来帮我们做扩大。

    解说的插件开发依赖下面mybatis版本号:

            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.2</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
            </dependency>

    首先我们的插件类须要扩展PluginAdapter类,例如以下:

    public class PaginationPlugin extends PluginAdapter {

    pluginAdapter提供了全部的接口,方便我们在插件进行到某个操作的时候。做自己定义的改动。这里我们主要介绍下面几类方法:

    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
                                                  IntrospectedTable introspectedTable)

    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
                XmlElement element, IntrospectedTable introspectedTable) 

    modelExampleClassGenerated方法顾名思义。就是生产modelExample类时的扩展,了解generator的都知道。他会帮你生产mapper、model、example三类java对象,当中example类是辅助我们用代码的方式来生产sql的,这个modelExampleClassGenerated方法就是在生产example的时候起效果的。


    比方。原有的generator1.3.2插件不支持分页,那我们能够首先在modelExampleClassGenerated方法中。对生产的Class加上limit的參数,代码例如以下:

    CommentGenerator commentGenerator = 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 int limitStart的属性,并加入了get和set方法。


    加入完example的属性后,我们须要将这个属性加到生成的mapper xml配置文件里。

    这就用到sqlMapSelectByExampleWithBLOBsElementGenerated方法,当然,注意这种方法的名字,我们想改动哪个xml文件里的sql,就须要找到相应的方法来复写。


    XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
            isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart>-1")); //$NON-NLS-1$ //$NON-NLS-2$
            isNotNullElement.addElement(new TextElement(
                    "limit ${limitStart} , ${limitEnd}"));
            element.addElement(isNotNullElement);

    如上述代码,我们在生成的xml文件里。增加一个if条件推断,推断假设当前example类的limitStart和limitEnd不为-1。注意这个-1是我们在example扩展字段时候加上的默认值。假设不是默认值。那么就加上limit ${limitStart}, ${limitEnd}分页条件。


    如上所述,就是这么简单。我们能够对生成的mybatis java代码、xml配置做自己的改动。


    以下看一下,怎样来使用该插件。

    我们首先须要在使用generator的projectpom文件里增加generator插件,并将我们的插件包依赖加进来,这里我们的样例是mybatis-page-plugin。

    <build>
            <defaultGoal>install</defaultGoal>
    
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <configurationFile>generatorConfig.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.6</version>
                        </dependency>
                        <dependency>
                            <groupId>org.duoku.groom</groupId>
                            <artifactId>mybatis-page-plugin</artifactId>
                            <version>1.0-SNAPSHOT</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>

    如配置所述,我们的插件配置文件是generatorConfig.xml。我们来看一下这个文件:

    <generatorConfiguration>
    
        <context id="MBG" targetRuntime="MyBatis3" defaultModelType="conditional">	<!--targetRuntime 此属性用于指定生成的代码的执行目标。

    --> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> <plugin type="org.duoku.groom.mybatis.plugin.PaginationPlugin"></plugin>


    如上图所看到的。我们在plugin中,加上我们的插件,PaginationPlugin。

    此,我们可以使用这个插件。

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    持续集成-禅道
    nohup.out 日志切分
    Flannel 介绍及使用场景
    【Unity游戏开发】初探Unity动画优化
    fastHttp服务端处理请求的过程
    PHPExcel导出文件代码实现
    PHPExcel 1.8
    CKfinder 安装与使用
    Ckeditor的配置
    Ckeditor的使用
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4654298.html
Copyright © 2011-2022 走看看