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 ...
随机推荐
- 工作笔记——js与文件上传下载
1 js判断上传文件的后缀名,文件大小 //判断照片大小 function getPhotoSize(obj){ photoExt=obj.value.substr(obj.value.lastInd ...
- 实现:左边为菜单导航,当一个菜单中包含多个Tabs,并且不同的Tab要根据权限的不同显示。
1.前台代码 //当点击左侧菜单时,将访问Controller中的Home方法,这样就会根据用户权限的不同,通过后台的判断来决定显示的页面<li class="@(ViewBag.Se ...
- 树莓派搭建Git服务器
目录 安装ssh 安装git-core 新增git用户 设置git用户目录 [服务端]设置git仓库 [客户端]设置git仓库 设置ssh登录 安装ssh sudo apt-get install s ...
- 2017 Multi-University Training Contest - Team 4 hdu6069 Counting Divisors
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6069 题目: Counting Divisors Time Limit: 10000/5000 ...
- yarn nodes label (yarn 划分子集群)
yarn node labels 特性给节点打标签可以把特性类似的节点分成一组,这样可以指定特定的应用执行在特定的机器群上.现在我们只支持节点划分,1.一个节点仅能有一个节点划分,即一个节点只能打一个 ...
- netty12---线程池简单源码
package com.cn; import java.io.IOException; import java.nio.channels.Selector; import java.util.Queu ...
- CSS 媒体类型
CSS 媒体类型 媒体类型允许你指定文件将如何在不同媒体呈现.该文件可以以不同的方式显示在屏幕上,在纸张上,或听觉浏览器等等. 一.媒体类型 一些CSS属性只设计了某些媒体.例如"voice ...
- goland 使用简单配置-不断更新
1.格式化代码 Ctrl+Alt+L 格式化代码 2.快捷键可能被占用 用tools-file watchers file->setting->tools->file watche ...
- golang中文字符编码转换
golang 有很多需要将中文转成utf8的 网上搜到一个直接转的,记录下,备用 package main import "golang.org/x/text/encoding/simpli ...
- RN中有两种方式使用全局变量
1.通过导入导出文件的方式 新建constants.js文件 const object = { website:'http://www.hao123.com', name:'好123', }; exp ...