MyBatisPlus Generator (MPG) 主要用于自动生成MyBatis的Mapper、Entity、Service以及Mapper XML文件等,不能自动生成DTO,虽然可以通过模板写,但是比较麻烦。
所以自己写了一个entity转dto的工具类,只要通过已经生成的entity就可以直接生成DTO文件了。
使用:
在main方法中输入源entity的路径、类的注解、输出路径(如果为空,则使用源entity路径)。
有用请点赞收藏 !
package com.utils;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Optional;
/**
* @description 转换实体类为DTO类
* @author :jixing2000
*/
public class TransToDTOUtil {
/*
需要导入pom.xml
<!-- JavaParser -->
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-symbol-solver-core</artifactId>
<version>3.23.1</version> <!-- 请检查最新版本并替换 -->
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.23.1</version> <!-- 请检查最新版本并替换 -->
</dependency>
*/
public static void main(String[] args) {
new TransToDTOUtil().doTrans("src/main/java/com/basis/entity/SysLogEntity.java","日志表","src/main/java/com/dto/");
}
/**
* 转换实体类为DTO类
* @param entityFilePath Entity类的文件路径
* @param title 类注释
* @param outPath DTO类的输出路径,非必填
*/
private void doTrans(String entityFilePath,String title,String outPath){
try {
File entityFile = new File(entityFilePath);
// 解析Entity类文件
CompilationUnit cu = StaticJavaParser.parse(entityFile);
// DTO类的包名与Entity相同
String packageName = "";
// 输出DTO类的文件路径,与Entity同目录
String dtoFilePath = "";
String realPathArr[] = entityFilePath.split("/");
String outPathArr[] = outPath.split("/");
for(int i =3;i<outPathArr.length;i++){
packageName += "."+outPathArr[i];
}
packageName = packageName.substring(1);
String entityNameAndSuffix = realPathArr[realPathArr.length-1];
entityNameAndSuffix = entityNameAndSuffix.replace("Entity.java", "DTO.java");
String className = entityNameAndSuffix.substring(0, entityNameAndSuffix.length()-5);
if(StringUtils.isNotEmpty(outPath)){
dtoFilePath = outPath+className+".java";
}
else{
dtoFilePath = entityFilePath.replace("Entity.java", "DTO.java");
}
File dtoFile = new File(dtoFilePath);
try (FileWriter writer = new FileWriter(dtoFile)) {
writer.write("package " + packageName + ";\n\n");
writer.write("import io.swagger.annotations.ApiModel;\n");
writer.write("import io.swagger.annotations.ApiModelProperty;\n");
writer.write("import lombok.Data;\n");
writer.write("import java.util.Date;\n");
title = title+" "+className;
writer.write("@ApiModel(value = \""+title+"\")\n");
writer.write("@Data\n");
writer.write("public class "+className+" {\n");
// 遍历字段,提取注释并生成DTO属性
new VoidVisitorAdapter<Void>() {
@Override
public void visit(FieldDeclaration fd, Void arg) {
super.visit(fd, arg);
Optional<JavadocComment> javadoc = fd.getJavadocComment();
javadoc.ifPresent(comment -> {
String commentContent = comment.getContent().toString().trim();
commentContent = commentContent.replace("*","");
commentContent = commentContent.replace("\n","");
commentContent = commentContent.replace("\r","");
commentContent = commentContent.replace("\r\n","");
commentContent = commentContent.replace(" ","");
String propName = fd.getVariable(0).getNameAsString();
try {
writer.write("\t@ApiModelProperty(value = \"" + commentContent + "\")\n");
writer.write("\tprivate " + fd.getVariable(0).getTypeAsString() + " " + propName + ";\n\n");
} catch (IOException e) {
e.printStackTrace();
}
});
}
}.visit(cu, null);
writer.write("}\n");
System.out.println("DTO class generated successfully at: " + dtoFilePath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}