项目里新建表时model,mapper以及mapper.xml基本都是用Mybatis Generator(以下简称为MBG)自动生成的,但是MBG自动生成的model的注释实在有点非人类,至少中国人是完全接受不了的,在配置中禁用掉注释吧,倒是简单了,可是生成的model类光秃秃的,啥都没有,字段方法没有注释,使用很不方便,别人看也不知道这个字段是啥含义,到最后还是要自己添加,一张表多点几十个字段,特么添加累死了,不能这么干,得想法子,百度了一下网上,基本有两种方法,第一种,就是直接修改MGB的源代码,第二种就是自己写一个类实现CommentGenerator接口,先说自己写一个新类实现CommentGenerator接口的方法来使自动生成的model类含有中文注释吧.

一:首先你得有一个maven项目,这个我就不多踢

二:通过mybatis generator实现自动生产model,mapper以及mapper.xml,具体参考 Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码

三:通过查看你会发现,注解类是在mybatis-generator-core jar包中  org.mybatis.generator.internal包下的DefaultCommentGenerator类。那么这里有两种方法,一种是重写DefaultCommentGenerator类,另外一种中修改jar包中的源码,但很明显第一种方法比较简单,这里也只介绍第一种方法的写法。

3.1在源代码中新建一个类MyCommentGenerator,实现CommentGenerator接口,类的代码如下:

package org.mybatis.generator;/*** Created by 草帽boy on 2017/2/16.* mybatis generator 自定义comment生成器.* 基于MBG 1.3.2.* @author ZhangAY 2016-02-19*/
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;public class MyCommentGenerator implements CommentGenerator {private Properties properties;private Properties systemPro;private boolean suppressDate;private boolean suppressAllComments;private String currentDateStr;public MyCommentGenerator() {super();properties = new Properties();systemPro = System.getProperties();suppressDate = false;suppressAllComments = false;currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());}public void addJavaFileComment(CompilationUnit compilationUnit) {// add no file level comments by defaultreturn;}/*** Adds a suitable comment to warn users that the element was generated, and* when it was generated.*/public void addComment(XmlElement xmlElement) {return;}public void addRootComment(XmlElement rootElement) {// add no document level comments by defaultreturn;}public void addConfigurationProperties(Properties properties) {this.properties.putAll(properties);suppressDate = StringUtility.isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));suppressAllComments = StringUtility.isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));}/*** This method adds the custom javadoc tag for. You may do nothing if you do* not wish to include the Javadoc tag - however, if you do not include the* Javadoc tag then the Java merge capability of the eclipse plugin will* break.** @param javaElement the java element*/protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {javaElement.addJavaDocLine(" *");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(MergeConstants.NEW_ELEMENT_TAG);if (markAsDoNotDelete) {sb.append(" do_not_delete_during_merge");}String s = getDateString();if (s != null) {sb.append(' ');sb.append(s);}javaElement.addJavaDocLine(sb.toString());}/*** This method returns a formated date string to include in the Javadoc tag* and XML comments. You may return null if you do not want the date in* these documentation elements.** @return a string representing the current timestamp, or null*/protected String getDateString() {String result = null;if (!suppressDate) {result = currentDateStr;}return result;}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerClass.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());sb.append(" ");sb.append(getDateString());innerClass.addJavaDocLine(sb.toString());innerClass.addJavaDocLine(" */");}public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerEnum.addJavaDocLine("/**");//      addJavadocTag(innerEnum, false);sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());innerEnum.addJavaDocLine(sb.toString());innerEnum.addJavaDocLine(" */");}public void addFieldComment(Field field, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();field.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedColumn.getRemarks());field.addJavaDocLine(sb.toString());//      addJavadocTag(field, false);
field.addJavaDocLine(" */");}public void addFieldComment(Field field, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();field.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());field.addJavaDocLine(sb.toString());field.addJavaDocLine(" */");}public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {}public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}//      method.addJavaDocLine("/**");//      addJavadocTag(method, false);//      method.addJavaDocLine(" */");
    }public void addGetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}method.addJavaDocLine("/**");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());sb.setLength(0);sb.append(" * @return ");sb.append(introspectedColumn.getActualColumnName());sb.append(" ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());//      addJavadocTag(method, false);
method.addJavaDocLine(" */");}public void addSetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}method.addJavaDocLine("/**");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());Parameter parm = method.getParameters().get(0);sb.setLength(0);sb.append(" * @param ");sb.append(parm.getName());sb.append(" ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());//      addJavadocTag(method, false);
method.addJavaDocLine(" */");}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerClass.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());innerClass.addJavaDocLine(sb.toString());sb.setLength(0);sb.append(" * @author ");sb.append(systemPro.getProperty("user.name"));sb.append(" ");sb.append(currentDateStr);//      addJavadocTag(innerClass, markAsDoNotDelete);
innerClass.addJavaDocLine(" */");}
}

3.2.再新建一个类StartUp,用于运行项目,也就是运行StartUp类 就会直接生成model,mapper以及mapper.xml,类的代码如下:

package org.mybatis.generator;/*** Created by 草帽boy on 2017/2/16.* 启动文件,只需要点击运行就行*/
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;public class StartUp {public static void main(String[] args) throws URISyntaxException {try {List<String> warnings = new ArrayList<String>();boolean overwrite = true;//直接获取generatorConfig.xml的文件路径 根据具体情况查看File configFile = new File("E:\\IDEAWorkPlace\\GraduationDesign\\CourseDesignManageSystem\\20170122\\CourseDesignManage\\src\\main\\resources\\generatorConfig.xml");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);} catch (SQLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (InvalidConfigurationException e) {e.printStackTrace();} catch (XMLParserException e) {e.printStackTrace();}}
}

3.3 另外需要简单修改一下generatorConfig.xml中的注释配置

  <commentGenerator type="org.mybatis.generator.MyCommentGenerator"><!-- <property name="suppressDate" value="true"/>&lt;!&ndash; 是否去除自动生成的注释 true:是 : false:否 &ndash;&gt;<property name="suppressAllComments" value="false"/>--></commentGenerator>

commentGenerator 的type是你刚刚重构的MyCommentGenerator类的位置

3.4其中我的文件结构如下:

四:点击运行StartUp,你会发现model,mapper以及mapper.xml都已经产生,其中实体类的 运行的结果如下

更多参考:

mybatis-generator自定义注释生成

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

转载于:https://www.cnblogs.com/luffyu/p/6408321.html

关于 mybatis-generator自定义注释生成 使用DefaultCommentGenerator重写来完成相关推荐

  1. mybatis generator自定义逆向工程防覆盖sql代码

    Mybatis generator 自定义逆向工程防覆盖sql 在项目中常常有数据库的变更,我们会常用到mybats generator逆向工程来为我们更新项目中的sql语句及entity实体,此时会 ...

  2. 【Spring框架家族】mybatis generator代码自动生成(看得上眼直接拿去用即可)

    小工具,直接上代码,有大佬看的上,拿去复制改改相关参数就好 基于idea上使用的 pom文件 <build><plugins><plugin><groupId ...

  3. mybatis generator 中文注释_mybatis代码生成器,提高开发效率

    出处:https://www.cnblogs.com/homejim/p/9782403.html 在使用 mybatis 过程中, 当手写 JavaBean和XML 写的越来越多的时候, 就越来越同 ...

  4. mybatis generator 中文注释_[SpringBoot2.X] 23- 整合持久层技术 -MyBatis - 配置

    整合MyBatis 1搭建项目环境 1.1.1创建项目--略 11.2修改POM文件,添加相关依赖 <dependency><groupId>org.springframewo ...

  5. idea mybaits逆向工程_IDEA 中集成 MyBatis Generator 组件逆向生成工程

    IDEA 逆向 MyBatis 工程时,不像支持 Hibernate 那样有自带插件,需要集成第三方的 MyBatis Generator. MyBatis Generator的详细介绍 http:/ ...

  6. MyEclipse下安装MyBatis Generator代码反向生成工具

    在http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/下载 features/ plug ...

  7. mybatis generator修改默认生成的sql模板

    相关连接: mybatis-generator扩展教程系列 -- 自定义sql xml文件 git项目地址 转载于:https://www.cnblogs.com/hujunzheng/p/71105 ...

  8. mybatis generator 中文注释_Mybatis提供的逆向工程

    首先需要了解一个软件:PowerDesigner--可以用来创建数据库表的脚本文件 如果数据库已经构建成功了,那么在项目中分为三步走的方式来构建逆向工程. 第一步:pom文件添加插件 <plug ...

  9. 使用MyBatis Generator自动生成实体、mapper和dao层

    原文链接 通过MyBatis Generator可以自动生成实体.mapper和dao层,记录一下怎么用的. 主要步骤: 关于mybatis从数据库反向生成实体.DAO.mapper: 参考文章:ht ...

最新文章

  1. java 使用正则表达式过滤HTML中标签
  2. 前端交接文档_开发型Web前端和设计型Web前端的区别是什么?
  3. 20155201 网络攻防技术 实验六 信息搜集与漏洞
  4. 13.文件:因为懂你,所以永恒
  5. Network 【TCP/IP 四层模型】
  6. 云计算机是一种基于资源,一种基于云平台和云计算的资源管理系统和方法
  7. 以阿里云RPA为例,来告诉你到底RPA是什么?
  8. 机器学习知识总结系列- 特征工程(1-1)
  9. c++远征之继承篇——继承方式
  10. dataframe 众数的方法_pandas 第11篇:DataFrame-数据处理(分组、聚合、窗口、相关、统计)...
  11. 【Kafka】Illegal unquoted character ((CTRL-CHAR, code 0)): has to be escaped using backs
  12. Spark Architecture
  13. 青蛙跳台阶(剑指 Offer 10- II)
  14. Android四大组件(activity task stack)
  15. 使用Daemontools监控管理服务
  16. dnf时装预览怎么打开_dnf时装预览怎么打开_dnf怎么查找各职业时装代码
  17. 解决征信中心密码控件无法安装
  18. html 导出 excel 列宽,Html2Excel 更名为 MyExcel,2.1.0 版本发布!
  19. 成为会带团队的技术人 大项目:把握关键点,谋定而后动
  20. Nvidia30系显卡+Ubuntu系统的CUDA 11安装100%成功教程

热门文章

  1. C语言常见单链表面试题(2)
  2. 黑马程序员-异常介绍与处理
  3. SQL Server 存储过程的应用
  4. SAP BAPI的一些初级资料
  5. document.all
  6. 2021_Nov_9_Supervision_STEMM_What_You_Need_In_Advance?
  7. Stanford 研究领域
  8. 开发:随笔记录之 Json字符串和对象的相互转换
  9. leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法
  10. JFrame中使用jpanel来布局