Mybatis Generator Maven Plugin 改进

博主在切换到VSCode后发现缺少MybatisGenerator的插件,于是采用官方的Maven插件。在配置过程中遇到了配置文件位置、多模块项目执行次数以及依赖解析问题。通过修改配置文件路径、将插件配置移动到对应模块及调整插件依赖解析范围,解决了这些问题。最后分享了一个修改后的MybatisGeneratorMavenPlugin版本供他人使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis Generator Maven Plugin 改进

近期为了统一前后端开发工具,将IDE换成了VS Code,但是发现VS Code 没有比较好用的Mybatis Generator插件,于是只能选择使用官方提供的Mybatis Generator,官方插件提供了Ant、Maven、Java Code、Eclipse Plugin四种使用方式,我的项目用的是Maven,因此可选的就是Maven和Java Code两种方式,为了减少代码开发和使用配置的方式来使用工具,因此选择了Mybatis Generator Maven Plugin。

但是在配置了pom以后发现了以下几个问题:

  1. generatorConfig.xml配置文件位置问题

    generatorConfig.xml配置文件默认是需要放到src/main/resources目录下的,而我放到了项目中的根目录,即与pom.xml同级目录,在执行mybatis-generator:generate时提示找不到文件,于是增加了以下配置

    <configurationFile>${basedir}/generatorConfig.xml</configurationFile>
    

    用来指定配置文件位置,加了这个配置以后在运行时发现log中显示成功生成了一次,但是接着又出现了多次找不到配置文件的信息,并且这些信息中的目录是相对子module的,于是引出了第二个问题。

  2. 在多modules项目中 plugin 配置问题

    我的项目是一个多modules项目,前边配置的maven plugin是在父pom中配置的,因此在执行的时候会执行n(子modules数)+1(parent project)次,且每次都是相对于当前工程的,即子modules和父项目中都会执行。

    因此需要将plugin配置到需要生成mybatis对象的module中去。但是此时又发现了新的问题,即第三个问题。

  3. 子module中依赖问题

    在子module中执行mybatis-generator:generate时提示缺少依赖,仔细看发现此依赖是该子module依赖的其他子module,而在平时的开发过程中我并不会将开发中的项目进行install操作,因此在检索依赖关系时就找不到依赖了,但是再仔细想想,我当前是在开发阶段,我需要的操作是进行generate操作,而并非是编译操作,因此不应该对我的其他依赖进行检查,为了解决此问题就去看了官方插件的源码,随后在org.mybatis.generator.maven.MyBatisGeneratorMojo中发现了端倪:

    ...
    /**
     * Goal which generates MyBatis artifacts.
     */
     @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
             requiresDependencyResolution = ResolutionScope.TEST)  // requiresDependencyResolution 指定了解析test范围的依赖
     public class MyBatisGeneratorMojo extends AbstractMojo {
    
         private ThreadLocal<ClassLoader> savedClassloader = new ThreadLocal<>();
    ...
    

    为了解决此问题,将这里换成ResolutionScope.NONE后达到了想要的效果。在后边的附件中附上了下载链接,方便需要的同学使用。

附件

点击下载附件ihai-mybatis-generator-maven-plugin.zip

pom.xml配置:

    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>ihai-mybatis-generator-maven-plugin</artifactId>
                <version>1.4.1</version>
                <configuration>
                    <configurationFile>${basedir}/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                    <includeCompileDependencies>false</includeCompileDependencies>
                    <includeAllDepencies>false</includeAllDepencies>
                </configuration>
            </plugin>
            ...
        </plugins>
    </build>
    ...

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <classPathEntry location="/.m2/mysql/mysql-connector-java/8.0.30/mysql-connector-java-8.0.30.jar" />

  <context id="mybatisGenerator" targetRuntime="MyBatis3">
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&amp;useSSL=true&amp;serverTimezone=GMT%2B8"
        userId="root"
        password="root">
    </jdbcConnection>

    <commentGenerator>
      <property name="suppressAllComments" value="false"/>
      <property name="addRemarkComments" value="true"/>
    </commentGenerator>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <javaModelGenerator targetPackage="domain.model" targetProject="project/src/main/java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <sqlMapGenerator targetPackage="domain.mapper"  targetProject="project/src/main/java">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="domain.mapper"  targetProject="project/src/main/java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <table schema="test" tableName="user">
      <property name="enableCountByExample" value="true"/>
      <property name="enableUpdateByExample" value="true"/>
      <property name="enableDeleteByExample" value="true"/>
      <property name="enableSelectByExample" value="true"/>
      <property name="selectByExampleQueryId" value="true"/>
    </table>
  </context>
</generatorConfiguration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EngineZhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值