目录

  • 1、背景:Mybatis generator根据数据库表自动生成POJO类完整解决方案
  • 2、解决方案:mybatis generator 1.3.6 已经有了这个功能,
    • 2.1、增加了一个新的属性:
    • 2.2、具体配置,在generatreConfig.xml, 例如:
  • 3、一定要开始看源码,非常锻炼编程能力

1、背景:Mybatis generator根据数据库表自动生成POJO类完整解决方案

在用Mybatis generator 生成可以用来访问(多个)表的基础对象,遇到一个问题,就是columnRenamingRule可以替换所有表元素里字段前缀

<columnRenamingRule searchString="^[^_]+" replaceString=""/> 

但是如果想去掉所有表的前缀,比如有多个表:

sys_usersys_citysys_order

期望得到的POJO结果是:

UserCityOrder

2、解决方案:mybatis generator 1.3.6 已经有了这个功能,

2.1、增加了一个新的属性:

domainObjectRenamingRule

2.2、具体配置,在generatreConfig.xml, 例如:

<table tableName="sys%">    <generatedKey column="id" sqlStatement="Mysql"/>    <domainObjectRenamingRule searchString="^sys" replaceString="" /></table>

参照:
https://github.com/mybatis/generator/issues/275
https://github.com/mybatis/generator/pull/176

我们在解决一些问题,要学会查看源码,发现已经有了这个属性:

FullyQualifiedTable.java

 core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/FullyQualifiedTable.java @@ -23,6 +23,10 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; import org.mybatis.generator.config.Context; +import org.mybatis.generator.config.DomainObjectRenamingRule; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; /**   * The Class FullyQualifiedTable. @@ -43,6 +47,7 @@      private boolean ignoreQualifiersAtRuntime;      private String beginningDelimiter;      private String endingDelimiter; +    private DomainObjectRenamingRule domainObjectRenamingRule;        /**       * This object is used to hold information related to the table itself, not the columns in the @@ -82,6 +87,9 @@       * @param delimitIdentifiers       *            if true, then the table identifiers will be delimited at runtime. The delimiter characters are       *            obtained from the Context. +     * @param domainObjectRenamingRule +     *            If domainObjectName is not configured, we'll build the domain object named based on the tableName or runtimeTableName. +     *            And then we use the domain object renameing rule to generate the final domain object name.       * @param context       *            the context       */ @@ -90,7 +98,8 @@ public FullyQualifiedTable(String introspectedCatalog, String domainObjectName, String alias, boolean ignoreQualifiersAtRuntime, String runtimeCatalog, String runtimeSchema, String runtimeTableName, - boolean delimitIdentifiers, Context context) { + boolean delimitIdentifiers, DomainObjectRenamingRule domainObjectRenamingRule, + Context context) { super(); this.introspectedCatalog = introspectedCatalog; this.introspectedSchema = introspectedSchema; @@ -99,6 +108,7 @@ public FullyQualifiedTable(String introspectedCatalog, this.runtimeCatalog = runtimeCatalog; this.runtimeSchema = runtimeSchema; this.runtimeTableName = runtimeTableName; + this.domainObjectRenamingRule = domainObjectRenamingRule; if (stringHasValue(domainObjectName)) { int index = domainObjectName.lastIndexOf('.'); @@ -238,11 +248,21 @@ public String getIbatis2SqlMapNamespace() { public String getDomainObjectName() { if (stringHasValue(domainObjectName)) { return domainObjectName; - } else if (stringHasValue(runtimeTableName)) { - return getCamelCaseString(runtimeTableName, true); + } + String finalDomainObjectName; + if (stringHasValue(runtimeTableName)) { + finalDomainObjectName = getCamelCaseString(runtimeTableName, true); } else { - return getCamelCaseString(introspectedTableName, true); + finalDomainObjectName = getCamelCaseString(introspectedTableName, true); + } + if(domainObjectRenamingRule != null){ + Pattern pattern = Pattern.compile(domainObjectRenamingRule.getSearchString()); + String replaceString = domainObjectRenamingRule.getReplaceString(); + replaceString = replaceString == null " />

你看过框架的源码吗?我们做个投票了解一下