use of org.mybatis.generator.config.Context in project generator by mybatis.
the class MyBatisGeneratorTest method testGenerateInvalidConfigWithTwoConnectionSources.
@Test
void testGenerateInvalidConfigWithTwoConnectionSources() {
List<String> warnings = new ArrayList<>();
Configuration config = new Configuration();
Context context = new Context(ModelType.HIERARCHICAL);
context.setId("MyContext");
context.setConnectionFactoryConfiguration(new ConnectionFactoryConfiguration());
context.setJdbcConnectionConfiguration(new JDBCConnectionConfiguration());
config.addContext(context);
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
InvalidConfigurationException e = assertThrows(InvalidConfigurationException.class, () -> {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
});
assertEquals(3, e.getErrors().size());
}
use of org.mybatis.generator.config.Context in project generator by mybatis.
the class MyBatisGenerator method generate.
/**
* This is the main method for generating code. This method is long running, but progress can be provided and the
* method can be cancelled through the ProgressCallback interface.
*
* @param callback
* an instance of the ProgressCallback interface, or <code>null</code> if you do not require progress
* information
* @param contextIds
* a set of Strings containing context ids to run. Only the contexts with an id specified in this list
* will be run. If the list is null or empty, than all contexts are run.
* @param fullyQualifiedTableNames
* a set of table names to generate. The elements of the set must be Strings that exactly match what's
* specified in the configuration. For example, if table name = "foo" and schema = "bar", then the fully
* qualified table name is "foo.bar". If the Set is null or empty, then all tables in the configuration
* will be used for code generation.
* @param writeFiles
* if true, then the generated files will be written to disk. If false,
* then the generator runs but nothing is written
* @throws SQLException
* the SQL exception
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws InterruptedException
* if the method is canceled through the ProgressCallback
*/
public void generate(ProgressCallback callback, Set<String> contextIds, Set<String> fullyQualifiedTableNames, boolean writeFiles) throws SQLException, IOException, InterruptedException {
if (callback == null) {
callback = NULL_PROGRESS_CALLBACK;
}
generatedJavaFiles.clear();
generatedXmlFiles.clear();
ObjectFactory.reset();
RootClassInfo.reset();
// calculate the contexts to run
List<Context> contextsToRun;
if (contextIds == null || contextIds.isEmpty()) {
contextsToRun = configuration.getContexts();
} else {
contextsToRun = new ArrayList<>();
for (Context context : configuration.getContexts()) {
if (contextIds.contains(context.getId())) {
contextsToRun.add(context);
}
}
}
// setup custom classloader if required
if (!configuration.getClassPathEntries().isEmpty()) {
ClassLoader classLoader = getCustomClassloader(configuration.getClassPathEntries());
ObjectFactory.addExternalClassLoader(classLoader);
}
// now run the introspections...
int totalSteps = 0;
for (Context context : contextsToRun) {
totalSteps += context.getIntrospectionSteps();
}
callback.introspectionStarted(totalSteps);
for (Context context : contextsToRun) {
context.introspectTables(callback, warnings, fullyQualifiedTableNames);
}
// now run the generates
totalSteps = 0;
for (Context context : contextsToRun) {
totalSteps += context.getGenerationSteps();
}
callback.generationStarted(totalSteps);
for (Context context : contextsToRun) {
context.generateFiles(callback, generatedJavaFiles, generatedXmlFiles, generatedKotlinFiles, warnings);
}
// now save the files
if (writeFiles) {
callback.saveStarted(generatedXmlFiles.size() + generatedJavaFiles.size());
for (GeneratedXmlFile gxf : generatedXmlFiles) {
projects.add(gxf.getTargetProject());
writeGeneratedXmlFile(gxf, callback);
}
for (GeneratedJavaFile gjf : generatedJavaFiles) {
projects.add(gjf.getTargetProject());
writeGeneratedJavaFile(gjf, callback);
}
for (GeneratedKotlinFile gkf : generatedKotlinFiles) {
projects.add(gkf.getTargetProject());
writeGeneratedKotlinFile(gkf, callback);
}
for (String project : projects) {
shellCallback.refreshProject(project);
}
}
callback.done();
}
use of org.mybatis.generator.config.Context in project generator by mybatis.
the class IbatorConfigurationParser method parseIbatorContext.
private void parseIbatorContext(Configuration configuration, Node node) {
Properties attributes = parseAttributes(node);
//$NON-NLS-1$
String defaultModelType = attributes.getProperty("defaultModelType");
//$NON-NLS-1$
String targetRuntime = attributes.getProperty("targetRuntime");
String introspectedColumnImpl = attributes.getProperty(//$NON-NLS-1$
"introspectedColumnImpl");
//$NON-NLS-1$
String id = attributes.getProperty("id");
ModelType mt = defaultModelType == null ? null : ModelType.getModelType(defaultModelType);
Context context = new Context(mt);
context.setId(id);
if (stringHasValue(introspectedColumnImpl)) {
context.setIntrospectedColumnImpl(introspectedColumnImpl);
}
if (stringHasValue(targetRuntime)) {
context.setTargetRuntime(targetRuntime);
}
configuration.addContext(context);
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
if (childNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
if ("property".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseProperty(context, childNode);
} else if ("ibatorPlugin".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseIbatorPlugin(context, childNode);
} else if ("commentGenerator".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseCommentGenerator(context, childNode);
} else if ("jdbcConnection".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseJdbcConnection(context, childNode);
} else if ("javaModelGenerator".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseJavaModelGenerator(context, childNode);
} else if ("javaTypeResolver".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseJavaTypeResolver(context, childNode);
} else if ("sqlMapGenerator".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseSqlMapGenerator(context, childNode);
} else if ("daoGenerator".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseDaoGenerator(context, childNode);
} else if ("table".equals(childNode.getNodeName())) {
//$NON-NLS-1$
parseTable(context, childNode);
}
}
}
use of org.mybatis.generator.config.Context in project generator by mybatis.
the class MyBatisGeneratorConfigurationParser method parseContext.
private void parseContext(Configuration configuration, Node node) {
Properties attributes = parseAttributes(node);
// $NON-NLS-1$
String defaultModelType = attributes.getProperty("defaultModelType");
// $NON-NLS-1$
String targetRuntime = attributes.getProperty("targetRuntime");
String introspectedColumnImpl = attributes.getProperty(// $NON-NLS-1$
"introspectedColumnImpl");
// $NON-NLS-1$
String id = attributes.getProperty("id");
ModelType mt = defaultModelType == null ? null : ModelType.getModelType(defaultModelType);
Context context = new Context(mt);
context.setId(id);
if (stringHasValue(introspectedColumnImpl)) {
context.setIntrospectedColumnImpl(introspectedColumnImpl);
}
if (stringHasValue(targetRuntime)) {
context.setTargetRuntime(targetRuntime);
}
configuration.addContext(context);
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
if (childNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
if ("property".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseProperty(context, childNode);
} else if ("plugin".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parsePlugin(context, childNode);
} else if ("commentGenerator".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseCommentGenerator(context, childNode);
} else if ("jdbcConnection".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseJdbcConnection(context, childNode);
} else if ("connectionFactory".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseConnectionFactory(context, childNode);
} else if ("javaModelGenerator".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseJavaModelGenerator(context, childNode);
} else if ("javaTypeResolver".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseJavaTypeResolver(context, childNode);
} else if ("sqlMapGenerator".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseSqlMapGenerator(context, childNode);
} else if ("javaClientGenerator".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseJavaClientGenerator(context, childNode);
} else if ("table".equals(childNode.getNodeName())) {
// $NON-NLS-1$
parseTable(context, childNode);
}
}
}
use of org.mybatis.generator.config.Context in project generator by mybatis.
the class MyBatisGeneratorTest method testGenerateInvalidConfigWithNoConnectionSources.
@Test
void testGenerateInvalidConfigWithNoConnectionSources() {
List<String> warnings = new ArrayList<>();
Configuration config = new Configuration();
Context context = new Context(ModelType.HIERARCHICAL);
context.setId("MyContext");
config.addContext(context);
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
InvalidConfigurationException e = assertThrows(InvalidConfigurationException.class, () -> {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
});
assertEquals(3, e.getErrors().size());
}
Aggregations