• Mybatis generator实战:自动生成POJO类完整解决方案


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

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

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

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

    sys_user
    sys_city
    sys_order
    
    • 1
    • 2
    • 3

    期望得到的POJO结果是:

    User
    City
    Order
    
    • 1
    • 2
    • 3

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

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

    domainObjectRenamingRule
    
    • 1

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

    <table tableName="sys%">
        <generatedKey column="id" sqlStatement="Mysql"/>
        <domainObjectRenamingRule searchString="^sys" replaceString="" />
    table>
    
    • 1
    • 2
    • 3
    • 4

    参照:
    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.
           * 
     -     * 

    This method is used for determining the sub package for Java client and + *

    This method is used for determining the sub package for Java client and

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    …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:
     + * 
     + * 
      + *
    • SYS_USER
    • + *
    • SYS_ROLE
    • + *
    • SYS_FUNCTIONS
    • + *
    + * + * 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 + * + *
      + *
    • searchString="^Sys"
    • + *
    • replaceString=""
    • + *
    + * + * Note that internally, the generator uses the + * java.util.regex.Matcher.replaceAll 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$ + } + } + + public XmlElement toXmlElement() { + XmlElement xmlElement = new XmlElement("domainRenamingRule"); //$NON-NLS-1$ + xmlElement.addAttribute(new Attribute("searchString", searchString)); //$NON-NLS-1$ + + if (replaceString != null) { + xmlElement.addAttribute(new Attribute( + "replaceString", replaceString)); //$NON-NLS-1$ + } + + return xmlElement; + } +}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    .mybatis-generator-core/src/main/java/org/mybatis/generator/config/TableConfiguration.j

          private boolean delimitIdentifiers;
      
     +    private DomainObjectRenamingRule domainObjectRenamingRule;
     +
          private ColumnRenamingRule columnRenamingRule;
      
          private boolean isAllColumnDelimitingEnabled;
     @@ -462,6 +464,10 @@ public XmlElement toXmlElement() {
                  xmlElement.addElement(generatedKey.toXmlElement());
              }
      
     +        if (domainObjectRenamingRule != null) {
     +            xmlElement.addElement(domainObjectRenamingRule.toXmlElement());
     +        }
     +
              if (columnRenamingRule != null) {
                  xmlElement.addElement(columnRenamingRule.toXmlElement());
              }
     @@ -544,6 +550,10 @@ public void validate(List<String> errors, int listPosition) {
                  }
              }
      
     +        if (domainObjectRenamingRule != null) {
     +            domainObjectRenamingRule.validate(errors, fqTableName);
     +        }
     +
              if (columnRenamingRule != null) {
                  columnRenamingRule.validate(errors, fqTableName);
              }
     @@ -561,6 +571,14 @@ public void validate(List<String> errors, int listPosition) {
              }
          }
      
     +    public DomainObjectRenamingRule getDomainObjectRenamingRule() {
     +        return domainObjectRenamingRule;
     +    }
     +
     +    public void setDomainObjectRenamingRule(DomainObjectRenamingRule domainObjectRenamingRule) {
     +        this.domainObjectRenamingRule = domainObjectRenamingRule;
     +    }
     +
          public ColumnRenamingRule getColumnRenamingRule() {
              return columnRenamingRule;
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    3、一定要开始看源码,非常锻炼编程能力

    参照:https://github.com/mybatis/generator/commit/9194d749943d0c6b4372f27bb87e65749d43cb4c
    https://github.com/mybatis/generator

    在这里插入图片描述
    在这里插入图片描述
    你看过框架的源码吗?我们做个投票了解一下

  • 相关阅读:
    基于深度学习的人脸表情识别的AR川剧变脸(二)
    c++ 模版的一些注意问题
    Electron:菜单
    AOF持久化
    【知识点】浅入线段树与区间最值问题
    Dijkstra算法不能解决负权边的问题
    若依(RuoYi)SpringBoot框架密码加密传输(前后分离板)
    fba海运详解:fba海运是什么意思,FBA海运费用怎么算
    cesium 实现地图环境功能 - 雨,雪,雾特效
    Java 函数式编程
  • 原文地址:https://blog.csdn.net/superdangbo/article/details/127947435