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>public class PaginationPlugin extends PluginAdapter {modelExampleClassGenerated方法顾名思义,就是生产modelExample类时的扩展,了解generator的都知道,他会帮你生产mapper、model、example三类java对象,其中example类是辅助我们用代码的方式来生产sql的,这个modelExampleClassGenerated方法就是在生产example的时候起效果的。public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated( XmlElement element, IntrospectedTable introspectedTable)
比如,原有的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);
添加完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);
如上所述,就是这么简单,我们可以对生成的mybatis java代码、xml配置做自己的修改。
下面看一下,如何来使用该插件。我们首先需要在使用generator的工程pom文件中加入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><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>这样,我们就可以使用该插件了。
原文地址:http://blog.csdn.net/shunlongjin/article/details/41980223