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以后发现了以下几个问题:
-
generatorConfig.xml配置文件位置问题
generatorConfig.xml配置文件默认是需要放到src/main/resources目录下的,而我放到了项目中的根目录,即与pom.xml同级目录,在执行mybatis-generator:generate时提示找不到文件,于是增加了以下配置
<configurationFile>${basedir}/generatorConfig.xml</configurationFile>
用来指定配置文件位置,加了这个配置以后在运行时发现log中显示成功生成了一次,但是接着又出现了多次找不到配置文件的信息,并且这些信息中的目录是相对子module的,于是引出了第二个问题。
-
在多modules项目中 plugin 配置问题
我的项目是一个多modules项目,前边配置的maven plugin是在父pom中配置的,因此在执行的时候会执行n(子modules数)+1(parent project)次,且每次都是相对于当前工程的,即子modules和父项目中都会执行。
因此需要将plugin配置到需要生成mybatis对象的module中去。但是此时又发现了新的问题,即第三个问题。
-
子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&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&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>