解决mybatis generator无法覆盖XML
今天发现 mybatis generator maven plugin 在重复生成的时候 xml 文件只会 merge,不会覆盖。
明明在 pom.xml 中配置了如下:
<configuration> <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration>
去github上查找与 overwrite 相关的 issue,找到了这个提交。
上面的意思是:当你取消了所有注释,你在重复运行 generator 时在 mapper.xml 中会出现重复的元素。并且这个 plugin 可以解决这个问题,版本是 1.3.7
去查看 generatorConfiguration,确实配置了取消生成注释。
<!-- 配置生成器 --> <generatorConfiguration><properties resource=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mybatis/jdbc.properties</span><span style="color: rgba(128, 0, 0, 1)">"</span>/> <context id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyBatis</span><span style="color: rgba(128, 0, 0, 1)">"</span> targetRuntime=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyBatis3</span><span style="color: rgba(128, 0, 0, 1)">"</span> defaultModelType=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">flat</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <!-- 不生成注释 --> <commentGenerator> <property name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">suppressAllComments</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">true</span><span style="color: rgba(128, 0, 0, 1)">"</span>/> </commentGenerator><span style="color: rgba(0, 0, 0, 1)"> ... ...
<generatorConfiguration>
那怎么既想取消注释又想覆盖 XML 文件生成呢?答案就是上面说的使用UnmergeableXmlMappersPlugin
在 <context> 下增加一个 <plugin>
<!-- 配置生成器 --> <generatorConfiguration><properties resource=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mybatis/jdbc.properties</span><span style="color: rgba(128, 0, 0, 1)">"</span>/> <context id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyBatis</span><span style="color: rgba(128, 0, 0, 1)">"</span> targetRuntime=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyBatis3</span><span style="color: rgba(128, 0, 0, 1)">"</span> defaultModelType=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">flat</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <!--覆盖生成XML文件--> <plugin type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin</span><span style="color: rgba(128, 0, 0, 1)">"</span> /> <!-- 不生成注释 --> <commentGenerator> <property name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">suppressAllComments</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">true</span><span style="color: rgba(128, 0, 0, 1)">"</span>/> </commentGenerator><span style="color: rgba(0, 0, 0, 1)"> ... ...
<generatorConfiguration>
GitHub 地址:https://github.com/syoukaihou/sbsm