Search in sources :

Example 1 with TableColumnMetaData

use of com.spawpaw.mybatis.generator.gui.entity.TableColumnMetaData in project mybatis-generator-gui-extension by spawpaw.

the class DatabaseConfig method connect.

/**
 * 连接数据库,初始化表信息
 */
public void connect() throws SQLException {
    if (tableConfigs != null && tableConfigs.size() != 0)
        return;
    tableConfigs = new HashMap<>();
    Connection connection = getConnection();
    DatabaseMetaData meta = connection.getMetaData();
    ResultSet rs;
    String[] types = { "TABLE", "VIEW" };
    // 获取表列表
    switch(DatabaseType.valueOf(databaseType.getValue())) {
        case MySQL:
            rs = meta.getTables(null, dbName.getValue(), null, types);
            break;
        case Oracle:
        case Oracle_SID:
        case Oracle_ServiceName:
        case Oracle_TNSName:
        case Oracle_TNSEntryString:
            rs = meta.getTables(null, userName.getValue().toUpperCase(), null, types);
            break;
        case SQLServer:
            rs = meta.getTables(null, null, "%", types);
            break;
        case PostgreSQL:
            rs = meta.getTables(null, "%", "%", types);
            break;
        case DB2:
            rs = meta.getTables(null, "jence_user", "%", types);
            break;
        case SYBASE:
            rs = meta.getTables(null, null, "%", types);
            break;
        case INFORMIX:
            rs = meta.getTables(null, null, "%", types);
            break;
        default:
            throw new RuntimeException(Constants.getI18nStr("msg.unsupportedDatabase"));
    }
    while (rs.next()) {
        tableConfigs.put(rs.getString(3), new ArrayList<>());
    }
    List<String> tmpList = new ArrayList<>(tableConfigs.keySet());
    tmpList.sort(Comparator.naturalOrder());
    // 获取每个表中的字段信息
    for (String tableName : tmpList) {
        // 生成表的基本信息(每个字段的名称、类型)
        rs = meta.getColumns(null, null, tableName, null);
        while (rs.next()) {
            TableColumnMetaData columnMetaData = new TableColumnMetaData();
            columnMetaData.setColumnName(rs.getString("COLUMN_NAME"));
            columnMetaData.setJdbcType(rs.getString("TYPE_NAME"));
            tableConfigs.get(tableName).add(columnMetaData);
        }
        // 生成TreeView
        TreeItem<String> item = new TreeItem<>(tableName);
        rootItem.getChildren().add(item);
        rootItem.setExpanded(true);
    }
// TODO: 2018/3/17 关闭数据库连接
}
Also used : TreeItem(javafx.scene.control.TreeItem) TableColumnMetaData(com.spawpaw.mybatis.generator.gui.entity.TableColumnMetaData)

Example 2 with TableColumnMetaData

use of com.spawpaw.mybatis.generator.gui.entity.TableColumnMetaData in project mybatis-generator-gui-extension by spawpaw.

the class MBGRunner method generate.

public String generate() {
    config = new Configuration();
    // default model type
    if (projectConfig.defaultModelType.getValue().equalsIgnoreCase("CONDITIONAL"))
        context = new Context(ModelType.CONDITIONAL);
    else if (projectConfig.defaultModelType.getValue().equalsIgnoreCase("FLAT"))
        context = new Context(ModelType.FLAT);
    else
        context = new Context(ModelType.HIERARCHICAL);
    // id
    context.setId("mybatis generator gui extension");
    // targetRuntime
    context.setTargetRuntime("MyBatis3");
    context.addProperty("javaFileEncoding", projectConfig.javaFileEncoding.getValue());
    // =====================================================================================================加载插件
    // initialize plugin data
    initPluginConfigs();
    addPlugins();
    // ====================================================================================================注释生成器
    if (projectConfig.enableComment.getValue()) {
        CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
        commentGeneratorConfiguration.setConfigurationType(DeclaredPlugins.CommentPlugin);
        // commentGeneratorConfiguration.addProperty("suppressAllComments", "true");
        if (pluginConfigs.containsKey(DeclaredPlugins.CommentPlugin)) {
            HashMap<String, String> pluginProperties = pluginConfigs.get(DeclaredPlugins.CommentPlugin);
            for (String key : pluginProperties.keySet()) commentGeneratorConfiguration.addProperty(key, pluginProperties.get(key));
        }
        context.setCommentGeneratorConfiguration(commentGeneratorConfiguration);
    }
    // ==============================================================================================jdbc connection
    JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
    jdbcConnectionConfiguration.setDriverClass(databaseConfig.driver());
    jdbcConnectionConfiguration.setConnectionURL(databaseConfig.connectionUrl());
    jdbcConnectionConfiguration.setUserId(databaseConfig.userName.getValue());
    jdbcConnectionConfiguration.setPassword(databaseConfig.password.getValue());
    context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
    // =============================================================================================javaTypeResolver
    JavaTypeResolverConfiguration javaTypeResolverConfiguration = new JavaTypeResolverConfiguration();
    javaTypeResolverConfiguration.addProperty("forceBigDecimals", "false");
    context.setJavaTypeResolverConfiguration(javaTypeResolverConfiguration);
    // ========================================================================================================model
    JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
    javaModelGeneratorConfiguration.setTargetPackage(projectConfig.entityPackage.getValue().replace(" ", ""));
    javaModelGeneratorConfiguration.setTargetProject(projectDir() + projectConfig.entityDir.getValue());
    javaModelGeneratorConfiguration.addProperty("enableSubPackages", "true");
    javaModelGeneratorConfiguration.addProperty("useActualColumnNames", projectConfig.useActualColumnNames.getValue().toString());
    javaModelGeneratorConfiguration.addProperty("trimStrings", "true");
    context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
    // =======================================================================================================mapper
    SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
    sqlMapGeneratorConfiguration.setTargetProject(projectDir() + projectConfig.mapperDir.getValue());
    sqlMapGeneratorConfiguration.setTargetPackage(projectConfig.mapperPackage.getValue());
    sqlMapGeneratorConfiguration.addProperty("useActualColumnNames", projectConfig.useActualColumnNames.getValue().toString());
    sqlMapGeneratorConfiguration.addProperty("enableSubPackages", "true");
    context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
    // ==========================================================================================================dao
    JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
    javaClientGeneratorConfiguration.setConfigurationType(projectConfig.javaClientMapperType.getValue());
    javaClientGeneratorConfiguration.setTargetProject(projectDir() + projectConfig.daoDir.getValue());
    javaClientGeneratorConfiguration.setTargetPackage(projectConfig.daoPackage.getValue());
    sqlMapGeneratorConfiguration.addProperty("useActualColumnNames", projectConfig.useActualColumnNames.getValue().toString());
    sqlMapGeneratorConfiguration.addProperty("enableSubPackages", "true");
    context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
    // ========================================================================================================table
    TableConfiguration tableConfiguration = new TableConfiguration(context);
    tableConfiguration.setTableName(projectConfig.selectedTable.getValue());
    tableConfiguration.setDomainObjectName(projectConfig.entityObjName.getValue().replace(" ", ""));
    tableConfiguration.setMapperName(projectConfig.daoObjName.getValue().replace(" ", ""));
    tableConfiguration.setInsertStatementEnabled(projectConfig.enableInsert.getValue());
    tableConfiguration.setSelectByPrimaryKeyStatementEnabled(projectConfig.enableSelectByPrimaryKey.getValue());
    tableConfiguration.setSelectByExampleStatementEnabled(projectConfig.enableSelectByExample.getValue());
    if (projectConfig.selectByPrimaryKeyQueryId.getValue().isEmpty())
        tableConfiguration.setSelectByPrimaryKeyQueryId(projectConfig.selectByPrimaryKeyQueryId.getValue());
    if (!projectConfig.selectByExampleQueryId.getValue().isEmpty())
        tableConfiguration.setSelectByExampleQueryId(projectConfig.selectByExampleQueryId.getValue());
    tableConfiguration.setUpdateByPrimaryKeyStatementEnabled(projectConfig.enableUpdateByPrimaryKey.getValue());
    tableConfiguration.setUpdateByExampleStatementEnabled(projectConfig.enableUpdateByExample.getValue());
    tableConfiguration.setDeleteByPrimaryKeyStatementEnabled(projectConfig.enableDeleteByPrimaryKey.getValue());
    tableConfiguration.setDeleteByExampleStatementEnabled(projectConfig.enableDeleteByExample.getValue());
    tableConfiguration.setCountByExampleStatementEnabled(projectConfig.enableCountByExample.getValue());
    // 使用小骆驼峰替代原列名
    tableConfiguration.addProperty("useActualColumnNames", projectConfig.useActualColumnNames.getValue().toString());
    if (!projectConfig.enableVirtualPrimaryKeyPlugin.getValue().isEmpty())
        tableConfiguration.addProperty("virtualKeyColumns", projectConfig.enableVirtualPrimaryKeyPlugin.getValue());
    // see http://www.mybatis.org/generator/configreference/generatedKey.html  ,JDBC is a database independent method of obtaining the value from identity columns,only for Mybatis3+
    if (!projectConfig.primaryKey.getValue().isEmpty())
        tableConfiguration.setGeneratedKey(new GeneratedKey(projectConfig.primaryKey.getValue(), "JDBC", true, null));
    // 添加忽略列/列覆写
    for (TableColumnMetaData column : databaseConfig.tableConfigs.get(projectConfig.selectedTable.getValue())) {
        if (!column.getChecked()) {
            System.out.println("忽略的列:" + column.getColumnName());
            tableConfiguration.addIgnoredColumn(new IgnoredColumn(column.getColumnName()));
        } else {
            ColumnOverride columnOverride = new ColumnOverride(column.getColumnName());
            columnOverride.setJavaProperty(column.getPropertyName());
            columnOverride.setJavaType(column.getJavaType());
            // columnOverride.setJdbcType(column.getJdbcType());
            columnOverride.setTypeHandler(column.getTypeHandler());
            tableConfiguration.addColumnOverride(columnOverride);
        }
    }
    context.addTableConfiguration(tableConfiguration);
    config.addContext(context);
    List<String> warnings = new ArrayList<>();
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    try {
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    } catch (InvalidConfigurationException | InterruptedException | SQLException | IOException e) {
        e.printStackTrace();
    }
    return warnings.toString();
}
Also used : SQLException(java.sql.SQLException) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) TableColumnMetaData(com.spawpaw.mybatis.generator.gui.entity.TableColumnMetaData) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Aggregations

TableColumnMetaData (com.spawpaw.mybatis.generator.gui.entity.TableColumnMetaData)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 TreeItem (javafx.scene.control.TreeItem)1 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)1 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)1 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)1