Search in sources :

Example 16 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project springboot-demo by small-rose.

the class MybatisGenerator method doGenerator.

public static void doGenerator() {
    try {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream("generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(is);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (InvalidConfigurationException e) {
        e.printStackTrace();
    } catch (XMLParserException e) {
        e.printStackTrace();
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) SQLException(java.sql.SQLException) XMLParserException(org.mybatis.generator.exception.XMLParserException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 17 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project ed-springboot-learning by QQ986945193.

the class CodeGenerator method genModelAndMapper.

/**
 * 生成model和mapper
 *
 * @param tableName
 *            表名
 * @param modelName
 *            生成的javabean名称
 */
public static void genModelAndMapper(String tableName, String modelName) {
    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(ProjectConstant.JDBC_URL);
    jdbcConnectionConfiguration.setUserId(ProjectConstant.JDBC_USERNAME);
    jdbcConnectionConfiguration.setPassword(ProjectConstant.JDBC_PASSWORD);
    jdbcConnectionConfiguration.setDriverClass(ProjectConstant.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);
    if (StringUtils.isNotEmpty(modelName))
        tableConfiguration.setDomainObjectName(modelName);
    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);
    }
    if (StringUtils.isEmpty(modelName))
        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 18 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project wechat by dllwh.

the class GeneratorSqlmap method generator.

public void generator() throws Exception {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    // 指向逆向工程配置文件
    String genCfg = "/generatorConfig.xml";
    File configFile = new File(GeneratorSqlmap.class.getResource(genCfg).getFile());
    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 19 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project tutorials-java by Artister.

the class MybatisGenerator method generateStubs.

protected void generateStubs(String... tableNames) throws Exception {
    if (ArrayUtils.isEmpty(tableNames)) {
        _LOGGER.info("no table name was specified");
        return;
    }
    makeXml(tableNames);
    File configFile = new File(mbgXmlPath);
    List<String> warnings = new ArrayList<>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(true);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
    _LOGGER.info("mybatis generated done !");
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) File(java.io.File) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 20 with MyBatisGenerator

use of org.mybatis.generator.api.MyBatisGenerator in project show-videos by RAOE.

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

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