mybatis-plus代码生成器去除注释中的日期
时间: 2023-05-30 19:03:33 浏览: 68
可以在代码生成器的配置文件中设置一个自定义的注释模板,在模板中去除日期部分。具体操作如下:
1. 找到代码生成器的配置文件,一般为 `mybatis-plus-generator\src\main\resources\generator\generatorConfig.xml`。
2. 在配置文件中找到 `commentGenerator` 配置项,修改为以下内容:
```xml
<commentGenerator type="com.baomidou.mybatisplus.generator.config.CustomCommentGenerator">
<!-- 去除日期的注释模板 -->
<property name="author" value="Your Name"/>
<property name="dateFormat" value=""/>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="false"/>
</commentGenerator>
```
3. 新建一个 `CustomCommentGenerator` 类,继承 `DefaultCommentGenerator` 类,并重写 `addGeneralMethodComment()` 和 `addGeneralMethodAnnotation()` 方法。代码如下:
```java
public class CustomCommentGenerator extends DefaultCommentGenerator {
/**
* 去除日期的方法注释模板
*/
private static final String METHOD_COMMENT_TEMPLATE_WITHOUT_DATE = " * %s";
/**
* 去除日期的方法注解模板
*/
private static final String METHOD_ANNOTATION_TEMPLATE_WITHOUT_DATE = "@%s";
/**
* 去除日期的方法注释
*/
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
sb.append(String.format(METHOD_COMMENT_TEMPLATE_WITHOUT_DATE, method.getName()));
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */");
}
/**
* 去除日期的方法注解
*/
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> set) {
String annotationName = null;
for (FullyQualifiedJavaType type : set) {
annotationName = type.getShortName();
break;
}
if (annotationName != null) {
StringBuilder sb = new StringBuilder();
sb.append(String.format(METHOD_ANNOTATION_TEMPLATE_WITHOUT_DATE, annotationName));
method.addAnnotation(sb.toString());
}
}
}
```
4. 在配置文件中加入 `CustomCommentGenerator` 类的配置,修改 `commentGenerator` 为以下内容:
```xml
<commentGenerator type="com.baomidou.mybatisplus.generator.config.CustomCommentGenerator">
<!-- 去除日期的注释模板 -->
<property name="author" value="Your Name"/>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="false"/>
</commentGenerator>
```
5. 重新运行代码生成器,生成的代码中注释中的日期就被去除了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)