User
City
Order

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 ? "" : replaceString;
 +            Matcher matcher = pattern.matcher(finalDomainObjectName);
 +            finalDomainObjectName = matcher.replaceAll(replaceString);
          }
 +        return finalDomainObjectName;
      }
  
      /\* (non-Javadoc)
 @@ -304,7 +324,7 @@ public String getAlias() {
 \* Calculates a Java package fragment based on the table catalog and schema.
 \* If qualifiers are ignored, then this method will return an empty string.
 \* 
 - \* <p>This method is used for determining the sub package for Java client and 
 + \* <p>This method is used for determining the sub package for Java client and

…s-generator-core/src/main/java/org/mybatis/generator/config/DomainObjectRenamingRule.java

package org.mybatis.generator.config;
 +
 +import org.mybatis.generator.api.dom.xml.Attribute;
 +import org.mybatis.generator.api.dom.xml.XmlElement;
 +
 +import java.util.List;
 +
 +import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
 +import static org.mybatis.generator.internal.ut`这里写代码片`il.messages.Messages.getString;
 +
 +/\*\*
 + \* This class is used to specify a renaming rule for table's domain object name.
 + \* 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.
 + \* 
 + \* For example, if some tables are named:
 + \* 
 + \* <ul>
 + \* <li>SYS\_USER</li>
 + \* <li>SYS\_ROLE</li>
 + \* <li>SYS\_FUNCTIONS</li>
 + \* </ul>
 + \* 
 + \* it might be annoying to have the generated domain name all containing the SYS
 + \* prefix. This class can be used to remove the prefix by specifying
 + \* 
 + \* <ul>
 + \* <li>searchString="^Sys"</li>
 + \* <li>replaceString=""</li>
 + \* </ul>
 + \* 
 + \* Note that internally, the generator uses the
 + \* <code>java.util.regex.Matcher.replaceAll</code> method for this function. See
 + \* the documentation of that method for example of the regular expression
 + \* language used in Java.
 + \* 
 + \* @author liuzh
 + \* 
 + \*/
 +public class DomainObjectRenamingRule {
 +    private String searchString;
 +    private String replaceString;
 +
 +    public String getReplaceString() {
 +        return replaceString;
 +    }
 +
 +    public void setReplaceString(String replaceString) {
 +        this.replaceString = replaceString;
 +    }
 +
 +    public String getSearchString() {
 +        return searchString;
 +    }
 +
 +    public void setSearchString(String searchString) {
 +        this.searchString = searchString;
 +    }
 +
 +    public void validate(List<String> errors, String tableName) {
 +        if (!stringHasValue(searchString)) {
 +            errors.add(getString("ValidationError.28", tableName)); //$NON-NLS-1$
 +        }


![img](https://img-blog.csdnimg.cn/img_convert/f10ece5d290710b11de3c6ea3702a572.png)
![img](https://img-blog.csdnimg.cn/img_convert/c8ec45003dca5509be43a0e11a36c5f6.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐