Search in sources :

Example 6 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project mybatis-generator-gui-extension by spawpaw.

the class SCVXGeneratorPlugin method contextGenerateAdditionalJavaFiles.

@Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
    log.info(">>>> generating extra files...");
    // : 2018/3/22 将 introspectedTable 转化为对象
    Map<String, String> map = new HashMap<>();
    for (Object o : properties.keySet()) {
        map.put(o.toString(), properties.getProperty(o.toString()));
    }
    Table table = new Table(context, introspectedTable, map);
    // : 2018/3/22 初始化context
    VelocityContext templateContext = new VelocityContext();
    templateContext.put("table", table);
    templateContext.put("projectDir.ss", projectDir);
    // 输出测试数据
    String content = renderTemplateAsString("test.vm", templateContext);
    log.info("hierarchical table structure: {}", content);
    // : 2018/3/22 保存到指定目录
    List<TemplateConfig> configs = new Yaml().loadAs(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.yml"), ConfigWrapper.class).getTemplateConfig();
    for (TemplateConfig config : configs) {
        String template = config.getTemplate();
        String destDir = config.getDestDir().replaceAll("\\.", "/");
        String destPackage = config.getDestPackage();
        String destFileName = config.getDestFileName();
        String absPath = (projectDir == null || projectDir.isEmpty() ? "" : projectDir) + destDir + "/" + destPackage.replace(".", "/") + "/" + destFileName;
        absPath = absPath.replace("//", "/");
        absPath = absPath.replace("${entityName}", table.getEntityName());
        absPath = absPath.replace("${basePackage}", basePackage.replace(".", "/"));
        log.info("generate file `{}` from template `{}`", absPath, template);
        content = renderTemplateAsString(config.getTemplate(), templateContext);
        // 写入文件
        try {
            FileUtil.writeStringToFile(absPath, content);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        log.info("content: {}", content);
    }
    log.info("<<< generated extra files.");
    return null;
}
Also used : IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) Table(com.spawpaw.mybatis.generator.gui.entity.Table) ConfigWrapper(com.spawpaw.mybatis.generator.gui.entity.ConfigWrapper) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) TemplateConfig(com.spawpaw.mybatis.generator.gui.entity.TemplateConfig) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml)

Example 7 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project generator by mybatis.

the class Context method validate.

/**
     * This method does a simple validate, it makes sure that all required fields have been filled in. It does not do
     * any more complex operations such as validating that database tables exist or validating that named columns exist
     *
     * @param errors
     *            the errors
     */
public void validate(List<String> errors) {
    if (!stringHasValue(id)) {
        //$NON-NLS-1$
        errors.add(getString("ValidationError.16"));
    }
    if (jdbcConnectionConfiguration == null && connectionFactoryConfiguration == null) {
        // must specify one
        //$NON-NLS-1$
        errors.add(getString("ValidationError.10", id));
    } else if (jdbcConnectionConfiguration != null && connectionFactoryConfiguration != null) {
        // must not specify both
        //$NON-NLS-1$
        errors.add(getString("ValidationError.10", id));
    } else if (jdbcConnectionConfiguration != null) {
        jdbcConnectionConfiguration.validate(errors);
    } else {
        connectionFactoryConfiguration.validate(errors);
    }
    if (javaModelGeneratorConfiguration == null) {
        //$NON-NLS-1$
        errors.add(getString("ValidationError.8", id));
    } else {
        javaModelGeneratorConfiguration.validate(errors, id);
    }
    if (javaClientGeneratorConfiguration != null) {
        javaClientGeneratorConfiguration.validate(errors, id);
    }
    IntrospectedTable it = null;
    try {
        it = ObjectFactory.createIntrospectedTableForValidation(this);
    } catch (Exception e) {
        //$NON-NLS-1$
        errors.add(getString("ValidationError.25", id));
    }
    if (it != null && it.requiresXMLGenerator()) {
        if (sqlMapGeneratorConfiguration == null) {
            //$NON-NLS-1$
            errors.add(getString("ValidationError.9", id));
        } else {
            sqlMapGeneratorConfiguration.validate(errors, id);
        }
    }
    if (tableConfigurations.size() == 0) {
        //$NON-NLS-1$
        errors.add(getString("ValidationError.3", id));
    } else {
        for (int i = 0; i < tableConfigurations.size(); i++) {
            TableConfiguration tc = tableConfigurations.get(i);
            tc.validate(errors, i);
        }
    }
    for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
        pluginConfiguration.validate(errors, id);
    }
}
Also used : IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) SQLException(java.sql.SQLException)

Example 8 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project generator by mybatis.

the class DatabaseIntrospector method calculateIntrospectedTables.

/**
     * Calculate introspected tables.
     *
     * @param tc
     *            the tc
     * @param columns
     *            the columns
     * @return the list
     */
private List<IntrospectedTable> calculateIntrospectedTables(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
    boolean delimitIdentifiers = tc.isDelimitIdentifiers() || stringContainsSpace(tc.getCatalog()) || stringContainsSpace(tc.getSchema()) || stringContainsSpace(tc.getTableName());
    List<IntrospectedTable> answer = new ArrayList<IntrospectedTable>();
    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
        ActualTableName atn = entry.getKey();
        // we only use the returned catalog and schema if something was
        // actually
        // specified on the table configuration. If something was returned
        // from the DB for these fields, but nothing was specified on the
        // table
        // configuration, then some sort of DB default is being returned
        // and we don't want that in our SQL
        FullyQualifiedTable table = new FullyQualifiedTable(stringHasValue(tc.getCatalog()) ? atn.getCatalog() : null, stringHasValue(tc.getSchema()) ? atn.getSchema() : null, atn.getTableName(), tc.getDomainObjectName(), tc.getAlias(), isTrue(tc.getProperty(PropertyRegistry.TABLE_IGNORE_QUALIFIERS_AT_RUNTIME)), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_CATALOG), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_SCHEMA), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_TABLE_NAME), delimitIdentifiers, context);
        IntrospectedTable introspectedTable = ObjectFactory.createIntrospectedTable(tc, table, context);
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            introspectedTable.addColumn(introspectedColumn);
        }
        calculatePrimaryKey(table, introspectedTable);
        enhanceIntrospectedTable(introspectedTable);
        answer.add(introspectedTable);
    }
    return answer;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

IntrospectedTable (org.mybatis.generator.api.IntrospectedTable)8 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ConfigWrapper (com.spawpaw.mybatis.generator.gui.entity.ConfigWrapper)1 Table (com.spawpaw.mybatis.generator.gui.entity.Table)1 TemplateConfig (com.spawpaw.mybatis.generator.gui.entity.TemplateConfig)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 VelocityContext (org.apache.velocity.VelocityContext)1 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)1 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)1 JavaTypeResolver (org.mybatis.generator.api.JavaTypeResolver)1 Plugin (org.mybatis.generator.api.Plugin)1 IntrospectedTableIbatis2Java5Impl (org.mybatis.generator.codegen.ibatis2.IntrospectedTableIbatis2Java5Impl)1 IntrospectedTableMyBatis3Impl (org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl)1