记一次 IDEA mybatis.generator 自定义扩展插件

 

 在使用 idea mybatis.generator 生成的代码,遇到 生成的代码不少重复的地方,html

虽然代码是生成的,咱们也不该该容许重复的代码出现,由于这些代码后期都要来手动维护。java

对于生成时间戳注释、Example类型,xml 等大多均可以经过xml配置来,让其不生成。apache

 

然而 对于一些符合本身编码习惯的代码风格再经过配置来生成就不大现实了。对于这种状况,我么能够经过扩展  mybatis.generator 的插件来解决。mybatis

插件的编写说明网上有不少,列出几个本身看过的app

http://generator.sturgeon.mopaas.com/reference/extending.htmlmaven

http://blog.geyuxu.com/2015/11/02/java/在 Intellij 中添加mybatis-generator分页插件/ide

Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码编码

mybatis generator生成带有分页的Mybatis代码idea

不少网友 提供了 对 dto 进行序列化、分页的各类插件,生成插件后,咱们如何签入到 IDEA 中呢?spa

 

step 1.将咱们的须要的插件 ,例如:SerializablePlugin、BaseMapperGeneratorPlugin,咱们单独放在项目中生成一个jar包

 

 

step  2.在咱们的实际项目中,将 step1 中生成的jar 包 放到咱们本地的maven仓储中

具体步骤可参考 这位博友的操做说明,这样作的目的就是,咱们须要将step1 中生成jar 在为 mybatis.generator 插件配置时,直接从本地仓储能找到改文件。

命令:

install:install-file -Dfile=/Users/geyuxu/Desktop/pagination-0.1.jar -DgroupId=com.geyuxu.utils -DartifactId=pagination -Dversion=0.1 -Dpackaging=jar

 

 

 

 step 3:  这一步 就是要为咱们的  org.mybatis.generator  插件进行配置,告诉他 到那个jar包中去找 咱们提供给他的插件

配置以下(若是不尽心这一步配置,可能会出现以下异常:

Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Cannot instantiate object of type com.)

(ps:若是按照以上步骤进行操做,依然出现上述异常,那几手动把本地maven仓储中的改jar文件删掉,项目中的引用也删掉,从新生成jar 从新引用)

            <!--mybatis-generator-maven-plugin插件来完成MyBatis model 和Mapper文件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--Mybatis自动生成规则配置文件地址-->
                    <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <artifactId>xxxx-xxx-utils</artifactId>
                        <groupId>xx.xxxx.xxx.xxx</groupId>
                        <version>1.0</version>
                    </dependency>
                </dependencies>
            </plugin>
相关文章
相关标签/搜索