Search in sources :

Example 6 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException in project jeesuite-libs by vakinge.

the class MyBatisGeneratorTool method main.

public static void main(String[] args) {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    // src的一级目录下
    String genCfg = "/generatorConfig.xml";
    File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile());
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = null;
    try {
        config = cp.parseConfiguration(configFile);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMLParserException e) {
        e.printStackTrace();
    }
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = null;
    try {
        myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    } catch (InvalidConfigurationException e) {
        e.printStackTrace();
    }
    try {
        myBatisGenerator.generate(null);
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) File(java.io.File) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 7 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException 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 8 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException 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 9 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException 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)

Example 10 with InvalidConfigurationException

use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.

the class MyBatisGeneratorTest method testGenerateMyBatis3WithInvalidConfig.

@Test
void testGenerateMyBatis3WithInvalidConfig() throws Exception {
    List<String> warnings = new ArrayList<>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigMyBatis3_badConfig.xml"));
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    InvalidConfigurationException e = assertThrows(InvalidConfigurationException.class, () -> {
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        myBatisGenerator.generate(null, null, null, false);
    });
    assertEquals(2, e.getErrors().size());
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) 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

InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)12 ArrayList (java.util.ArrayList)11 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)11 Configuration (org.mybatis.generator.config.Configuration)11 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)9 IOException (java.io.IOException)8 SQLException (java.sql.SQLException)8 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)8 XMLParserException (org.mybatis.generator.exception.XMLParserException)7 File (java.io.File)5 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)4 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 Test (org.junit.jupiter.api.Test)3 Properties (java.util.Properties)2 BuildException (org.apache.tools.ant.BuildException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 Context (org.mybatis.generator.config.Context)2 EclipseProgressCallback (org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback)2