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

这篇具有很好参考价值的文章主要介绍了Mybatis generator实战:自动生成POJO类完整解决方案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

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

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

sys_user
sys_city
sys_order

期望得到的POJO结果是:

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
Mybatis generator实战:自动生成POJO类完整解决方案
Mybatis generator实战:自动生成POJO类完整解决方案

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

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

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

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

Mybatis generator实战:自动生成POJO类完整解决方案
Mybatis generator实战:自动生成POJO类完整解决方案
你看过框架的源码吗?我们做个投票了解一下文章来源地址https://www.toymoban.com/news/detail-441598.html

到了这里,关于Mybatis generator实战:自动生成POJO类完整解决方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot 集成MyBatis-Plus-Generator(最新版3.5.2版本)自动生成代码(附带集成MyBatis-Plus)

    快速入门 代码生成器配置(新) spring boot 2.3.12.RELEASE mybatis 3.5.2 mybatis plus 3.5.2 mybatis plus generator 3.5.2 mysql 8.0.17 velocity 2.3 hutool 5.8.15 druid 1.2.8 lombok 自带 示例程序选择的技术目前各项技术的稳定版本。 相信大家厌烦重复的造轮子过程,编写与数据库表对应的实体类,接着再进

    2024年02月21日
    浏览(41)
  • 【重要】springboot实战(六)之mybatis-plus代码自动生成器

    目录 环境: 步骤: 1.添加依赖 2.配置代码 3.运行 测试 1.测试生成的service 1.1、service用法 2.分页查询 2.1、分页插件配置  2.2、测试 3.源码 jdk:1.8 springboot版本:2.7.15 mybatis-plus版本:3.5.1以上 (本文章用的当前最新版本:3.5.3.2,代码适用于3.5.1版本以上的版本) 在测试类中创建

    2024年02月03日
    浏览(35)
  • Javascript——生成器(Generator)自动执行

    Generator自动化是通过Thunk函数进行实现,写这篇文章的目的是为了理解通过Thunk实现Generator函数的自动执行。 我们可以带入一个业务场景来帮助我们理解Thunk实现Generator自动执行的好处,业务场景如下: 假设小明今天干了一件事情是:         1、买菜         2、买完菜回家

    2024年03月25日
    浏览(86)
  • MyBatis中如何获取自动生成的(主)键值

    Mybatis中insert 方法总是返回一个int值 ,这个值代表的是插入所影响的行数。 如果id采用自增长策略,自动生成的键值在 insert 方法执行完后可以被设置到传入的参数对象中 。那么我们可以在service中通过传入的对象来获得插入的id值。 mapper.xml文件 service代码 日志文件为:

    2023年04月10日
    浏览(23)
  • 如何利用Mybatis-Plus自动生成代码(超详细注解)

    1、简介 MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 特性 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑 损耗小:启动即会自动注入基本 CURD,性

    2024年02月01日
    浏览(30)
  • 5.6 Mybatis代码生成器Mybatis Generator (MBG)实战详解

    本文我们主要实战Mybatis官方的代码生成器:Mybatis Generator(MBG),掌握它以后,可以简化大部分手写代码,我们只需要写复杂逻辑代码! 通过前几篇,我们掌握了在SpringBoot下Mybatis的基本用法,操作步骤回顾一下: 创建与MySQL表对应的Java PO对象,字段一一对应; 创建Mapper接口,

    2024年02月05日
    浏览(29)
  • 【企业级SpringBoot单体项目模板 】——Mybatis-plus自动代码生成

    😜 作           者 :是江迪呀 ✒️ 本文 : SpringBoot项目模版 、 企业级 、 模版 ☀️ 每日   一言 : 我们之所以这样认为,是因为他们这样说。他们之所以那样说,是因为他们想让我们那样认为。所以实践才是检验真理的唯一准则。 上回 我们说了一些开发规范

    2024年02月07日
    浏览(32)
  • 【ChatGPT实战】6.使用ChatGPT自动化生成PPT

    如果需要使用ChatGPT生成PPT,我相信看了上一篇Excel教程的读者,就会想到也通过ChatGPT生成VBA代码,再通过运行VBA代码的方式来自动生成PPT。 理论上是可以的,但是当你实际操作的时候会发现很难实现。因为PPT包含的元素实在是太多了,每张幻灯片都有标题和正文、都有不同

    2024年02月08日
    浏览(33)
  • 深度学习实战28-AIGC项目:自动生成定制化的PPT文件

    大家好,我是微学AI,今天给大家介绍一下深度学习实战28-AIGC项目:自动生成定制化的PPT文件,AIGC项目是一个基于自然语言处理技术的创新性项目,旨在利用ChatGPT模型生成定制化的PPT文件。该项目主要应用于商务和教育领域,可以帮助用户快速创建具有专业性和高质量的P

    2024年02月08日
    浏览(32)
  • 深度学习实战30-AIGC项目:自动生成思维导图文件,解放双手

    大家好,我是微学AI,今天给大家介绍一下深度学习实战30-AIGC项目:自动生成思维导图文件,解放双手,思维导图是一种常见的工具,用于将复杂的信息和概念以图形化方式展示出来。AIGC项目旨在将这种思维导图的创建过程自动化,并通过使用ChatGPT作为生成器,使其变得更

    2024年02月14日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包