目标

  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. java poi解析excel日期为数字的问题

    这个数字是什么呢?是以1900年为原点,到2015年8月21日,之间经过的天数. 知道这个后,就很好处理了,我们拿到1900年的日期,在这个日期上加上42237天即可.如下: Calendar cal ...

  2. 部署apollo-client到maven私服上时遇到的问题及排查过程

    场景回顾: 应用客户端如果需要接入到Apollo配置服务中心的话,需要引用apollo-client的依赖包使之与config-server保持连接,从而可以及时的收到更新之后的配置信息. 1.将ap ...

  3. Mybatis入门配置

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  4. Http协议面试题

    1.说一下什么是Http协议? 对器客户端和 服务器端之间数据传输的格式规范,格式简称为“超文本传输协议”. 2.什么是Http协议无状态协议?怎么解决Http协议无状态协议?(曾经去某创业公司问到) ...

  5. TOSCA自动化测试工具--建立测试用例

    1.测试链接 demowebshop.tricentis.com 测试login 2.检查元素 3.Modules模块,建立自己的文件夹,右键Scan Application , Desktop 4. ...

  6. JAVA BIO与NIO、AIO的区别

    IO的方式通常分为几种,同步阻塞的BIO.同步非阻塞的NIO.异步非阻塞的AIO. 一.BIO 在JDK1.4出来之前,我们建立网络连接的时候采用BIO模式,需要先在服务端启动一个ServerSock ...

  7. ansible playbook基本操作

    一.ansible playbook简单使用 相当于是把模块写入到配置文件里面 vim /etc/ansible/test.yml //写入如下内容: --- - hosts: 127.0.0.1 r ...

  8. iOS 性能监测

    给些链接: http://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ%3D%3D&idx=1&mid=207890859&scene=23& ...

  9. awk十三问-【AWK学习之旅】

    ---===AWK学习之旅===--- 十三个常用命令行处理   [root@monitor awkdir]# cat emp.txt Beth 4.00 0 Dan 3.75 0 Kathy 4.0 ...

  10. 20145324 Java实验四

    在IDEA上操作 由于不会创建安卓模拟器失败 选择老师给的插件 成功 实验总结 开始开发安卓,感觉更难了,这次实验完全是看运气拼电脑的实验! 步骤 耗时 百分比 需求分析 10m 17% 设计 20m ...