继承DefaultCommentGenerator 或者CommentGenerator


package com.zhianchen.mysqlremark.toword.config;

import org.apache.commons.lang3.StringUtils;
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.PropertyRegistry;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/*
*
*@Description MybatisGenerator
*@Author chenzhian
*@Date 2021/11/11 16:36
*/
public class MybatisGenerator implements CommentGenerator {
private Properties properties;
private Properties systemPro;
//时间
private String currentDateStr;

private Boolean suppressDate;
//时间
private Boolean suppressAllComments;

private String author;

public MybatisGenerator(){
properties=new Properties();
systemPro=System.getProperties();
currentDateStr=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

}
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
String strsuppressDate= properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE);
suppressDate="true".equals(strsuppressDate);
String strsuppressAllComments= properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS);
suppressAllComments="true".equals(strsuppressAllComments);
author=properties.getProperty("author");
}

/**
* 字段注释的方法
*/
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
// 添加字段注释
field.addJavaDocLine("/**");
field.addJavaDocLine("* "+introspectedColumn.getActualColumnName());
field.addJavaDocLine("* "+introspectedColumn.getRemarks());
field.addJavaDocLine(" */");
}

public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
// 添加字段注释
field.addJavaDocLine("/**");
field.addJavaDocLine("* "+field.getName());
field.addJavaDocLine(" */");
}

public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if(suppressAllComments){
return;
}
// 添加字段注释
topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine(" * ");
topLevelClass.addJavaDocLine(""+introspectedTable.getFullyQualifiedTable());
topLevelClass.addJavaDocLine(" * @description : " + introspectedTable.getRemarks());
topLevelClass.addJavaDocLine(" * @author : " + author);
topLevelClass.addJavaDocLine(" * @date : " + currentDateStr);
topLevelClass.addJavaDocLine(" * @modify : " );
topLevelClass.addJavaDocLine(" */");
}

public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
if(suppressAllComments){
return;
}
// 添加字段注释
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * ");
innerClass.addJavaDocLine(""+introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(" * @description : " + introspectedTable.getRemarks());
innerClass.addJavaDocLine(" * @author : " + author);
innerClass.addJavaDocLine(" * @date : " + currentDateStr);
innerClass.addJavaDocLine(" * @modify : " );
innerClass.addJavaDocLine(" */");
}

public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {
if(suppressAllComments){
return;
}
// 添加字段注释
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * ");
innerClass.addJavaDocLine(""+introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(" * @description : " + introspectedTable.getRemarks());
innerClass.addJavaDocLine(" * @author : " + author);
innerClass.addJavaDocLine(" * @date : " + currentDateStr);
innerClass.addJavaDocLine(" * @modify : " );
innerClass.addJavaDocLine(" */");
}

public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
if(suppressAllComments){
return;
}
// 添加字段注释
innerEnum.addJavaDocLine("/**");
innerEnum.addJavaDocLine(" * ");
innerEnum.addJavaDocLine(""+introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(" * @description : " + introspectedTable.getRemarks());
innerEnum.addJavaDocLine(" * @author : " + author);
innerEnum.addJavaDocLine(" * @date : " + currentDateStr);
innerEnum.addJavaDocLine(" * @modify : " );
innerEnum.addJavaDocLine(" */");
}

public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
method.addJavaDocLine("/**");
method.addJavaDocLine("* "+introspectedTable.getRemarks());
method.addJavaDocLine("* @return "+method.getName());

method.addJavaDocLine(" */");
}

public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
method.addJavaDocLine("/**");
method.addJavaDocLine("* "+introspectedTable.getRemarks());
Parameter parameter=method.getParameters().get(0);
method.addJavaDocLine("* @param "+parameter.getName());

method.addJavaDocLine(" */");
}

public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
if(suppressAllComments){
return;
}
// 添加字段注释
method.addJavaDocLine("/**");
method.addJavaDocLine(" * ");
//method.addJavaDocLine(""+method.getName());
method.addJavaDocLine(" * @description : " + introspectedTable.getRemarks());
method.addJavaDocLine(" * @author : " + author);
method.addJavaDocLine(" * @date : " + currentDateStr);
method.addJavaDocLine(" * @modify : " );
method.addJavaDocLine(" */");
}

public void addJavaFileComment(CompilationUnit compilationUnit) {
if(suppressAllComments){
return;
}

}

public void addComment(XmlElement xmlElement) {

}

public void addRootComment(XmlElement xmlElement) {

}
}

,就是introspectedColumn.getRemarks()获取不到字段的注释,生成的javabean里面应该显示字段注释的地方显示的是null.

或者 introspectedTable.getRemarks() 获取不到表的注释,记得加上以下

<?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>
<classPathEntry location="J:\program\myprogram\maven\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 生成mysql带有分页的sql的插件 这个可以自己写,-->
<!-- <plugin type="generator.MysqlPaginationPlugin" />-->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!-- <property name="javaFileEncoding" value="UTF-8"/>-->

<!-- 自定义的注释规则,继承 DefaultCommentGenerator 重写 一些方法 -->
<commentGenerator type="com.zhianchen.mysqlremark.toword.config.MybatisGenerator">
<!-- 是否去除自动生成日期的注释 true:是 : false:否 -->
<property name="suppressDate" value="true"/>
<!-- 是否去除所有自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
<property name="author" value="zhianchen"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/information_schema"
userId="aaa"
password="aaaa">
<!-- 针对oracle数据库 -->
<!--<property name="remarksReporting" value="true"></property>-->
<!-- 针对mysql数据库 -->
<property name="useInformationSchema" value="true"></property>
</jdbcConnection>
<!--生成entity类存放位置-->
<javaModelGenerator targetPackage="com.zhianchen.mysqlremark.toword.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapping.master" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zhianchen.mysqlremark.toword.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- <table tableName="TABLES" domainObjectName="Tables" />
<table tableName="COLUMNS" domainObjectName="Columns"/>-->
<table tableName="STATISTICS" domainObjectName="Statistics"/>

</context>
</generatorConfiguration>

来源

mysql generator备注_MyBatis Generator 自定义生成注释的方法

Mybatis Generator 自定义注释(生成带有中文字段名注释的Bean)

mybatis-generator自定义注释生成

最新文章

  1. iOS 安装应用
  2. 俄罗斯方块(Win32实现,Codeblocks+GCC编译)
  3. Java提高篇——对象克隆(复制)
  4. PHP中fopen,file_get_contents,curl函数的区别
  5. oraclede chuangjian yu dajian(zhuan)
  6. 各种html5 的 polyfill
  7. Android下 scrollview的滚动停止事件的监听方法
  8. 【HDU1231】How Many Tables(并查集基础题)
  9. hdoj 3549 Flow Problem(最大网络流)
  10. Ubuntu 13.04/12.10安装Oracle 11gR2图文教程(转)
  11. UML常用关系
  12. 查找git ignore的追踪
  13. 一大波 Android 刘海屏来袭,全网最全适配技巧!
  14. Django目录
  15. 让DIV随滚动条滚动
  16. Django--CRM
  17. MySQL----数据库简单操作
  18. PythonStudy——函数嵌套定义 Function nesting definition
  19. hibernate查询竟然有6种方法
  20. scope_identity() 与 @@identity的区别

热门文章

  1. javascript生成一棵树
  2. 绕过 Docker ,大规模杀死容器
  3. ts中 any、unknown、never 、void的区别
  4. C++进阶-3-5-list容器
  5. ubuntu 获取 gateway 网关 ip
  6. 关于Linux添加字体
  7. grpc-java源码环境编译
  8. Flask框架实现登录注册功能(mysql数据库)
  9. IPC机制与线程的操作
  10. 基于SqlSugar的开发框架循序渐进介绍(8)-- 在基类函数封装实现用户操作日志记录