Mybatis-Generator 自定义注释
继承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自定义注释生成
Mybatis-Generator 自定义注释的更多相关文章
- MyBatis Generator 自定义生成注释
注释生成器 为了生成db里面的注释,必须自定义注释生成器 EmptyCommentGenerator: import org.mybatis.generator.api.CommentGenerato ...
- 记一次 IDEA mybatis.generator 自定义扩展插件
在使用 idea mybatis.generator 生成的代码,遇到 生成的代码很多重复的地方, 虽然代码是生成的,我们也不应该允许重复的代码出现,因为这些代码后期都要来手动维护. 对于生成时间戳注 ...
- SpringBoot(十一):springboot2.0.2下配置mybatis generator环境,并自定义字段/getter/settetr注释
Mybatis Generator是供开发者在mybatis开发时,快速构建mapper xml,mapper类,model类的一个插件工具.它相对来说对开发者是有很大的帮助的,但是它也有不足之处,比 ...
- mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)
我们都知道mybatis generator自动生成的注释没什么实际作用,而且还增加了代码量.如果能将注释从数据库中捞取到,不仅能很大程度上增加代码的可读性,而且减少了后期手动加注释的工作量. 1.首 ...
- 关于 mybatis-generator自定义注释生成 使用DefaultCommentGenerator重写来完成
项目里新建表时model,mapper以及mapper.xml基本都是用Mybatis Generator(以下简称为MBG)自动生成的,但是MBG自动生成的model的注释实在有点非人类,至少中国人 ...
- mybatis-generator自定义注释生成
最近做的项目发现没有中文注释,故查找资料,特此记录. 本文所用的是基于mybatis-generator 1.3.2版本来完成的. mybatis-generator 自动生成的代码注释是很反人类的, ...
- MyBatis Generator 超详细配置
想快速开始,请直接拉到最后,看整体配置. MyBatis Generator 是 MyBatis 提供的一个代码生成工具.可以帮我们生成 表对应的持久化对象(po).操作数据库的接口(dao).CRU ...
- MyBatis Generator 生成数据库自带中文注释
1. maven依赖 <!-- mybatis生成 jar包 --> <dependency> <groupId>org.mybatis.generator< ...
- Mybatis Generator生成数据库自带的中文注释
1.相关jar包 <!-- mybatis生成 jar包 --> <dependency> <groupId>org.mybatis.generator</g ...
随机推荐
- 分享一款高逼格的Linux磁盘信息查看工具
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 可以使用df命令来显示在Linux.macOS和类Unix系统中挂载的文件系统上有多少可用磁盘 ...
- Java中的JVM和Redis,你了解的透彻么?
招聘在前不久已经渐渐拉下帷幕了,看到最近技术群一个问题,引起了我的思考:"今年面试为什么那么难?" 想必大家都知道程序员要涨薪主要靠跳槽来完成!但是无论是考试,还是求职,这个难度, ...
- 按照 Promise/A+ 规范逐行注释并实现 Promise
0. 前言 面试官:「你写个 Promise 吧.」 我:「对不起,打扰了,再见!」 现在前端越来越卷,不会手写 Promise 都不好意思面试了(手动狗头.jpg).虽然没多少人会在业务中用自己实现 ...
- CSS加载不会阻塞DOM树解析
1.css的加载不会阻塞DOM树解析. 1).css加载不会阻塞DOM树的解析: 2).css加载会阻塞DOM树渲染: 先把DOM树结构先解析完,等CSS加载完后根据最终样式渲染DOM树,免了css加 ...
- Redis 为何使用近似 LRU 算法淘汰数据,而不是真实 LRU?
在<Redis 数据缓存满了怎么办?>我们知道 Redis 缓存满了之后能通过淘汰策略删除数据腾出空间给新数据. 淘汰策略如下所示: 设置过期时间的 key volatile-ttl.vo ...
- 图解Dijkstra(迪杰斯特拉)算法+代码实现
简介 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法是很有代表性的 ...
- Android.mk编译App源码
在Andriod源码环境编译APP主要考虑如何引入第三方jar包和arr包的问题,初次尝试,步步是坑,这里给出一个模板: LOCAL_PATH := $(call my-dir) include $( ...
- 文件操作(Java)
学习内容:文件操作 1.输入流:InputStream类是字节输入流的抽象类,常用的一些方法有: raed()方法:从输入流中读取数据的下一个字节 reset()方法:将输入指针返回到当 ...
- Kube-OVN v1.10.0:新增Windows节点支持,用户自定义子网ACL等10+硬核功能
在Kube-OVN社区小伙伴的共同努力下,Kube-OVN v1.10.0于五月份正式发布.Kube-OVN v1.10.0版本中,我们一如既往地对Kube-OVN 的功能.性能.稳定性和易用性进行了 ...
- Linux详解(基础、环境配置、项目部署入门)
Linux(CentOS 7)操作系统 消息队列(Kafka.RabbitMQ.RocketMQ),缓存(Redis),搜索引擎(ES),集群分布式(需要购买多台服务器,如果没有服务器我们就只能使用虚 ...