Skip to content

Code Merging Information

Jeff Butler edited this page Jul 3, 2018 · 3 revisions

Code Merging in MyBatis Generator

Basics of Code Merging

The generator will attempt to merge generated XML and, in some cases, generated Java code with existing code from a prior run of the generator. The hope is that you will be able to add modifications to the generated code as you wish and those modifications will remain after running the generator again.

The basic merge algorithm is simple: after the code generator runs, the generator will check to see if a file exists with the same name and directory structure as the newly generated code. If a matching file exists, the generator will deleted any generated code in an existing file and write the newly generated code into the existing file - leaving any local modifications intact.

To enable this function, the generator will add custom comments or annotations to the generated code. These comments and annotations inform the merger about what is safe to delete in existing files.

For this reason it is crucial to understand that if you disable all comments in the comment generator, then the merge will no longer function. If you disable comments and run the generator again, you may end up with duplicated code.

Java Code Merge

Java code merge is only supported in the Eclipse plugin. All the underlying Java parsing and AST support is supplied by the Eclipse JDT. When running the generator through the Eclipse plugin Java code will always be merged.

Outside of the Eclipse plugin, the generator will either overwrite existing Java files, or will generate a new file with a unique name (e.g. MyTable.java.1). This is controlled by the -overwrite flag in the Maven/Ant/Command line method of running the generator.

XML Code Merge

XML merge is supported in all environments. By default the generator will always attempt to merge XML files.

If you wish to disable XML file merging, you can use the UnmergeableXmlMappersPlugin to disable XML code merge. In Eclipse, this will force XML files to be written with a unique name. Outside of Eclipse this will cause the generator to honor the -overwrite flag to either overwrite existing XML or generate a file with a unique name.