|
|
|
|
@ -11,64 +11,78 @@ import org.mybatis.generator.internal.util.StringUtility;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 自定义注释生成器
|
|
|
|
|
* 自定义注释生成器,用于生成Java代码中的注释。
|
|
|
|
|
* Created by macro on 2018/4/26.
|
|
|
|
|
*/
|
|
|
|
|
public class CommentGenerator extends DefaultCommentGenerator {
|
|
|
|
|
// 是否添加备注信息的标志,默认为false
|
|
|
|
|
private boolean addRemarkComments = false;
|
|
|
|
|
private static final String EXAMPLE_SUFFIX="Example";
|
|
|
|
|
private static final String MAPPER_SUFFIX="Mapper";
|
|
|
|
|
private static final String API_MODEL_PROPERTY_FULL_CLASS_NAME="io.swagger.annotations.ApiModelProperty";
|
|
|
|
|
// Example类的后缀
|
|
|
|
|
private static final String EXAMPLE_SUFFIX = "Example";
|
|
|
|
|
// Mapper类的后缀
|
|
|
|
|
private static final String MAPPER_SUFFIX = "Mapper";
|
|
|
|
|
// Swagger注解的完整类名
|
|
|
|
|
private static final String API_MODEL_PROPERTY_FULL_CLASS_NAME = "io.swagger.annotations.ApiModelProperty";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置用户配置的参数
|
|
|
|
|
* 设置用户配置的参数,从配置文件中读取是否添加备注信息。
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addConfigurationProperties(Properties properties) {
|
|
|
|
|
// 调用父类方法
|
|
|
|
|
super.addConfigurationProperties(properties);
|
|
|
|
|
// 从配置文件中获取addRemarkComments的值,并转换为布尔值
|
|
|
|
|
this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 给字段添加注释
|
|
|
|
|
* 给字段添加注释,包括Swagger的注解。
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
|
|
|
|
|
IntrospectedColumn introspectedColumn) {
|
|
|
|
|
// 获取数据库字段的备注信息
|
|
|
|
|
String remarks = introspectedColumn.getRemarks();
|
|
|
|
|
//根据参数和备注信息判断是否添加swagger注解信息
|
|
|
|
|
if(addRemarkComments&&StringUtility.stringHasValue(remarks)){
|
|
|
|
|
// addFieldJavaDoc(field, remarks);
|
|
|
|
|
//数据库中特殊字符需要转义
|
|
|
|
|
if(remarks.contains("\"")){
|
|
|
|
|
remarks = remarks.replace("\"","'");
|
|
|
|
|
// 根据参数和备注信息判断是否添加Swagger注解信息
|
|
|
|
|
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
|
|
|
|
|
// 数据库中特殊字符需要转义
|
|
|
|
|
if (remarks.contains("\"")) {
|
|
|
|
|
remarks = remarks.replace("\"", "'");
|
|
|
|
|
}
|
|
|
|
|
//给model的字段添加swagger注解
|
|
|
|
|
field.addJavaDocLine("@ApiModelProperty(value = \""+remarks+"\")");
|
|
|
|
|
// 给model的字段添加Swagger注解
|
|
|
|
|
field.addJavaDocLine("@ApiModelProperty(value = \"" + remarks + "\")");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 给model的字段添加注释
|
|
|
|
|
* 给model的字段添加注释,包括文档注释。
|
|
|
|
|
*/
|
|
|
|
|
private void addFieldJavaDoc(Field field, String remarks) {
|
|
|
|
|
//文档注释开始
|
|
|
|
|
// 文档注释开始
|
|
|
|
|
field.addJavaDocLine("/**");
|
|
|
|
|
//获取数据库字段的备注信息
|
|
|
|
|
// 获取数据库字段的备注信息
|
|
|
|
|
String[] remarkLines = remarks.split(System.getProperty("line.separator"));
|
|
|
|
|
for(String remarkLine:remarkLines){
|
|
|
|
|
field.addJavaDocLine(" * "+remarkLine);
|
|
|
|
|
for (String remarkLine : remarkLines) {
|
|
|
|
|
field.addJavaDocLine(" * " + remarkLine);
|
|
|
|
|
}
|
|
|
|
|
// 添加注释标签
|
|
|
|
|
addJavadocTag(field, false);
|
|
|
|
|
// 文档注释结束
|
|
|
|
|
field.addJavaDocLine(" */");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 给Java文件添加注释,包括导入Swagger注解类。
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addJavaFileComment(CompilationUnit compilationUnit) {
|
|
|
|
|
// 调用父类方法
|
|
|
|
|
super.addJavaFileComment(compilationUnit);
|
|
|
|
|
//只在model中添加swagger注解类的导入
|
|
|
|
|
if(!compilationUnit.getType().getFullyQualifiedName().contains(MAPPER_SUFFIX)&&!compilationUnit.getType().getFullyQualifiedName().contains(EXAMPLE_SUFFIX)){
|
|
|
|
|
// 只在model中添加Swagger注解类的导入
|
|
|
|
|
if (!compilationUnit.getType().getFullyQualifiedName().contains(MAPPER_SUFFIX) &&
|
|
|
|
|
!compilationUnit.getType().getFullyQualifiedName().contains(EXAMPLE_SUFFIX)) {
|
|
|
|
|
// 导入Swagger注解类
|
|
|
|
|
compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_PROPERTY_FULL_CLASS_NAME));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|