Search in sources :

Example 26 with MyBatisGenerator

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 生成成功");
}
Also used : DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) IOException(java.io.IOException) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 27 with MyBatisGenerator

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);
    }
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException) Properties(java.util.Properties) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) StringTokenizer(java.util.StringTokenizer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) EclipseProgressCallback(org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) HashSet(java.util.HashSet) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 28 with MyBatisGenerator

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);
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) SQLException(java.sql.SQLException) XMLParserException(org.mybatis.generator.exception.XMLParserException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 29 with MyBatisGenerator

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();
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 30 with MyBatisGenerator

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());
}
Also used : Context(org.mybatis.generator.config.Context) Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) Test(org.junit.jupiter.api.Test)

Aggregations

MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)49 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)43 ArrayList (java.util.ArrayList)41 Configuration (org.mybatis.generator.config.Configuration)39 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)37 File (java.io.File)22 IOException (java.io.IOException)17 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)15 SQLException (java.sql.SQLException)11 XMLParserException (org.mybatis.generator.exception.XMLParserException)10 InputStream (java.io.InputStream)5 Test (org.junit.Test)4 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)4 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)4 HashSet (java.util.HashSet)3 Test (org.junit.jupiter.api.Test)3 ShellCallback (org.mybatis.generator.api.ShellCallback)3 Properties (java.util.Properties)2 StringTokenizer (java.util.StringTokenizer)2 BuildException (org.apache.tools.ant.BuildException)2