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

news2024/10/7 2:29:36

目录

  • 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_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
在这里插入图片描述
在这里插入图片描述

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

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/20062.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

训练集表达的分类规则整理

(A&#xff0c;B)---3*30*2---(1,0)(0,1) 做一个网络分类A和B&#xff0c;让A和B的训练集都只有一张图片3个点&#xff0c;测试集为 0 0 0 0 1 0 0 1 2 0 1 0 3 0 1 1 4 1 0 0 5 1 0 1 6 1 1 0 7 1 1 1 二进制的0-7.记录网络随着A和B的改变分类…

LeetCode - 300 最长递增子序列

目录 题目来源 题目描述 示例 提示 题目解析 算法源码 题目来源 300. 最长递增子序列 - 力扣&#xff08;LeetCode&#xff09; 题目描述 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&…

Windows进程间利用管道通信

实验一 一、实验内容或题目&#xff1a; 在父进程中创建一个子进程&#xff0c;并建立一个管道&#xff0c;子进程向管道中写入一个字符串&#xff0c;父进程从管道中读出字符串。 二、实验目的与要求&#xff1a; 利用CRT相关接口&#xff0c;学习在父子进程间实现管道通信…

酒水商城|基于Springboot实现酒水商城系统

作者主页&#xff1a;编程千纸鹤 作者简介&#xff1a;Java、前端、Pythone开发多年&#xff0c;做过高程&#xff0c;项目经理&#xff0c;架构师 主要内容&#xff1a;Java项目开发、毕业设计开发、面试技术整理、最新技术分享 收藏点赞不迷路 关注作者有好处 项目编号&…

【Java八股文总结】之集合

文章目录Java集合一、集合概述1、List、Set、Queue、Map的区别&#xff1f;2、Collections和Collection的区别&#xff1f;3、集合和数组的区别二、List1、ArrayList和LinkedList的区别&#xff1f;2、ArrayList和Vector的区别3、Vector、ArrayList和LinkedList的区别4、ArrayL…

Echarts:简单词云图实现

Echarts是一个开源的可视化图表库&#xff0c;支持丰富的图表&#xff0c;官网中还有大量示例可以选择使用、参考。 其中比较好玩、有趣的是词云&#xff0c;词云就是用关键词组成的一朵云&#xff0c;更广泛的定义是&#xff0c;由关键词组成的任意一种图案均称为词云。因此&…

[附源码]java毕业设计社区空巢老人关爱服务平台

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

【服务器】无法进行ssh连接的问题逐一排查以及解决方法

一、检查服务器网络 先检查是否是网络的问题。按快捷键WinR&#xff0c;在弹出的对话框中输入cmd。 点击确定运行。在cmd窗口输入ping一下服务器的ip地址。 如果出现请求超时&#xff0c;解决办法如下&#xff1a; 在服务器端输入ifconfig命令&#xff0c;查看要连接的网络的…

[计算机毕业设计]知识图谱的检索式对话系统

前言 &#x1f4c5;大四是整个大学期间最忙碌的时光,一边要忙着准备考研,考公,考教资或者实习为毕业后面临的就业升学做准备,一边要为毕业设计耗费大量精力。近几年各个学校要求的毕设项目越来越难,有不少课题是研究生级别难度的,对本科同学来说是充满挑战。为帮助大家顺利通过…

NX二次开发-调内部函数SEL_set_type_filter_index_by_label设置类型过滤器例子剖析怎么查找内部函数调用内部函数

NX二次开发-调内部函数SEL_set_type_filter_index_by_label设置类型过滤器例子剖析怎么查找内部函数调用内部函数 前言 给那些不会调内部函数的人,一个学习方法,大概知道怎么找内部接口,怎么调用内部函数的。 复杂的东西我也不会,等我研究出来了,在更新到博客上。 版本…

业务级灾备架构设计

同城多中心架构 同城双中心基本架构 关键特征&#xff1a; 相同城市&#xff0c;相距50km以上光纤互联机房间网络延时<2ms 同城双中心架构本质 同城双中心可以当做一个逻辑机房可以应对机房级别的灾难 同城双中心应用技巧-多光纤通路 同一集群&#xff0c;部署在同城两个…

异常~~~

异常 异常体系 编译时异常和运行时异常的区别 Java中的异常被分为两大类&#xff1a;编译时异常和运行时异常&#xff0c;也别成为受检异常和非受检异常 所有的RuntimeException类及其子类被称为运行时异常&#xff0c;其他的异常都是编译时异常 编译时异常&#xff1a;必须…

怎么在bios里设置光驱启动 bios设置光驱启动图文教程

大部分主板都是在开机以后按DEL键进入BIOS设置。 第一部分&#xff1a;学会各种bios主板的光驱启动设置&#xff0c;稍带把软驱关闭掉。 图1&#xff1a; 图2&#xff1a;光驱启动设置 。 图3&#xff1a;回车后要保存退出 。 图4&#xff1a;提示用户&#xff0c;必须选择“…

机器学习:BP神经网络

神经网络人工神经网络的结构特点人工神经网络单层神经网络双层神经网络多层神经网络BP神经网络通过TensorFlow实现BP神经网络单层感知网络是最初的神经网络&#xff0c;具有模型清晰、结构简单、计算量小等优点&#xff0c;但是它无法处理非线性问题。BP神经网络具有任意复杂的…

性能工具之前端分析工Chrome Developer Tools性能标签

文章目录一、前言二、第一部分三、第二部分四、第三部分五、第四部分六、小结一、前言 之前本博曾经写过几篇和前端性能分析相关的文章&#xff0c;如下&#xff1a; 常见性能工具一览性能工具之常见压力工具是否能模拟前端&#xff1f;性能工具之前端分析工Chrome Developer…

【Pytorch with fastai】第 19 章 :从零开始的 fastai 学习者

&#x1f50e;大家好&#xff0c;我是Sonhhxg_柒&#xff0c;希望你看完之后&#xff0c;能对你有所帮助&#xff0c;不足请指正&#xff01;共同学习交流&#x1f50e; &#x1f4dd;个人主页&#xff0d;Sonhhxg_柒的博客_CSDN博客 &#x1f4c3; &#x1f381;欢迎各位→点赞…

AutoDWG 文件属性编辑修改控件/Attribute Modifier-X

AutoDWG 文件属性编辑修改控件组件/AttributeX 控件组件 属性控件组件是开发者无需AutoCAD就可以直接在dwg文件中提取或修改属性值的组件。 主要特征&#xff1a; 从 dwg 文件中提取属性值。 直接在dwg文件中添加或修改属性值。 支持 R9 到 2016 版本的 DWG 和 DXF。 独立于 A…

如何学习Python技术?自学Python需要多久?

前言 Python要学习的时间取决于学习方式&#xff1a;自学至少需要学8个月;报班的话&#xff0c;可能需要4个月左右的时间。如果想具体了解Python要学多久&#xff0c;那不妨接着往下看吧! &#xff08;文末送读者福利&#xff09; 这个问题分为两种情况&#xff0c;分为自学和…

机械设计基础总复习

《机械设计基础》 一、简答题 1. 机构与机器的特征有何不同&#xff1f; 机器的特征&#xff1a;&#xff08;1&#xff09;人为机件的组合&#xff1b;&#xff08;2&#xff09;有确定的运动&#xff1b;&#xff08;3&#xff09;能够进行能量转换或代替人的劳动。 机构…

攻防世界Encode

Encode 题目描述&#xff1a;套娃&#xff1f; 题目环境&#xff1a;https://download.csdn.net/download/m0_59188912/87094879 打开timu.txt文件&#xff0c;猜测是rot13加密。 Rot13在线解码网址&#xff1a;https://www.jisuan.mobi/puzzm6z1B1HH6yXW.html 解密得到&#x…