mybatis generator插件开发
2015-07-17 14:09
525 查看
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。此,我们可以使用这个插件。
相关文章推荐
- Google的IP地址一览表,加上代理服务器
- DT大数据梦工厂 第55,56讲
- 黑马程序员--------IO流
- json 格式化
- (4.3.1.3)【项目一】主体框架Fragment中Selector的使用与自定义button-shape
- C/C++程序设计05(泛型编程、容器、STL)
- C# Management 类获取本机各种信息
- Daddy, I found it!, 15 Awesome Linux Find Command Examples (15个极好的Linux find命令示例 )
- 【故障处理】ORA-12162 错误的处理
- GDB应用的总结
- WPF 格式化输出- IValueConverter接口的使用
- ibatis 控制台输出sql语句
- android CheckBox和右侧文字间距问题
- SVN Unable to connect to a repository at URL问题解决
- android textview 笔记
- SVN Unable to connect to a repository at URL问题解决
- 云片网-最好用的短信服务商 短信平台
- java注解机制
- Jaxb2 实现JavaBean与xml互转
- CentOS 7.0搭建LAMP服务器(Apache+PHP+MariaDB)