Mybatis Generator 扩展
目标
- 修改Model的名称
- 修改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 扩展的更多相关文章
- 记一次 IDEA mybatis.generator 自定义扩展插件
在使用 idea mybatis.generator 生成的代码,遇到 生成的代码很多重复的地方, 虽然代码是生成的,我们也不应该允许重复的代码出现,因为这些代码后期都要来手动维护. 对于生成时间戳注 ...
- Mybatis Generator生成工具配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- MyBatis Generator 详解
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
- MyBatis Generator 详解 【转来纯为备忘】
版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com 目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...
- mybatis generator使用总结
一.mybatis项目的体系结构 百度mybaits,可以进入mybatis的github:https://github.com/mybatis. mybatis是一个大大的体系,它不是孤立的,它可以 ...
- mybatis Generator配置文件详解
这里按照配置的顺序对配置逐个讲解,更细的内容可以配合中文文档参照. 1. 配置文件头 <?xml version="1.0" encoding="UTF-8&quo ...
- mybatis generator配置文件
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration ...
- 【转】Mybatis Generator最完整配置详解
本文转简书:http://www.jianshu.com/p/e09d2370b796 --> --> <!-- 自动识别数据库关键字,默认false,如果设置为true,根据Sql ...
- Mybatis Generator最完整配置详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
随机推荐
- Maven详解(转)
原文出自: http://www.cnblogs.com/hongwz/p/5456578.html http://ifeve.com/maven-1/ Maven介绍: Maven是一个强大的Jav ...
- 『NiFi 学习之路』使用 —— 主要组件的使用
一.概述 大部分 NiFi 使用者都是通过 NiFi 的 Processor 来实现自己的业务的.因此,我也主要就 NiFi 官方提供的 Porcessor 进行介绍. 二.Processor 如果你 ...
- 洛谷 P4211 [LNOI2014]LCA (树链剖分+离线)
题目:https://www.luogu.org/problemnew/solution/P4211 相当难的一道题,其思想难以用言语表达透彻. 对于每个查询,区间[L,R]中的每个点与z的lca肯定 ...
- iOS 关于BTC 一些知识点
1.BTC 用这个网 可以校验 自己的库生成的助记词 地址 是否是合法正常的 https://iancoleman.io/bip39/ 2.知晓 BTC 钱包是否有钱 和交易记录 https://te ...
- Wex5各组件介绍
1.http://doc.wex5.com/comp-base/ 2.select 组件 http://doc.wex5.com/comps-select/ 3.页面交互以及传递参数 http:// ...
- nginx windows版本 1024限制
Windows版本因为文件访问句柄数被限制为1024了,当访问量大时就会无法响应. 会有如下错误提示:maximum number of descriptors supported by select ...
- ArchLinux基本系统到XFCE4桌面搭建
Keep It Simple, Stupid 这是ArchLinux的哲学,更是一种人生哲学 好久没用linux了,这段时间因为一点点"破坏性"需求重新拾起linux用了一把 ...
- db2 xml 转 table
版本:DB2 Version 9.1 1.创建测试表,初始化数据 create table emp (doc XML); INSERT INTO EMP VALUES ('<dept bldg= ...
- Spring中Bean生命周期
Spring中的bean生命周期是一个重要的点,只有理解Bean的生命周期,在开发中会对你理解代码是非常有用的.对于Bean的周期,个人认为可以分为四个阶段.第一阶段:Bean的实例化,在该阶段主要是 ...
- 批量操作QT UI中的控件
背景:在一个项目中,可能一个UI中存在大量相同的tablewidget,combobox,label等控件,每种可能有100个,此时想对它们进行同样的操作 方案:(以tablewidget为例,UI中 ...