转载自  解决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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐