Search in sources :

Example 21 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project java-challenge-answers by lanqiao-labs.

the class TestMyBatis method testRunGeneratorConfig.

@Test
public void testRunGeneratorConfig() throws Exception {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("generatorConfig.xml"));
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
    for (String warning : warnings) {
        System.out.println(warning);
    }
}
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) Test(org.junit.Test)

Example 22 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project ssm-curd by ChenSheng6869.

the class Generator method main.

public static void main(String[] args) throws Exception {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    File configFile = new File("F:\\9.SSM项目\\ssm-curd\\src\\main\\resources\\mbg.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)

Example 23 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project mall by macrozheng.

the class Generator method main.

public static void main(String[] args) throws Exception {
    // MBG 执行过程中的警告信息
    List<String> warnings = new ArrayList<String>();
    // 当生成的代码重复时,覆盖原代码
    boolean overwrite = true;
    // 读取我们的 MBG 配置文件
    InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(is);
    is.close();
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    // 创建 MBG
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    // 执行生成代码
    myBatisGenerator.generate(null);
    // 输出警告信息
    for (String warning : warnings) {
        System.out.println(warning);
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 24 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project OnlineSchoolShop by codingxin.

the class MBGTest method main.

public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
    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) ContextConfiguration(org.springframework.test.context.ContextConfiguration) 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)

Example 25 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)

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