1. 程式人生 > >解決mybatis generator無法覆蓋XML

解決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="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