首先,试了好几次,重写 DefaultCommentGenerator ,创建MyCommentGenerator类实现CommentGenerator接口,都是按照网上的方案来的,执行也没问题,但是就是没有实体生成,然后又看到说可以直接改jar包的方法,那么我就下手改了,
首先说一下我引的包,引入了一个plugin插件,且内部引入了generator 核心包和 mysql驱动包
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
</plugin>
然后看一下我的 generatorConfig.xml文件
<?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="MyBatis3">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8" />
<!-- 格式化java代码 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
<!-- JavaBean 实现 序列化 接口 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- genenat entity时,生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- 自定义物理分页 可生成支持Mysql数据的limit 不支持Oracle -->
<!--<plugin type="org.mybatis.generator.plugins.page.PaginationPlugin" />-->
<!-- 自定义查询指定字段 -->
<!--<plugin type="org.mybatis.generator.plugins.field.FieldsPlugin" />-->
<!-- 开启支持内存分页 可生成 支持内存分布的方法及参数
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
-->
<!-- generate entity时,生成hashcode和equals方法
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
-->
<!-- 此处是将Example改名为Criteria 当然 想改成什么都行~ -->
<!--<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">-->
<!--<property name="searchString" value="Example$" />-->
<!--<!– 替换后 -->
<!--<property name="replaceString" value="Criteria" /> -->
<!--–>-->
<!--<property name="replaceString" value="Query" />-->
<!--</plugin>-->
<!-- 此处是将UserMapper.xml改名为UserDao.xml 当然 想改成什么都行~ -->
<!--<plugin type="org.mybatis.generator.plugins.rename.RenameSqlMapperPlugin">-->
<!--<property name="searchString" value="Mapper" />-->
<!--<property name="replaceString" value="Dao" />-->
<!--</plugin>-->
<!--<!– 此处是将UserMapper改名为UserDao 接口 当然 想改成什么都行~ –>-->
<!--<plugin type="org.mybatis.generator.plugins.rename.RenameJavaMapperPlugin">-->
<!--<property name="searchString" value="Mapper$" />-->
<!--<property name="replaceString" value="Dao" />-->
<!--</plugin>-->
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<!--生成的注释包含时间戳-->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3307/workflow?serverTimezone=UTC"
userId="root" password="111111">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.neodsc.platform.workflow.entity" targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<!--<sqlMapGenerator targetPackage="com.allen.common.mapper" targetProject="src/main/java">-->
<!--<!– enableSubPackages:是否让schema作为包的后缀 –>-->
<!--<property name="enableSubPackages" value="true"/>-->
<!--</sqlMapGenerator>-->
<!--ANNOTATEDMAPPER: 生成java类文件,基于注解的Mapper接口,不会有对应的XML映射文件
MIXEDMAPPER:XML和注解的混合形式
XMLMAPPER:所有的方法都在XML中,接口调用依赖XML文件 -->
<!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.neodsc.platform.workflow.mapper" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="branch" domainObjectName="Branch" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
下面的是否去除注释我改为了否,如果不需要中文注释的话,一般是要设置为true关掉的,要不然会有原生的英文样式的注解在每个字段上面,感兴趣的同学自己去试试了,操作比较简单,如果是idea 点击maven ,然后当前项目的插件,点击图片中按钮就可以自动生成了
,
因为我这里只想把成功的记录下来
<property name="suppressAllComments" value="false"/> <!-- 是否去除自动生成的注释 true:是 : false:否 -->
下面就是改源码了,找到jar,如图,我是在下面的位置,有个办法,把pom打开,按着ctrl,鼠标移上<artifactId>mybatis-generator-core</artifactId>,就会看到本地仓库jar包地址,找到然后解压
解压 ,在 org.mybatis.generator.internal,文件夹下,会看到 DefaultCommentGenerator.class,我们现在在本地工程里面建一下这个文件,把需要的位置修改一下,等一下给他替换一下,
如图所示,包名和类名必须按照这个写,否则会报错
下面写一下DefaultCommentGenerator 的代码,
大家注意,我改的是下面的方法
@Override public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); // field.addJavaDocLine("/**"); //如果使用,会/** 分支对应的工作流节点id*/是这样格式的注解,大家自己修改成需要的样式 sb.append(introspectedColumn.getRemarks()); field.addJavaDocLine(sb.toString()); // field.addJavaDocLine(" */"); }
package org.mybatis.generator.internal;
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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
/**
* mybatis generator 注释
*
* @author Klaus Allen
* @date 2020/2/20
*/
public class DefaultCommentGenerator implements CommentGenerator {
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String currentDateStr;
public DefaultCommentGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
// add no file level comments by default
return;
}
/**
* Adds a suitable comment to warn users that the element was generated, and
* when it was generated.
*/
@Override
public void addComment(XmlElement xmlElement) {
return;
}
@Override
public void addRootComment(XmlElement rootElement) {
// add no document level comments by default
return;
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> set) {
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
}
@Override
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = 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;
}
@Override
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(" */");
}
@Override
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(" */");
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**"); ///** 分支对应的工作流节点id*/是这样格式的注解,大家自己修改成需要的样式
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString());
field.addJavaDocLine(" */");
}
@Override
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(" */");
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
}
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
// method.addJavaDocLine("/**");
// addJavadocTag(method, false);
// method.addJavaDocLine(" */");
}
@Override
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(" */");
}
@Override
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(" */");
}
@Override
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(" */");
}
}
然后需要把当前的DefaultCommentGenerator.java文件编译成class文件,替换jar包中的文件,编译方法很简单,idea的话,还是maven,install,如图
然后在target文件夹中,找到DefaultCommentGenerator.class
替换刚刚的
然后执行generator,就得到了想要的字段名注释
暖花_ 发布了30 篇原创文章 · 获赞 1 · 访问量 1万+ 私信 关注