EntityClassGenerator.java:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
public class DTOGenerator {
private static Set<String> excludeSet=new HashSet<>();
static {
excludeSet.add("create_time");
excludeSet.add("create_by");
excludeSet.add("update_time");
excludeSet.add("update_by");
}
public static void generateEntityClass(ResultSet columns,String tabName) throws SQLException, IOException {
tabName=toCamelCase(tabName,true);
tabName=tabName+"DTO";
StringBuilder classContent = new StringBuilder();
classContent.append("import lombok.Data;\n");
classContent.append("import lombok.EqualsAndHashCode;\n");
classContent.append("import javax.validation.constraints.Size;\n");
classContent.append("import com.jxepdi.ry.common.core.validate.AddGroup;\n");
classContent.append("import com.jxepdi.ry.common.core.validate.EditGroup;\n");
classContent.append("\n");
classContent.append("@Data\n");
classContent.append("@EqualsAndHashCode\n");
classContent.append(String.format("public class %s {\n", tabName));
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
if (excludeSet.contains(columnName)){
continue;
}
columnName=toCamelCase(columnName,false);
int columnSize = columns.getInt("COLUMN_SIZE");
String remark = columns.getString("REMARKS");
String columnType = "String"; // Simplification, handle other types as needed
classContent.append("\t/**\n");
classContent.append("\t *"+remark+"\n");
classContent.append("\t */\n");
classContent.append(" @Size(max = ").append(columnSize).append(String.format(",message = \"%s长度超过限制\"", remark)).append(",groups = {AddGroup.class, EditGroup.class}").append(")\n");
classContent.append(" private ").append(columnType).append(" ").append(columnName).append(";\n");
classContent.append("\n");
}
classContent.append("}\n");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(String.format("E:/tmp/gen/%s.java", tabName)))) {
writer.write(classContent.toString());
}
}
//转驼峰
private static String toCamelCase(String s, boolean capitalizeFirstLetter) {
StringBuilder camelCaseString = new StringBuilder();
boolean nextUpperCase = capitalizeFirstLetter;
for (char c : s.toCharArray()) {
if (c == '_') {
nextUpperCase = true;
} else {
if (nextUpperCase) {
camelCaseString.append(Character.toUpperCase(c));
nextUpperCase = false;
} else {
camelCaseString.append(c);
}
}
}
return camelCaseString.toString();
}
}
Main.java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main{
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/fwgs";
String user = "root";
String password = "123456";
try (Connection connection = DriverManager.getConnection(url, user, password)) {
DatabaseMetaData metaData = connection.getMetaData();
String tabName="table_name";
ResultSet columns = metaData.getColumns(null, null, tabName, null);
DTOGenerator.generateEntityClass(columns,tabName);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}
}