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。

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

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








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4654298.html,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值