在看本篇之前,最好先看一下上一篇通过实现CommentGenerator接口的方法来实现中文注释的例子,因为很多操作和上一篇基本是一致的,所以本篇可能不那么详细.

首先说一下上篇通过实现CommentGenerator接口的一些不足,毕竟只是实现了CommentGenerator接口,在里面的方法再怎么改,有效的也只是针对model类,并且使用的人大概也发现了,里面的addClassComment方法都知道是在类文件上面生成注释,但是无论我们在这个方法实现里写什么都没有效果,其实因为MGB默认是没有调用这个方法的,这个时候如果有需求希望生成的类文件自动加了类文档说明就办不到了,而如果在源代码的基础上修改,就好办多了,想怎么改就怎么改,只要找对地方,什么样的需要都可以自己写代码来实现.

  • 首先一样,自己新建一个maven项目,怎么做就不详细说了,前一篇有,都弄好后,修改pom.xml文件,首先引入MBG的源码,在pom.xml中加入依赖
  1.     <dependency>
  2. <groupId>org.mybatis.generator</groupId>
  3. <artifactId>mybatis-generator-core</artifactId>
  4. <version>1.3.</version>
  5. </dependency>

  保存过后就会自动下载mybatis-generator-core的jar包,然后我们到自己的本地仓库中去找存放这个jar包的位置,下载下来后pom.xml中这个依赖可以删除了.

我们会看到不仅下载了jar包,源代码也一起下载了,用压缩软件打开source,jar,复制org文件夹及其内所有文件到自己的eclipse建立的maven项目中,这样就取得了MBG的源代码,你也可以在

http://search.maven.org中搜索mybatis-generator-core然后点击source.jar来直接下载源码.

  • 第一步得到源代码后之后,复制到项目中我们会发现是报错的,因为有依赖包没有引入,缺少log4j和ant包,在pom.xml中添加上依赖,顺便添加了oracle和mysql的驱动程序包,oracle的本地安装从上一篇中可以看到,加入后项目就没有错误了
  1.      <dependency>
  2. <groupId>com.oracle</groupId>
  3. <artifactId>ojdbc6</artifactId>
  4. <version>6.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <version>5.1.9</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>log4j</groupId>
  13. <artifactId>log4j</artifactId>
  14. <version>1.2.7</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.ant</groupId>
  18. <artifactId>ant</artifactId>
  19. <version>1.9.0</version>
  20. </dependency>

这个时候我们的项目结构应该是这个样子的

然后就很简单了,在源代码里找到 org.mybatis.generator.internal.DefaultCommentGenerator类,随便改吧,自己diy,以下是一个样本

  1. /*
  2. * Copyright 2008 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package org.mybatis.generator.internal;
  18.  
  19. import static org.mybatis.generator.internal.util.StringUtility.isTrue;
  20.  
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.Properties;
  24.  
  25. import org.mybatis.generator.api.CommentGenerator;
  26. import org.mybatis.generator.api.IntrospectedColumn;
  27. import org.mybatis.generator.api.IntrospectedTable;
  28. import org.mybatis.generator.api.dom.java.CompilationUnit;
  29. import org.mybatis.generator.api.dom.java.Field;
  30. import org.mybatis.generator.api.dom.java.InnerClass;
  31. import org.mybatis.generator.api.dom.java.InnerEnum;
  32. import org.mybatis.generator.api.dom.java.JavaElement;
  33. import org.mybatis.generator.api.dom.java.Method;
  34. import org.mybatis.generator.api.dom.java.Parameter;
  35. import org.mybatis.generator.api.dom.xml.XmlElement;
  36. import org.mybatis.generator.config.MergeConstants;
  37. import org.mybatis.generator.config.PropertyRegistry;
  38.  
  39. /**
  40. * @author Jeff Butler
  41. *
  42. */
  43. public class DefaultCommentGenerator implements CommentGenerator {
  44.  
  45. private Properties properties;
  46. private Properties systemPro;
  47. private boolean suppressDate;
  48. private boolean suppressAllComments;
  49. private String currentDateStr;
  50.  
  51. public DefaultCommentGenerator() {
  52. super();
  53. properties = new Properties();
  54. systemPro = System.getProperties();
  55. suppressDate = false;
  56. suppressAllComments = false;
  57. currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
  58. }
  59.  
  60. public void addJavaFileComment(CompilationUnit compilationUnit) {
  61. // add no file level comments by default
  62. return;
  63. }
  64.  
  65. /**
  66. * Adds a suitable comment to warn users that the element was generated, and
  67. * when it was generated.
  68. */
  69. public void addComment(XmlElement xmlElement) {
  70. return;
  71. }
  72.  
  73. public void addRootComment(XmlElement rootElement) {
  74. // add no document level comments by default
  75. return;
  76. }
  77.  
  78. public void addConfigurationProperties(Properties properties) {
  79. this.properties.putAll(properties);
  80.  
  81. suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
  82.  
  83. suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
  84. }
  85.  
  86. /**
  87. * This method adds the custom javadoc tag for. You may do nothing if you do
  88. * not wish to include the Javadoc tag - however, if you do not include the
  89. * Javadoc tag then the Java merge capability of the eclipse plugin will
  90. * break.
  91. *
  92. * @param javaElement
  93. * the java element
  94. */
  95. protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
  96. javaElement.addJavaDocLine(" *");
  97. StringBuilder sb = new StringBuilder();
  98. sb.append(" * ");
  99. sb.append(MergeConstants.NEW_ELEMENT_TAG);
  100. if (markAsDoNotDelete) {
  101. sb.append(" do_not_delete_during_merge");
  102. }
  103. String s = getDateString();
  104. if (s != null) {
  105. sb.append(' ');
  106. sb.append(s);
  107. }
  108. javaElement.addJavaDocLine(sb.toString());
  109. }
  110.  
  111. /**
  112. * This method returns a formated date string to include in the Javadoc tag
  113. * and XML comments. You may return null if you do not want the date in
  114. * these documentation elements.
  115. *
  116. * @return a string representing the current timestamp, or null
  117. */
  118. protected String getDateString() {
  119. String result = null;
  120. if (!suppressDate) {
  121. result = currentDateStr;
  122. }
  123. return result;
  124. }
  125.  
  126. public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
  127. if (suppressAllComments) {
  128. return;
  129. }
  130. StringBuilder sb = new StringBuilder();
  131. innerClass.addJavaDocLine("/**");
  132. sb.append(" * ");
  133. sb.append(introspectedTable.getFullyQualifiedTable());
  134. sb.append(" ");
  135. sb.append(getDateString());
  136. innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
  137. innerClass.addJavaDocLine(" */");
  138. }
  139.  
  140. public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
  141. if (suppressAllComments) {
  142. return;
  143. }
  144. StringBuilder sb = new StringBuilder();
  145. innerEnum.addJavaDocLine("/**");
  146. sb.append(" * ");
  147. sb.append(introspectedTable.getFullyQualifiedTable());
  148. innerEnum.addJavaDocLine(sb.toString().replace("\n", " "));
  149. innerEnum.addJavaDocLine(" */");
  150. }
  151.  
  152. public void addFieldComment(Field field, IntrospectedTable introspectedTable,
  153. IntrospectedColumn introspectedColumn) {
  154. if (suppressAllComments) {
  155. return;
  156. }
  157. StringBuilder sb = new StringBuilder();
  158. field.addJavaDocLine("/**");
  159. sb.append(" * ");
  160. sb.append(introspectedColumn.getRemarks());
  161. field.addJavaDocLine(sb.toString().replace("\n", " "));
  162. field.addJavaDocLine(" */");
  163. }
  164.  
  165. public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
  166. if (suppressAllComments) {
  167. return;
  168. }
  169. StringBuilder sb = new StringBuilder();
  170. field.addJavaDocLine("/**");
  171. sb.append(" * ");
  172. sb.append(introspectedTable.getFullyQualifiedTable());
  173. field.addJavaDocLine(sb.toString().replace("\n", " "));
  174. field.addJavaDocLine(" */");
  175. }
  176.  
  177. public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
  178. if (suppressAllComments) {
  179. return;
  180. }
  181. // method.addJavaDocLine("/**");
  182. // addJavadocTag(method, false);
  183. // method.addJavaDocLine(" */");
  184. }
  185.  
  186. public void addGetterComment(Method method, IntrospectedTable introspectedTable,
  187. IntrospectedColumn introspectedColumn) {
  188. if (suppressAllComments) {
  189. return;
  190. }
  191. method.addJavaDocLine("/**");
  192. StringBuilder sb = new StringBuilder();
  193. sb.append(" * ");
  194. sb.append(introspectedColumn.getRemarks());
  195. method.addJavaDocLine(sb.toString().replace("\n", " "));
  196. sb.setLength(0);
  197. sb.append(" * @return ");
  198. sb.append(introspectedColumn.getActualColumnName());
  199. sb.append(" ");
  200. sb.append(introspectedColumn.getRemarks());
  201. method.addJavaDocLine(sb.toString().replace("\n", " "));
  202. method.addJavaDocLine(" */");
  203. }
  204.  
  205. public void addSetterComment(Method method, IntrospectedTable introspectedTable,
  206. IntrospectedColumn introspectedColumn) {
  207. if (suppressAllComments) {
  208. return;
  209. }
  210. method.addJavaDocLine("/**");
  211. StringBuilder sb = new StringBuilder();
  212. sb.append(" * ");
  213. sb.append(introspectedColumn.getRemarks());
  214. method.addJavaDocLine(sb.toString().replace("\n", " "));
  215. Parameter parm = method.getParameters().get(0);
  216. sb.setLength(0);
  217. sb.append(" * @param ");
  218. sb.append(parm.getName());
  219. sb.append(" ");
  220. sb.append(introspectedColumn.getRemarks());
  221. method.addJavaDocLine(sb.toString().replace("\n", " "));
  222. method.addJavaDocLine(" */");
  223. }
  224.  
  225. public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
  226. if (suppressAllComments) {
  227. return;
  228. }
  229. StringBuilder sb = new StringBuilder();
  230. innerClass.addJavaDocLine("/**");
  231. sb.append(" * 描述:");
  232. sb.append(introspectedTable.getFullyQualifiedTable()+"表的实体类");
  233. innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
  234. sb.setLength(0);
  235. sb.append(" * @version");
  236. innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
  237. sb.setLength(0);
  238. sb.append(" * @author: ");
  239. sb.append(systemPro.getProperty("user.name"));
  240. innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
  241. sb.setLength(0);
  242. sb.append(" * @创建时间: ");
  243. sb.append(currentDateStr);
  244. innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
  245. innerClass.addJavaDocLine(" */");
  246. }
  247. }

为了是辛辛苦苦写的addClassComment生效,还得找到org.mybatis.generator.codegen.mybatis3.model.BaseRecordGenerator类,在大约60行的地方,在commentGenerator.addJavaFileComment(topLevelClass);后加一句:

  1. commentGenerator.addClassComment(topLevelClass, introspectedTable,false);

这样自己的方法就调用到了,说明一下,仔细看一下源代码,会发现DefaultCommentGenerator类,会发现addClassComment方法有两个,一个加了boolean markAsDoNotDelete参数,一个没有加,你这里调用的如果不加false参数,就改DefaultCommentGenerator类中两个参数的addClassComment方法,我的样例中你会发现方法中传了参数也不管用,因为真正的源代码中是有这么一行的

然后addJavadocTag方法中是这么调用的

我的样例中addJavadocTag方法的调用都被删除了,所以传个参数也没啥用.

  • 其他的如果想有什么改动,就自己去研究源代码,然后自己手动改了,到这里就差不多结束了,剩下的工作和上次的一样,使用mvn clean package命令打成jar包,然后在控制台使用

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite命令运行,贴一下完整的pom.xml和generatorConfig.xml,部分地方和上次还是有区别的.

pom.xml:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>org.mybatis</groupId>
  6. <artifactId>mybatis-generator-coree</artifactId>
  7. <version>1.3.2</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>MybatisGenerator</name>
  11. <url>http://maven.apache.org</url>
  12.  
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16.  
  17. <dependencies>
  18. <dependency>
  19. <groupId>com.oracle</groupId>
  20. <artifactId>ojdbc6</artifactId>
  21. <version>6.0</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>mysql</groupId>
  25. <artifactId>mysql-connector-java</artifactId>
  26. <version>5.1.9</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>log4j</groupId>
  30. <artifactId>log4j</artifactId>
  31. <version>1.2.7</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.apache.ant</groupId>
  35. <artifactId>ant</artifactId>
  36. <version>1.9.0</version>
  37. </dependency>
  38. </dependencies>
  39.  
  40. <build>
  41. <finalName>mybatis-generator-core-1.3.2</finalName>
  42. <plugins>
  43. <plugin>
  44. <artifactId>maven-assembly-plugin</artifactId>
  45. <version>2.6</version>
  46. <configuration>
  47. <appendAssemblyId>false</appendAssemblyId>
  48. <archive>
  49. <manifest>
  50. <mainClass>org.mybatis.generator.api.ShellRunner</mainClass>
  51. </manifest>
  52. </archive>
  53. <descriptorRefs>
  54. <descriptorRef>jar-with-dependencies</descriptorRef>
  55. </descriptorRefs>
  56. </configuration>
  57. <executions>
  58. <execution>
  59. <id>make-assembly</id>
  60. <phase>package</phase>
  61. <goals>
  62. <goal>assembly</goal>
  63. </goals>
  64. </execution>
  65. </executions>
  66. </plugin>
  67. </plugins>
  68. </build>
  69. </project>

generatorConfig.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5.  
  6. <generatorConfiguration>
  7. <context id="context1" targetRuntime="MyBatis3">
  8. <!-- 指定生成的java文件的编码,没有直接生成到项目时中文可能会乱码 -->
  9. <property name="javaFileEncoding" value="UTF-8"/>
  10. <!-- oracle配置 -->
  11. <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
  12. connectionURL="jdbc:oracle:thin:@***:1521:orcl" userId="***"
  13. password="***">
  14. 针对oracle数据库
  15. <property name="remarksReporting" value="true"></property>
  16. </jdbcConnection> -->
  17.  
  18. <!-- mysql配置 -->
  19. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  20. connectionURL="jdbc:mysql://localhost:3306/bookshop" userId="root"
  21. password="root">
  22. <!-- 针对mysql数据库 -->
  23. <property name="useInformationSchema" value="true"></property>
  24. </jdbcConnection>
  25.  
  26. <javaTypeResolver>
  27. <property name="forceBigDecimals" value="false" />
  28. </javaTypeResolver>
  29.  
  30. <!-- dto class -->
  31. <javaModelGenerator targetPackage="model"
  32. targetProject="C:\Users\Administrator\Desktop">
  33. <property name="enableSubPackages" value="true" />
  34. <property name="trimStrings" value="true" />
  35. </javaModelGenerator>
  36.  
  37. <!-- mybatis xml file -->
  38. <sqlMapGenerator targetPackage="dao"
  39. targetProject="C:\Users\Administrator\Desktop">
  40. <property name="enableSubPackages" value="true" />
  41. </sqlMapGenerator>
  42.  
  43. <!-- mapper class -->
  44. <javaClientGenerator type="XMLMAPPER"
  45. targetPackage="dao" targetProject="C:\Users\Administrator\Desktop">
  46. <property name="enableSubPackages" value="true" />
  47. </javaClientGenerator>
  48.  
  49. <!--不生成帮助类(Exmaples) -->
  50. <!-- enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
  51. enableSelectByExample="false" selectByExampleQueryId="false" -->
  52. <!--已生成的表 <table schema="demo" tableName="USER" domainObjectName="User"></table> -->
  53. <table schema="" tableName="bookinfo" domainObjectName="BookInfo"
  54. enableCountByExample="false" enableUpdateByExample="false"
  55. enableDeleteByExample="false" enableSelectByExample="false"
  56. selectByExampleQueryId="false">
  57. </table>
  58.  
  59. </context>
  60. </generatorConfiguration>

这里使用的是默认的就不用指定commentGenerator节点的type属性了.生成后的model效果如下:

生成好的工具下载:

MyGenerator.rar

源码下载:

MyGenerator-source.rar

Mybatis Generator的model生成中文注释,支持oracle和mysql(通过修改源码的方式来实现)的更多相关文章

  1. Mybatis Generator的model生成中文注释,支持oracle和mysql(通过实现CommentGenerator接口的方法来实现)

    自己手动实现的前提,对maven项目有基本的了解,在本地成功搭建了maven环境,可以参考我之前的文章:maven环境搭建 项目里新建表时model,mapper以及mapper.xml基本都是用My ...

  2. springboot + mybatis 支持oracle和mysql切换含源码

    1.springboot 启动类加入bean 如下 // DatabaseIdProvider元素主要是为了支持不同的数据库@Beanpublic DatabaseIdProvider getData ...

  3. mybatis generator 使用教程(生成带注释的实体类)

    引言: 最近的一个项目,由于数据库表巨多,导致需要创建N多个java实体.dao.mapper.xml映射文件,如果均使用纯手工编写,无疑需要耗费大量时间和精力.于是上网学习了mybatis gene ...

  4. Eclipse 使用mybatis generator插件自动生成代码

    Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...

  5. Windows7 64位环境6sv2.1大气传输模型修改源码添加国产高分卫星GF-1 GF-2光谱响应支持

    下面开始添加国产卫星光谱响应的支持: 以下主要参考文章“6S大气传输模型修改源码添加.自定义卫星光谱响应(以HJ-1B CCD为例)”网址:http://blog.csdn.net/sam92/art ...

  6. 关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造)

    关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造) 摘要:在中文搜索中的标点.符号往往也是有语义的,比如我们要搜索“C++”或是“C#”,我们不希望搜索出来的全是“C”吧?那样对程 ...

  7. mybatis generator 生成中文注释

    mybatis generator默认生成 的注释太奇葩了,完全不能拿到生产去用,不过幸亏提供了接口可以自己扩展.长话短说,要生成如下的domain, package com.demo.domain; ...

  8. mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)

    我们都知道mybatis generator自动生成的注释没什么实际作用,而且还增加了代码量.如果能将注释从数据库中捞取到,不仅能很大程度上增加代码的可读性,而且减少了后期手动加注释的工作量. 1.首 ...

  9. JAVA入门[7]-Mybatis generator(MBG)自动生成mybatis代码

    一.新建测试项目 新建Maven项目MybatisDemo2,修改pom.xml引入依赖.dependencies在上节基础上新增 <dependency> <groupId> ...

随机推荐

  1. Python之json使用

    一.概念 json是一种通用的数据类型,任何语言都认识 接口返回的数据类型都是json 长得像字典,形式也是k-v { } 其实json是字符串 字符串不能用key.value来取值,要先转成字典才可 ...

  2. Ubuntu Linux Recovery Mode

    在安全模式/修復模式有以下的選項︰resume Resume normal boot繼續正常啟動作業,供不小心誤入此選單的使用者開機使用.(继续以正常模式启动) clean Try to make f ...

  3. 同事写得Python对页面压测脚本

    #!/usr/bin/env python # *-* coding:utf-8 *-* import threading import requests import time # headers ...

  4. Linux系统中常用的命令汇总

    日常开发,上线的服务器系统一般都是Linux系统,所以,熟练的掌握常用的命令操作就尤其的重要了 1) 查看某个服务的运行情况 (例如Redis) ps -ef | grep redis //e-显示程 ...

  5. java内存模型(转)

    前提知识: Java内存模型(JMM)是一个概念模型,底层是计算机的寄存器.缓存内存.主内存和CPU等.  多处理器环境下,共享数据的交互硬件设备之间的关系: JMM: 从以上两张图中,谈一谈以下几个 ...

  6. [转帖]firewall-cmd

    firewall-cmd https://wangchujiang.com/linux-command/c/firewall-cmd.html 高手大作 等哪天需要防火墙了 再练习一下. Linux上 ...

  7. Angular 自定义指令传参

    <!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...

  8. Entity Framework 6 自定义连接字符串ConnectionString连接MySQL

    在开始介绍之前,首先来看看官方对Entity Framework的解释:Entity Framework (EF) is an object-relational mapper that enable ...

  9. spring程序打包war,直接通过-jar启动,并指定spring.profiles.active参数控制多环境配置

    备注:spring boot有内嵌tomcat,jar项目可以用java -jar命令启动,war包也可以,且可以直接指定spring.profiles.active参数控制多环境配置 直接指定传参, ...

  10. 如何在mac下安装php

    步骤如下: 1.下载php源码并解压 2.进入php源码并configure 3.安装openssl 4.sudo make及make test 5.sudo make install 具体命令如下: ...