Search in sources :

Example 21 with DefaultShellCallback

use of org.mybatis.generator.internal.DefaultShellCallback 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 22 with DefaultShellCallback

use of org.mybatis.generator.internal.DefaultShellCallback 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)

Example 23 with DefaultShellCallback

use of org.mybatis.generator.internal.DefaultShellCallback in project new-cloud by xie-summer.

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", ProjectConstant.MAPPER_INTERFACE_REFERENCE);
    context.addPluginConfiguration(pluginConfiguration);
    JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
    javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
    javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.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(ProjectConstant.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);
    }
}
Also used : DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) IOException(java.io.IOException) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 24 with DefaultShellCallback

use of org.mybatis.generator.internal.DefaultShellCallback in project imooc-springboot-starter by leechenxiang.

the class GeneratorDisplay method generator.

public void generator() throws Exception {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    // 指定 逆向工程配置文件
    File configFile = new File("generatorConfig.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    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) File(java.io.File) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Aggregations

DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)24 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)23 ArrayList (java.util.ArrayList)21 Configuration (org.mybatis.generator.config.Configuration)20 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)19 File (java.io.File)12 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)8 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)4 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)4 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)4 XMLParserException (org.mybatis.generator.exception.XMLParserException)4 Test (org.junit.jupiter.api.Test)3 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 Context (org.mybatis.generator.config.Context)2 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)2 Generator (com.sun.tools.corba.se.idl.Generator)1 DbRemarksCommentGenerator (com.zzg.mybatis.generator.plugins.DbRemarksCommentGenerator)1 InputStream (java.io.InputStream)1