mavenpom.xmlmybatisoverwritemybatis-generator

How could let MyBatis Generator overwriting the already generated *Mapper.xml?


Like title, when i execute the mybatis-generator, i want to overwriting the already generated *Mapper.xml all, not merge! but i try many config way, it doesn't implement correct. and everytime is generator the more once the xml content. like this:

<resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"> ...
<resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"> ...
<resultMap id="BaseResultMap" type="com.test.entity.GoodsEntity"> ...

in the properties, i had add this line:

<mybatis.generator.overwrite>true</mybatis.generator.overwrite>

and in the build > plugin, i add below lines:

<plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.5</version>
            <configuration>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
                <configurationFile>${mybatis.generator.configurationFile}</configurationFile>
            </configuration>
            <executions>
                <execution>
                    <id>Generate MyBatis Artifacts</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.test</groupId>
                    <artifactId>ob-maven-plugin-mybatis-generator</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
        </plugin>

in the mybatis-generator.xml, i try overwrite config yet. all config it doesn't work goo.

How could I modify the configuration?


Solution

  • I was able to get around this by creating a plugin and adding it to the mybatis-generator-config.xml file. Note, of course, that this solution will cause the Mapper.xml files to always be overwritten regardless of whether or not the -overwrite flag is specified.

    mybatis-generator-config.xml:

    <generatorConfiguration>
        ...
        <context id="myContextId">
            <plugin type="com.mydomain.DeleteExistingSqlMapsPlugin"></plugin>
            ...
        </context>
    </generatorConfiguration>
    

    DeleteExistingSqlMapsPlugin.java:

    ...
    public class DeleteExistingSqlMapsPlugin extends PluginAdapter {
    
        @Override
        public boolean validate(List<String> warnings) {
            return true;
        }
    
        @Override
        public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
                IntrospectedTable introspectedTable)
        {
            String sqlMapPath = sqlMap.getTargetProject() + File.separator
                    + sqlMap.getTargetPackage().replaceAll("\\.", File.separator)
                    + File.separator + sqlMap.getFileName();
            File sqlMapFile = new File(sqlMapPath);
    
            sqlMapFile.delete();
    
            return true;
        }
    
    }
    

    This works because sqlMapGenerated() is called after a Mapper.xml file is created in memory but before it is written to disk.