目标

  1. 修改Model的名称
  2. 修改Dao的名称

配置文件

context.targetRuntime 替换为自定义的类型

原理在:org.mybatis.generator.internal.ObjectFactory里

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="test" targetRuntime="eerp.mybatis.generator.plugin.CnoocIntrospectedTable" defaultModelType="flat">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator>
<!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
<!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="net.sourceforge.jtds.jdbc.Driver"
connectionURL="jdbc:jtds:sqlserver://10.68.108.3:1433;DatabaseName=CMAPE"
userId="eerpsa"
password="eerpP@ssword">
</jdbcConnection>
<javaTypeResolver>
<!-- This property is used to specify whether MyBatis Generator should
force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="eerp.model"
targetProject="src\main\java">
<property name="constructorBased" value="true" />
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="eerp.dao" implementationPackage="eerp.dao.impl"
targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!-- 要生成哪些表 -->
<table tableName="T_MGT_Group"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table> </context>
</generatorConfiguration>

自定义IntrospectedTableMyBatis3Impl的子类

public class CnoocIntrospectedTable extends IntrospectedTableMyBatis3Impl {

    private Logger logger = Logger.getLogger(CnoocIntrospectedTable.class);

    @Override
protected void calculateJavaClientAttributes() { super.calculateJavaClientAttributes(); StringBuffer sb = new StringBuffer();
sb.append(this.calculateJavaClientInterfacePackage());
sb.append('.'); // sb.append(this.fullyQualifiedTable.getDomainObjectName());
sb.append(this.generateDomainName());
sb.append("Dao"); String mapperName = sb.toString();
logger.debug("Rename MyBatis3JavaMapperTypeName:" + mapperName);
super.setMyBatis3JavaMapperType(mapperName); } @Override
public void calculateModelAttributes(){
super.calculateModelAttributes();
String packageName = super.calculateJavaModelPackage();
StringBuilder sb = new StringBuilder(); sb.append(packageName);
sb.append('.');
// sb.append(this.fullyQualifiedTable.getDomainObjectName());
String newDomainName = this.generateDomainName();
sb.append(newDomainName);
logger.debug("Rename setBaseRecordType:" + newDomainName);
this.setBaseRecordType(sb.toString());
} protected String generateDomainName() {
String newDomainName = this.fullyQualifiedTable.getIntrospectedTableName();
int index = newDomainName.lastIndexOf("_");
if (index > 0) {
newDomainName = newDomainName.substring(index + 1);
} else {
newDomainName = this.fullyQualifiedTable.getDomainObjectName();
}
return newDomainName;
}
}

代码方式运行mybatis generator

public class MybatisGeneratorRunner {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
URL url = JavaGeneratorRunner.class.getClassLoader().getResource("generatorConfig.xml");
File configFile = new File(url.getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}

Mybatis Generator 扩展的更多相关文章

  1. 记一次 IDEA mybatis.generator 自定义扩展插件

    在使用 idea mybatis.generator 生成的代码,遇到 生成的代码很多重复的地方, 虽然代码是生成的,我们也不应该允许重复的代码出现,因为这些代码后期都要来手动维护. 对于生成时间戳注 ...

  2. Mybatis Generator生成工具配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  3. MyBatis Generator 详解

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  4. MyBatis Generator 详解 【转来纯为备忘】

    版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com   目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...

  5. mybatis generator使用总结

    一.mybatis项目的体系结构 百度mybaits,可以进入mybatis的github:https://github.com/mybatis. mybatis是一个大大的体系,它不是孤立的,它可以 ...

  6. mybatis Generator配置文件详解

    这里按照配置的顺序对配置逐个讲解,更细的内容可以配合中文文档参照. 1. 配置文件头 <?xml version="1.0" encoding="UTF-8&quo ...

  7. mybatis generator配置文件

    <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration ...

  8. 【转】Mybatis Generator最完整配置详解

    本文转简书:http://www.jianshu.com/p/e09d2370b796 --> --> <!-- 自动识别数据库关键字,默认false,如果设置为true,根据Sql ...

  9. Mybatis Generator最完整配置详解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

随机推荐

  1. linux安装以及相关配置

    计算机操作系统简介 操作系统是什么 操作系统的内核是什么 两种操作系统用户界面 安装Linux操作系统的准备工作 LINUX发行版(CENTOS.RHEL.FEDORA.UBUNTU.SUSE) RH ...

  2. python中的内置函数总结

    官方文档 一. 数学函数 #abs() 绝对值 #bin() 二进制 0b #oct() 八进制 0o #hex() 十六进制 0x #complex 复数 x=1-2j print(x) print ...

  3. 文本文件显示 删除文本文件前n个字符

    #include<iostream>#include<string.h>using namespace std;void displayContent(const char * ...

  4. vue指令与组件

    参考http://blog.csdn.net/lioldamon/article/details/74058222?utm_source=itdadao&utm_medium=referral ...

  5. Ubuntu&Linux系统出现文件系统只读Read-only file system 的快速解决方法

    问题描述: 周末运行盘平台服务程序,周一来操作系统卡顿,主进程已退出,重启进程时提示Read-only file system:新建目录和其他chmod -R等等操作都提示Read-only file ...

  6. java内存空间

    Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入Java核心,详细介绍一下Java在内存分配方面的知识.一般 ...

  7. node异步流程控制async

    1.串行无关联:async.series(tasks,callback); 多个函数依次执行,之间没有数据交换,其中一个函数出错,后续函数不再执行 async.series({ one: functi ...

  8. 前端工程师在实现支付功能的时候能做些什么(V客学院技术分享)?

    现在最流行的两种支付微信支付和支付宝支付,在日常开发的过程中肯定离不开支付功能的开发,有很多人第一次接触时会有些措手不及. 一.业务逻辑 (电商平台为例子) 支付大部分用在电商平台,各种打赏,游戏充值 ...

  9. Django学习笔记之django-debug-toolbar使用指南

    介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. github地址 文档地址 安装 pip3 in ...

  10. igraph Tutorial

      igraph Tutorial¶   参考http://www.cs.rhul.ac.uk/home/tamas/development/igraph/tutorial/tutorial.html ...