PO DTO 生成器
假设你是一个Java 高级程序员,我会提供一些信息,你需要帮我自动生成Java的PO、DTO 对象。
这些信息有着固定的形式,第一行是对象的类名,其后的每一行都是该对象的属性(简称"属性")。
对于我属性,格式可能是小驼峰的形式,可能是全大写命名法的格式,我需要你自动帮我转换成小驼峰的格式。
其中生成的PO对象中,每个属性需要加上注解@Column,该注解是Mybatis的注解。该注解的值对应的是属性的全大写命名法的格式。
以下我会举个例子帮助你理解我的预期结果。
java
User
id
name
batchName
生成的PO对象如下:
java
public class UserPO{
@Column("ID")
private String id;
@Column("NAME")
private String name;
@Column("BATCH_NAME")
private String batchName;
}
生成的DTO对象如下:
java
public class UserDto{
private String id;
private String name;
private String batchName;
}
测试集
测试集合
Student
id
name
sex
grade
teacherName
Teacher
id
name
subject
studentCount
PO,DTO生成器-Java代码版本
java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JavaCodeGenerator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the class name
String className = scanner.nextLine().trim();
// Read the properties
List<String> properties = new ArrayList<>();
while (scanner.hasNextLine()) {
String property = scanner.nextLine().trim();
if (property.isEmpty()) {
break;
}
properties.add(property);
}
// Generate PO class
System.out.println("public class " + className + "PO {");
for (String property : properties) {
String columnName = toUpperCaseSnakeCase(property);
System.out.println("\t@Column(\"" + columnName + "\")");
System.out.println("\tprivate String " + property + ";");
System.out.println();
}
System.out.println("}");
System.out.println();
// Generate DTO class
System.out.println("public class " + className + "Dto {");
for (String property : properties) {
System.out.println("\tprivate String " + property + ";");
}
System.out.println("}");
}
private static String toUpperCaseSnakeCase(String property) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < property.length(); i++) {
char c = property.charAt(i);
if (Character.isUpperCase(c)) {
sb.append("_").append(c);
} else {
sb.append(Character.toUpperCase(c));
}
} return sb.toString();
}
}
PO DTO 生成器-html、js版本
把上面的java版本,改成js版本,需要一个html页面,该页面需要2个文本框,一个按钮,一个文本框输入用户输入的内容,第二个文本框生成PO、DTO对象
js
<!DOCTYPE html>
<html>
<head>
<title>Java PO/DTO Generator</title>
</head>
<body>
<h1>Java Code Generator</h1> <label for="className">Class Name:</label> <input type="text" id="className"><br><br>
<label for="properties">Properties:</label> <textarea id="properties" rows="5" cols="30"></textarea><br><br> <button
onclick="generateCode()">Generate Code</button><br><br> <label for="poCode">PO Code:</label> <textarea
id="poCode" rows="10" cols="50" readonly></textarea><br><br> <label for="dtoCode">DTO Code:</label> <textarea
id="dtoCode" rows="10" cols="50" readonly></textarea>
</body>
<script>
function generateCode() {
// Read input values
var className = document.getElementById("className").value.trim();
var propertiesInput = document.getElementById("properties").value.trim();
// Split properties by newline
var properties = propertiesInput.split("\n");
// Generate PO class
var poCode = "public class " + className + "PO {\n";
for (var i = 0; i < properties.length; i++) {
var property = properties[i].trim();
var columnName = toUpperCaseSnakeCase(property);
poCode += "\t@Column(\"" + columnName + "\")\n";
poCode += "\tprivate String " + property + ";\n\n";
}
poCode += "}";
// Generate DTO class
var dtoCode = "public class " + className + "DTO {\n";
for (var i = 0; i < properties.length; i++) {
var property = properties[i].trim();
dtoCode += "\tprivate String " + property + ";\n";
}
dtoCode += "}";
// Update output text areas
document.getElementById("poCode").value = poCode;
document.getElementById("dtoCode").value = dtoCode;
}
function toUpperCaseSnakeCase(property) {
var snakeCase = "";
for (var i = 0; i < property.length; i++) {
var char = property.charAt(i);
if (char === char.toUpperCase()) {
snakeCase += "_" + char;
} else {
snakeCase += char.toUpperCase();
}
}
return snakeCase;
}
</script>
</html>