MyBatis Generator (MBG) may be invoked directly from Java. For configuration, you may use either an XML configuration file, or configure MBG completely with Java.
The following code sample shows how to call MBG from Java with an XML based configuration. It does not show exception handling, but that should be obvious from the compiler errors :)
List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null);
Notes:
generated.source.dir
can be
accessed in the configuration file with the escape sequence
${generated.source.dir}
The following code sample shows how to call MBG from Java with a Java based configuration. It does not show exception handling, but that should be obvious from the compiler errors :)
List<String> warnings = new ArrayList<String>(); boolean overwrite = true; Configuration config = new Configuration(); // ... fill out the config object as appropriate... DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null);