mybatis-generator autoDelimitKeywords 无效问题解决办法

不用看其它博客了,看这个就完事了,其它博客一个抄一个,全是错的。有可能是 mybatis-generator 迭代导致的,也有可能是根本自己没试过,就是个错的。 

mybatis-generator 怎么用我就不再说了,网上一大堆,现在说说以下这段配置,为什么生成的表,表名两边还是没有 `` 字符包裹。

<property name="javaFileEncoding" value="UTF-8"/>
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>

如果只想知道结果的,我这里可以告诉你们,在 <table> 标签中加一个参数就可以了,先列答案。

<table tableName="ABC"
               domainObjectName="ABC"
               enableSelectByPrimaryKey="true"
               enableSelectByExample="true"
               enableUpdateByPrimaryKey="true"
               enableInsert="true"
               enableUpdateByExample="true"
               enableCountByExample="true" delimitIdentifiers="true">
        </table>

这样就解决问题了,多设置一个 delimitIdentifiers 参数就行了。只拿结果的到这里就不用看了。

以下说说为什么。

首先我们找一下 beginningDelimiter 和 endingDelimiter 在哪儿使用的。以下是使用位置。

org.mybatis.generator.api.FullyQualifiedTable#FullyQualifiedTable

这个方法代码如下

public FullyQualifiedTable(String introspectedCatalog,
            String introspectedSchema, String introspectedTableName,
            String domainObjectName, String alias,
            boolean ignoreQualifiersAtRuntime, String runtimeCatalog,
            String runtimeSchema, String runtimeTableName,
            boolean delimitIdentifiers, DomainObjectRenamingRule domainObjectRenamingRule,
            Context context) {
        super();
        this.introspectedCatalog = introspectedCatalog;
        this.introspectedSchema = introspectedSchema;
        this.introspectedTableName = introspectedTableName;
        this.ignoreQualifiersAtRuntime = ignoreQualifiersAtRuntime;
        this.runtimeCatalog = runtimeCatalog;
        this.runtimeSchema = runtimeSchema;
        this.runtimeTableName = runtimeTableName;
        this.domainObjectRenamingRule = domainObjectRenamingRule;

        if (stringHasValue(domainObjectName)) {
            int index = domainObjectName.lastIndexOf('.');
            if (index == -1) {
                this.domainObjectName = domainObjectName;
            } else {
                this.domainObjectName = domainObjectName.substring(index + 1);
                this.domainObjectSubPackage = domainObjectName.substring(0, index);
            }
        }

        if (alias == null) {
            this.alias = null;
        } else {
            this.alias = alias.trim();
        }

        beginningDelimiter = delimitIdentifiers ? context
                .getBeginningDelimiter() : ""; //$NON-NLS-1$
        endingDelimiter = delimitIdentifiers ? context.getEndingDelimiter()
                : ""; //$NON-NLS-1$
    }

 

注释我就不贴了,在这个方法最后,说的很清楚,beginningDelimiter 和 endingDelimiter 可以是你设置的值,但是务必要使 delimitIdentifiers = true。那我们继续找一下 delimitIdentifiers 在哪儿设置的。

org.mybatis.generator.internal.db.DatabaseIntrospector#calculateIntrospectedTables

在这个地方我们看他代码

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();
            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,
                    tc.getDomainObjectRenamingRule(),
                    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;
    }

 

在这里我们可以看到 delimitIdentifiers 的判断规则。

从以上梳理的结果,我们大致可以认为,以下四种情况,可以使 beginningDelimiter 和 endingDelimiter 的设置生效。

1.<table> 标签中设置 delimitIdentifiers 属性。

2.<table> 标签中设置 catalog 属性,并且在前后或者任何位置,有空格。(不要问我为什么,我也不知道。。)

3.<table> 标签中设置 schema 属性,并且在前后或者任何位置,有空格。(不要问我为什么,我也不知道。。)

4.<table> 标签中设置 tableName 属性,并且在前后或者任何位置,有空格。(不要问我为什么,我也不知道。。)

以上。

猜你喜欢

转载自kehui.iteye.com/blog/2409222
今日推荐