• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ZhouZhou
博客园    首页    新随笔    联系   管理    订阅  订阅

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

 

 在使用 idea mybatis.generator 生成的代码,遇到 生成的代码很多重复的地方,

虽然代码是生成的,我们也不应该允许重复的代码出现,因为这些代码后期都要来手动维护。

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

 

然而 对于一些符合自己编码习惯的代码风格再通过配置来生成就不大现实了。对于这种情况,我么可以通过扩展  mybatis.generator 的插件来解决。

插件的编写说明网上有很多,列出几个自己看过的

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

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

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

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

很多网友 提供了 对 dto 进行序列化、分页的各种插件,生成插件后,我们如何签入到 IDEA 中呢?

 

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>

 

posted @ 2016-12-09 15:31  JarviseZhou  阅读(3535)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3