use of org.mybatis.generator.api.MyBatisGenerator in project rabbitmq-demo by songdada1995.
the class CodeGenerator method genModelAndMapper.
public static void genModelAndMapper(String tableName) {
Context context = new Context(ModelType.FLAT);
context.setId("Potato");
context.setTargetRuntime("MyBatis3Simple");
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
context.addPluginConfiguration(pluginConfiguration);
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
sqlMapGeneratorConfiguration.setTargetPackage("mapper");
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration.setTableName(tableName);
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
context.addTableConfiguration(tableConfiguration);
List<String> warnings;
MyBatisGenerator generator;
try {
Configuration config = new Configuration();
config.addContext(context);
config.validate();
boolean overwrite = true;
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
warnings = new ArrayList<String>();
generator = new MyBatisGenerator(config, callback, warnings);
generator.generate(null);
} catch (Exception e) {
throw new RuntimeException("生成Model和Mapper失败", e);
}
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
throw new RuntimeException("生成Model和Mapper失败:" + warnings);
}
String modelName = tableNameConvertUpperCamel(tableName);
System.out.println(modelName + ".java 生成成功");
System.out.println(modelName + "Mapper.java 生成成功");
System.out.println(modelName + "Mapper.xml 生成成功");
}
use of org.mybatis.generator.api.MyBatisGenerator in project generator by mybatis.
the class GeneratorAntTask method execute.
/*
* (non-Javadoc)
*
* @see org.apache.tools.ant.Task#execute()
*/
@Override
public void execute() throws BuildException {
setLoggingImplementation();
if (!StringUtility.stringHasValue(configfile)) {
throw new BuildException("configfile is a required parameter");
}
List<String> warnings = new ArrayList<>();
File configurationFile = new File(configfile);
if (!configurationFile.exists()) {
throw new BuildException("configfile " + configfile + " does not exist");
}
Set<String> fullyqualifiedTables = new HashSet<>();
if (StringUtility.stringHasValue(fullyQualifiedTableNames)) {
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(fullyQualifiedTableNames, ",");
while (st.hasMoreTokens()) {
String s = st.nextToken().trim();
if (s.length() > 0) {
fullyqualifiedTables.add(s);
}
}
}
Set<String> contexts = new HashSet<>();
if (StringUtility.stringHasValue(contextIds)) {
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(contextIds, ",");
while (st.hasMoreTokens()) {
String s = st.nextToken().trim();
if (s.length() > 0) {
contexts.add(s);
}
}
}
IProgressMonitor monitor = (IProgressMonitor) getProject().getReferences().get(AntCorePlugin.ECLIPSE_PROGRESS_MONITOR);
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
subMonitor.beginTask("Generating MyBatis Artifacts:", 1000);
subMonitor.subTask("Parsing Configuration");
Properties p = propertyset == null ? new Properties() : propertyset.getProperties();
p.putAll(getProject().getUserProperties());
ConfigurationParser cp = new ConfigurationParser(p, warnings);
Configuration config = cp.parseConfiguration(configurationFile);
subMonitor.worked(50);
monitor.subTask("Generating Files from Database Tables");
MyBatisGenerator generator = new MyBatisGenerator(config, new EclipseShellCallback(), warnings);
EclipseProgressCallback progressCallback = new EclipseProgressCallback(subMonitor.newChild(950));
generator.generate(progressCallback, contexts, fullyqualifiedTables);
} catch (XMLParserException e) {
for (String error : e.getErrors()) {
log(error, Project.MSG_ERR);
}
throw new BuildException(e.getMessage());
} catch (SQLException e) {
throw new BuildException(e.getMessage());
} catch (IOException e) {
throw new BuildException(e.getMessage());
} catch (InvalidConfigurationException e) {
throw new BuildException(e.getMessage());
} catch (InterruptedException e) {
throw new BuildException("Cancelled by user");
} finally {
monitor.done();
}
for (String warning : warnings) {
log("WARNING: " + warning, Project.MSG_WARN);
}
}
use of org.mybatis.generator.api.MyBatisGenerator in project generator by mybatis.
the class GeneratorAntTask method execute.
@Override
public void execute() {
File configurationFile = calculateConfigurationFile();
Set<String> fullyqualifiedTables = calculateTables();
Set<String> contexts = calculateContexts();
List<String> warnings = new ArrayList<>();
try {
Properties p = propertyset == null ? null : propertyset.getProperties();
ConfigurationParser cp = new ConfigurationParser(p, warnings);
Configuration config = cp.parseConfiguration(configurationFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(new AntProgressCallback(this, verbose), contexts, fullyqualifiedTables);
} catch (XMLParserException | InvalidConfigurationException e) {
for (String error : e.getErrors()) {
log(error, Project.MSG_ERR);
}
throw new BuildException(e.getMessage());
} catch (SQLException | IOException e) {
throw new BuildException(e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
log(e, Project.MSG_ERR);
throw new BuildException(e.getMessage());
}
for (String error : warnings) {
log(error, Project.MSG_WARN);
}
}
use of org.mybatis.generator.api.MyBatisGenerator in project generator by mybatis.
the class JavaCodeGenerationTest method generateJavaFiles.
static List<GeneratedJavaFile> generateJavaFiles(String configFile) throws Exception {
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(JavaCodeGenerationTest.class.getResourceAsStream(configFile));
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
return myBatisGenerator.getGeneratedJavaFiles();
}
use of org.mybatis.generator.api.MyBatisGenerator 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