Search in sources :

Example 6 with Configuration

use of org.mybatis.generator.config.Configuration in project paascloud-master by paascloud.

the class MybatisGenerator method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 *
 * @throws Exception the exception
 */
public static void main(String[] args) throws Exception {
    List<String> warnings = new ArrayList<>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(Generator.class.getResourceAsStream("/generator/generatorConfig.xml"));
    DefaultShellCallback callback = new DefaultShellCallback(true);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
}
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) Generator(com.sun.tools.corba.se.idl.Generator) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 7 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class MyBatisGeneratorConfigurationParser method parseConfiguration.

public Configuration parseConfiguration(Element rootNode) throws XMLParserException {
    Configuration configuration = new Configuration();
    NodeList nodeList = rootNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if ("properties".equals(childNode.getNodeName())) {
            //$NON-NLS-1$
            parseProperties(configuration, childNode);
        } else if ("classPathEntry".equals(childNode.getNodeName())) {
            //$NON-NLS-1$
            parseClassPathEntry(configuration, childNode);
        } else if ("context".equals(childNode.getNodeName())) {
            //$NON-NLS-1$
            parseContext(configuration, childNode);
        }
    }
    return configuration;
}
Also used : SqlMapGeneratorConfiguration(org.mybatis.generator.config.SqlMapGeneratorConfiguration) CommentGeneratorConfiguration(org.mybatis.generator.config.CommentGeneratorConfiguration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JavaTypeResolverConfiguration(org.mybatis.generator.config.JavaTypeResolverConfiguration) JavaModelGeneratorConfiguration(org.mybatis.generator.config.JavaModelGeneratorConfiguration) TableConfiguration(org.mybatis.generator.config.TableConfiguration) Configuration(org.mybatis.generator.config.Configuration) JavaClientGeneratorConfiguration(org.mybatis.generator.config.JavaClientGeneratorConfiguration) PluginConfiguration(org.mybatis.generator.config.PluginConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 8 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class ConfigurationParser method parseConfiguration.

private Configuration parseConfiguration(InputSource inputSource) throws IOException, XMLParserException {
    parseErrors.clear();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new ParserEntityResolver());
        ParserErrorHandler handler = new ParserErrorHandler(warnings, parseErrors);
        builder.setErrorHandler(handler);
        Document document = null;
        try {
            document = builder.parse(inputSource);
        } catch (SAXParseException e) {
            throw new XMLParserException(parseErrors);
        } catch (SAXException e) {
            if (e.getException() == null) {
                parseErrors.add(e.getMessage());
            } else {
                parseErrors.add(e.getException().getMessage());
            }
        }
        if (parseErrors.size() > 0) {
            throw new XMLParserException(parseErrors);
        }
        Configuration config;
        Element rootNode = document.getDocumentElement();
        DocumentType docType = document.getDoctype();
        if (rootNode.getNodeType() == Node.ELEMENT_NODE && docType.getPublicId().equals(XmlConstants.IBATOR_CONFIG_PUBLIC_ID)) {
            config = parseIbatorConfiguration(rootNode);
        } else if (rootNode.getNodeType() == Node.ELEMENT_NODE && docType.getPublicId().equals(XmlConstants.MYBATIS_GENERATOR_CONFIG_PUBLIC_ID)) {
            config = parseMyBatisGeneratorConfiguration(rootNode);
        } else {
            //$NON-NLS-1$
            throw new XMLParserException(getString("RuntimeError.5"));
        }
        if (parseErrors.size() > 0) {
            throw new XMLParserException(parseErrors);
        }
        return config;
    } catch (ParserConfigurationException e) {
        parseErrors.add(e.getMessage());
        throw new XMLParserException(parseErrors);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Configuration(org.mybatis.generator.config.Configuration) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLParserException(org.mybatis.generator.exception.XMLParserException) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) DocumentType(org.w3c.dom.DocumentType) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 9 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class JavaCodeGenerationTest method generateJavaFiles.

private static List<GeneratedJavaFile> generateJavaFiles(String configFile) throws Exception {
    List<String> warnings = new ArrayList<String>();
    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 10 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class MyBatisGeneratorTest method testGenerateMyBatis3WithInvalidConfig.

@Test(expected = InvalidConfigurationException.class)
public void testGenerateMyBatis3WithInvalidConfig() throws Exception {
    List<String> warnings = new ArrayList<String>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigMyBatis3_badConfig.xml"));
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    try {
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        myBatisGenerator.generate(null, null, null, false);
    } catch (InvalidConfigurationException e) {
        assertEquals(2, e.getErrors().size());
        throw e;
    }
}
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) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) Test(org.junit.Test)

Aggregations

Configuration (org.mybatis.generator.config.Configuration)15 ArrayList (java.util.ArrayList)12 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)11 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)10 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)9 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)9 XMLParserException (org.mybatis.generator.exception.XMLParserException)6 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)5 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)5 HashSet (java.util.HashSet)4 StringTokenizer (java.util.StringTokenizer)4 Test (org.junit.Test)4 File (java.io.File)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 JavaClientGeneratorConfiguration (org.mybatis.generator.config.JavaClientGeneratorConfiguration)2