今天發現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="mybatis/jdbc.properties"/> <context id="MyBatis" targetRuntime="MyBatis3" defaultModelType="flat"> <!-- 不生成注釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> ... ... <generatorConfiguration>
那怎么既想取消注釋又想覆蓋XML文件生成呢?答案就是上面說的使用UnmergeableXmlMappersPlugin
在<context>下增加一個<plugin>
<!-- 配置生成器 --> <generatorConfiguration> <properties resource="mybatis/jdbc.properties"/> <context id="MyBatis" targetRuntime="MyBatis3" defaultModelType="flat"> <!--覆蓋生成XML文件--> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <!-- 不生成注釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> ... ... <generatorConfiguration>
GitHub地址:https://github.com/syoukaihou/sbsm