MyBatis Generator原生提供的生成方式targetRuntime有几种,但都不符合项目需求或想自定义自己的方法。

  网上的文章也很多:
  
  如:http://generator.sturgeon.mopaas.com/reference/extending.html
  
  这里我说下我的做法:   1、继承IntrospectedTableMyBatis3Impl,重写自己要改写的方法     InsoIntrospectedTable.java
    重写calculateXmlMapperGenerator使用自己的XMLMapperGenerator
    重写createJavaClientGenerator使用自己的JavaMapperGenerator

    我的做法比较粗暴,就是注释掉原来的逻辑,自己new自己的替代原来的。
public class InsoIntrospectedTable extends IntrospectedTableMyBatis3Impl {

    protected void calculateXmlMapperGenerator(AbstractJavaClientGenerator javaClientGenerator,
List<String> warnings,
ProgressCallback progressCallback) {
// if (javaClientGenerator == null) {
// if (context.getSqlMapGeneratorConfiguration() != null) {
// xmlMapperGenerator = new XMLMapperGenerator();
// }
// } else {
// xmlMapperGenerator = javaClientGenerator.getMatchedXMLGenerator();
// } xmlMapperGenerator = new InsoXMLMapperGenerator(); initializeAbstractGenerator(xmlMapperGenerator, warnings,
progressCallback);
} protected AbstractJavaClientGenerator createJavaClientGenerator() {
if (context.getJavaClientGeneratorConfiguration() == null) {
return null;
} // String type = context.getJavaClientGeneratorConfiguration()
// .getConfigurationType(); AbstractJavaClientGenerator javaGenerator;
// if ("XMLMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
// javaGenerator = new JavaMapperGenerator();
// } else if ("MIXEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
// javaGenerator = new MixedClientGenerator();
// } else if ("ANNOTATEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
// javaGenerator = new AnnotatedClientGenerator();
// } else if ("MAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
// javaGenerator = new JavaMapperGenerator();
// } else {
// javaGenerator = (AbstractJavaClientGenerator) ObjectFactory
// .createInternalObject(type);
// }
javaGenerator = new InsoJavaMapperGenerator(); return javaGenerator;
} }

  2、继承XMLMapperGenerator,重写自己要改写的方法

  InsoXMLMapperGenerator.java在getSqlMapElement方法中,可以添加或删除自己要的方法,这里我只添加了一个selectAll方法

public class InsoXMLMapperGenerator extends XMLMapperGenerator {

    public InsoXMLMapperGenerator() {
super();
} protected XmlElement getSqlMapElement() {
FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
progressCallback.startTask(getString(
"Progress.12", table.toString())); //$NON-NLS-1$
XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$
namespace)); context.getCommentGenerator().addRootComment(answer); addResultMapWithoutBLOBsElement(answer);
addResultMapWithBLOBsElement(answer);
addExampleWhereClauseElement(answer);
addMyBatis3UpdateByExampleWhereClauseElement(answer);
addBaseColumnListElement(answer);
addBlobColumnListElement(answer);
addSelectByExampleWithBLOBsElement(answer);
addSelectByExampleWithoutBLOBsElement(answer);
addSelectByPrimaryKeyElement(answer);
addDeleteByPrimaryKeyElement(answer);
addDeleteByExampleElement(answer);
addInsertElement(answer);
addInsertSelectiveElement(answer);
addCountByExampleElement(answer);
addUpdateByExampleSelectiveElement(answer);
addUpdateByExampleWithBLOBsElement(answer);
addUpdateByExampleWithoutBLOBsElement(answer);
addUpdateByPrimaryKeySelectiveElement(answer);
addUpdateByPrimaryKeyWithBLOBsElement(answer);
addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
//add select all
addSimpleSelectAllElement(answer); return answer;
} protected void addSimpleSelectAllElement(
XmlElement parentElement) {
if (introspectedTable.getRules()
.generateSelectByPrimaryKey()) {
AbstractXmlElementGenerator elementGenerator = new InsoSelectAllElementGenerator();
initializeAndExecuteGenerator(elementGenerator, parentElement);
}
}
}
  3、继承JavaMapperGenerator,重写自己要改写的方法
  InsoJavaMapperGenerator.java在getCompilationUnits方法中定制自己要的方法,这里我只添加了一个selectAll方法。
  注意:这里的项要与上面的XMLMapperGenerator一一对应,其它情况,我没有研究(比较菜。。。)
public class InsoJavaMapperGenerator extends JavaMapperGenerator {

    @Override
public List<CompilationUnit> getCompilationUnits() {
progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$
introspectedTable.getFullyQualifiedTable().toString()));
CommentGenerator commentGenerator = context.getCommentGenerator(); FullyQualifiedJavaType type = new FullyQualifiedJavaType(
introspectedTable.getMyBatis3JavaMapperType());
Interface interfaze = new Interface(type);
interfaze.setVisibility(JavaVisibility.PUBLIC);
commentGenerator.addJavaFileComment(interfaze); String rootInterface = introspectedTable
.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
if (!stringHasValue(rootInterface)) {
rootInterface = context.getJavaClientGeneratorConfiguration()
.getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
} if (stringHasValue(rootInterface)) {
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(
rootInterface);
interfaze.addSuperInterface(fqjt);
interfaze.addImportedType(fqjt);
} addCountByExampleMethod(interfaze);
addDeleteByExampleMethod(interfaze);
addDeleteByPrimaryKeyMethod(interfaze);
addInsertMethod(interfaze);
addInsertSelectiveMethod(interfaze);
addSelectByExampleWithBLOBsMethod(interfaze);
addSelectByExampleWithoutBLOBsMethod(interfaze);
addSelectByPrimaryKeyMethod(interfaze);
addUpdateByExampleSelectiveMethod(interfaze);
addUpdateByExampleWithBLOBsMethod(interfaze);
addUpdateByExampleWithoutBLOBsMethod(interfaze);
addUpdateByPrimaryKeySelectiveMethod(interfaze);
addUpdateByPrimaryKeyWithBLOBsMethod(interfaze);
addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze);
//增加selectAll
addSelectAllMethod(interfaze); List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
if (context.getPlugins().clientGenerated(interfaze, null,
introspectedTable)) {
answer.add(interfaze);
} List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
if (extraCompilationUnits != null) {
answer.addAll(extraCompilationUnits);
} return answer;
} /**
* 增加eelectAll
* @param interfaze
*/
protected void addSelectAllMethod(Interface interfaze) {
if (introspectedTable.getRules()
.generateSelectByPrimaryKey()) {
AbstractJavaMapperMethodGenerator methodGenerator = new SelectAllMethodGenerator();
initializeAndExecuteGenerator(methodGenerator, interfaze);
}
}

  4、配置XML使用上面自定义targetRuntime

   inso-generator\src\main\resources\generatorConfig-sys.xml

  • targetRuntime中配置全路径类名
  • 在table节点,可以配置一些开关,是否生成某些方法,如下,我关掉生成ByExample的方法
  • 在table节点里,属性ignoreQualifiersAtRuntime,默认为false,如果设置为true,在生成的SQL中,table名字不会加上catalog或schema,此配置,在Oracle下好用。
<?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="default" targetRuntime="com.xxcomp.core.generator.codegen.InsoIntrospectedTable" defaultModelType="flat">
<plugin type="com.xxcomp.core.generator.plugin.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<plugin type="com.xxcomp.core.generator.plugin.MapperPlugin">
<property name="targetProject" value="../inso-sys-service/src/main/java"/>
<property name="targetPackage" value="com.xxcomp.dao.generator"/>
<property name="expandTargetPackage" value="com.xxcomp.dao.sys"/>
</plugin>
<commentGenerator>
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@192.168.1.19:1521:EEMS"
userId="TEST" password="TEST">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.xxcomp.model.generator" targetProject="../inso-sys-api/src/main/java">
<property name="constructorBased" value="false"/>
<property name="useActualColumnNames" value="true" />
<property name="enableSubPackages" value="false"/>
<property name="immutable" value="false"/>
<property name="trimStrings" value="true"/>
<property name="rootClass" value="com.xxcomp.core.base.BaseModel"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mappers.generator" targetProject="../inso-sys-service/src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.xxcomp.dao.generator" targetProject="../inso-sys-service/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value=""/>
<property name="methodNameCalculator" value=""/>
<property name="rootInterface" value="com.xxcomp.core.base.BaseMapper"/>
</javaClientGenerator>
<table tableName="SYS_%" catalog="INSO" schema="INSO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!-- 默认为false,如果设置为true,在生成的SQL中,table名字不会加上catalog或schema; -->
<property name="ignoreQualifiersAtRuntime" value="true"/>
</table>
</context>
</generatorConfiguration>

  5、pom文件参考

  项目原型是ibase4j(http://git.oschina.net/iBase4J/iBase4J),此pom仅供参考一下吧  

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>inso-generator</artifactId>
<name>inso-generator</name>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<parent>
<groupId>com.xxcomp</groupId>
<artifactId>inso</artifactId>
<version>0.5.0</version>
</parent> <!-- 使用不同配置文件生成不同项目的MyBatis文件 -->
<!-- install mybatis-generator:generate -DconfigurationFile=generatorConfig-scheduler.xml -->
<!-- install mybatis-generator:generate -DconfigurationFile=generatorConfig-sys.xml --> <build>
<finalName>${project.name}</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<testIncludes>
<testInclude>none</testInclude>
</testIncludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jdeps-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>jdkinternals</goal>
<goal>test-jdkinternals</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/${configurationFile}</configurationFile>
</configuration>
<dependencies>
<!--
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
-->
<!-- 导入Oracle数据库链接jar包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>com.xxcomp</groupId>
<artifactId>inso-generator</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
  通过以上的改造,可以简单自定义组装一些原有的方法,生成自己需要的sqlMap,进一步还可以自己编写具体的方法实现,生成真正自定义的sql。(此步,我没有去做,太懒了。。)
  
  从没写过文章,真不会写,若看不明白或对你没帮忙,大侠请自动忽略。   写一写才发觉,写这个有点费时间,虽然我只是贴一下。   2016-09-27 10:31:24

定制自己的mybatis生成的更多相关文章

  1. mybatis生成的pojo 中的属性或方法不够我们当做dto使用时

    我们在写代码的时候,如果一个 mybatis生成的pojo 中的属性或方法不够我们使用(当做dto和前台交互)时,我们有两种方法: 第一: 直接在 原 pojo 中增加属性或者方法 第二:我们可以再写 ...

  2. 用mybatis生成插件自动生成配置文件

    1.在当前的maven项目的pom.xml中添加插件 <build> <plugins> <plugin> <groupId>org.mybatis.g ...

  3. mybatis 生成 映射文件生成工具 mybatisGenerator 使用

    第一:新建 generatorConfig.xml 文件 ,写入下面的 内容 <?xml version="1.0" encoding="UTF-8"?& ...

  4. mybatis 生成代码配置 mybatis-generator:generate 的使用详解

    一.环境 mysql+eclipse 二.代码配置 pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...

  5. 定制个性化的FlashPaper生成的文件

    1:找到已安装FlashPaper目录下的子目录Interface下的文件DefaultViewer2.swf,在此swf文件的基础上实现自己的修改. 2:利用swf反编译工具,这里推荐 硕思闪客精灵 ...

  6. oracle 用mybatis生成主键

    oracle主键是不能像mysql一样自动管理的,需要自己手动管理,先生成,再插入. <selectKey keyProperty="id" resultType=" ...

  7. XML Parser Error on line 1: 前言中不允许有内容, Mybatis 生成代码

    使用用notepad++打开xml文件,然后在菜单“格式”中选择“以UTF-8无BOM格式编码”,保存.

  8. Intellij IDEA 2016 mybatis 生成 mapper

    转载地址:http://gold.xitu.io/entry/57763ab77db2a2005517ae3f

  9. mybatis Generator生成代码及使用方式

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...

随机推荐

  1. ubuntu升级内核后vmware-player启动失败

    在虚拟机软件中,vmware player是对硬件支持很好的,通过它可以很方便的使用网银.单片机开发等等工作.但是最近ubuntu每次升级内核后,vmware都会启动失败,提示:Before you ...

  2. OData的初步认识

    What – OData是什么? OData - Open Data Protocal,是一个设计和使用RESTful API的标准.REST本身只是一个构建web服务的思想和理念,其没有规定一个统一 ...

  3. Java 浅析三大特性之一封装

    在说Java 三个特性之前,我们先了解一下什么是面向对象,以及为什么Java是面向对象的语言. 面向对象是区别于面向过程的一种编程的思想.我们可以通过这个例子冰箱装大象的例子来了解一下面向对象与面向过 ...

  4. win7+IIS7下木有4.0框架问题的解决方案

  5. 【WP开发】记录屏幕操作

    在某些应用中,比如游戏,有时候需要将用户的操作记录下来.ScreenCapture类提供了这个功能.但必须注意的是:此屏幕记录功能只对当前应用程序的屏幕有效,即只有当前应用程序在前台运行时才有效. 与 ...

  6. 那些让IE6-8羞愧的替补型js

    1,html5shiv 这个js特别简单,可以让IE8识别一些新的标签,常用的比如 header footor section,就能使用更好的语义的标签了. 引入方式: <!--[if lt I ...

  7. ELF文件

    ELF文件格式是一个开发标准,各种UNIX系统的可执行文件都采用ELF格式,它有三种不同的类型: 可重定位的目标文件 可执行文件 共享库 现在分析一下上一篇文章中经过汇编之后生成的目标文件max.o和 ...

  8. 前端学PHP之数据类型

    × 目录 [1]总括 [2]布尔型 [3]整型[4]浮点型[5]字符串[6]数组[7]对象[8]NULL[9]资源 前面的话 同javascript一样,php也是一门弱类型语言,或者说成类型松散的语 ...

  9. 深入学习jQuery选择器系列第七篇——表单选择器

    × 目录 [1]表单元素 [2]对象属性 前面的话 无论是提交还是传递数据,表单元素在动态交互页面的作用是非常重要的.jQuery专门加入了表单选择器,从而能够极其方便地获取到某个类型的表单元素 表单 ...

  10. POJ2513-Colored Sticks

    /*思路:类似图论中“一笔画”问题,两根木棒的相连接的端点是一样的颜色,(a,b)--(b,c)--(c, d)....方法:trie树+并查集, 利用trie树建立字符串和某一个节点的映射,并将这些 ...