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

img
img

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

需要这份系统化资料的朋友,可以戳这里获取

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

  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$
 +        }
 +    }
 +
 +    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;
 +    }
 +}

.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) {


![img](https://img-blog.csdnimg.cn/img_convert/2ee5fa6a1f147e8ff0c739c78dcfe935.png)
![img](https://img-blog.csdnimg.cn/img_convert/47d196c2b732c6d79a32fa31e9094e7c.png)
![img](https://img-blog.csdnimg.cn/img_convert/8f952550781e0f18e206b6531312ed70.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

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

基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

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

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值