一款Spring Boot + Vue代码生成器示例,可以生成实体类(Entity)、数据访问层(Repository)、业务逻辑层(Service)、控制器(Controller)以及前端的基础增删改查页面。
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class CodeGenerator {
private static final String BACKEND_PACKAGE = "com.example.backend";
private static final String FRONTEND_PACKAGE = "src/main/resources/frontend";
public static void main(String[] args) {
generateBackendCode();
generateFrontendCode();
}
private static void generateBackendCode() {
// 生成Entity类
String entityCode = generateEntityCode();
saveToFile(entityCode, BACKEND_PACKAGE, "Entity.java");
// 生成Repository接口
String repositoryCode = generateRepositoryCode();
saveToFile(repositoryCode, BACKEND_PACKAGE, "Repository.java");
// 生成Service接口
String serviceCode = generateServiceCode();
saveToFile(serviceCode, BACKEND_PACKAGE, "Service.java");
// 生成ServiceImpl实现类
String serviceImplCode = generateServiceImplCode();
saveToFile(serviceImplCode, BACKEND_PACKAGE, "ServiceImpl.java");
// 生成Controller类
String controllerCode = generateControllerCode();
saveToFile(controllerCode, BACKEND_PACKAGE, "Controller.java");
// 生成Mapper.xml文件
String mapperXmlCode = generateMapperXmlCode();
saveToFile(mapperXmlCode, BACKEND_PACKAGE, "Mapper.xml");
}
private static void generateFrontendCode() {
// 生成基础增删改查页面
String basicCrudPageCode = generateBasicCrudPageCode();
saveToFile(basicCrudPageCode, FRONTEND_PACKAGE, "BasicCrudPage.vue");
}
private static String generateEntityCode() {
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.append("package ").append(BACKEND_PACKAGE).append(";\n\n");
codeBuilder.append("import javax.persistence.Entity;\n");
codeBuilder.append("import javax.persistence.GeneratedValue;\n");
codeBuilder.append("import javax.persistence.GenerationType;\n");
codeBuilder.append("import javax.persistence.Id;\n\n");
codeBuilder.append("@Entity\n");
codeBuilder.append("public class Entity {\n");
codeBuilder.append("\t@Id\n");
codeBuilder.append("\t@GeneratedValue(strategy = GenerationType.IDENTITY)\n");
codeBuilder.append("\tprivate Long id;\n\n");
// 添加更多字段...
codeBuilder.append("}\n");
return codeBuilder.toString();
}
private static String generateRepositoryCode() {
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.append("package ").append(BACKEND_PACKAGE).append(";\n\n");
codeBuilder.append("import org.springframework.data.jpa.repository.JpaRepository;\n\n");
codeBuilder.append("public interface Repository extends JpaRepository<Entity, Long> {\n");
codeBuilder.append("\t// 自定义查询方法...\n");
codeBuilder.append("}\n");
return codeBuilder.toString();
}
private static String generateServiceCode() {
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.append("package ").append(BACKEND_PACKAGE).append(";\n\n");
codeBuilder.append("import java.util.List;\n\n");
codeBuilder.append("public interface Service {\n");
codeBuilder.append("\tList<Entity> getAllEntities();\n");
codeBuilder.append("\tEntity getEntityById(Long id);\n");
codeBuilder.append("\tvoid createEntity(Entity entity);\n");
codeBuilder.append("\tvoid updateEntity(Long id, Entity entity);\n");
codeBuilder.append("\tvoid deleteEntity(Long id);\n");
codeBuilder.append("}\n");
return codeBuilder.toString();
}
private static String generateServiceImplCode() {
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.append("package ").append(BACKEND_PACKAGE).append(";\n\n");
codeBuilder.append("import org.springframework.beans.factory.annotation.Autowired;\n");
codeBuilder.append("import org.springframework.stereotype.Service;\n\n");
codeBuilder.append("import java.util.List;\n\n");
codeBuilder.append("@Service\n");
codeBuilder.append("public class ServiceImpl implements Service {\n");
codeBuilder.append("\t@Autowired\n");
codeBuilder.append("\tprivate Repository repository;\n\n");
codeBuilder.append("\t@Override\n");
codeBuilder.append("\tpublic List<Entity> getAllEntities() {\n");
codeBuilder.append("\t\treturn repository.findAll();\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@Override\n");
codeBuilder.append("\tpublic Entity getEntityById(Long id) {\n");
codeBuilder.append("\t\treturn repository.findById(id).orElse(null);\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@Override\n");
codeBuilder.append("\tpublic void createEntity(Entity entity) {\n");
codeBuilder.append("\t\trepository.save(entity);\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@Override\n");
codeBuilder.append("\tpublic void updateEntity(Long id, Entity entity) {\n");
codeBuilder.append("\t\tEntity existingEntity = repository.findById(id).orElse(null);\n");
codeBuilder.append("\t\tif (existingEntity != null) {\n");
codeBuilder.append("\t\t\t// 更新字段...\n");
codeBuilder.append("\t\t\trepository.save(existingEntity);\n");
codeBuilder.append("\t\t}\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@Override\n");
codeBuilder.append("\tpublic void deleteEntity(Long id) {\n");
codeBuilder.append("\t\trepository.deleteById(id);\n");
codeBuilder.append("\t}\n");
codeBuilder.append("}\n");
return codeBuilder.toString();
}
private static String generateControllerCode() {
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.append("package ").append(BACKEND_PACKAGE).append(";\n\n");
codeBuilder.append("import org.springframework.beans.factory.annotation.Autowired;\n");
codeBuilder.append("import org.springframework.http.HttpStatus;\n");
codeBuilder.append("import org.springframework.http.ResponseEntity;\n");
codeBuilder.append("import org.springframework.web.bind.annotation.*;\n\n");
codeBuilder.append("@RestController\n");
codeBuilder.append("@RequestMapping(\"/api/entities\")\n");
codeBuilder.append("public class Controller {\n");
codeBuilder.append("\t@Autowired\n");
codeBuilder.append("\tprivate Service service;\n\n");
codeBuilder.append("\t@GetMapping\n");
codeBuilder.append("\tpublic ResponseEntity<?> getAllEntities() {\n");
codeBuilder.append("\t\treturn ResponseEntity.ok(service.getAllEntities());\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@GetMapping(\"/{id}\")\n");
codeBuilder.append("\tpublic ResponseEntity<?> getEntityById(@PathVariable Long id) {\n");
codeBuilder.append("\t\tEntity entity = service.getEntityById(id);\n");
codeBuilder.append("\t\tif (entity != null) {\n");
codeBuilder.append("\t\t\treturn ResponseEntity.ok(entity);\n");
codeBuilder.append("\t\t} else {\n");
codeBuilder.append("\t\t\treturn ResponseEntity.notFound().build();\n");
codeBuilder.append("\t\t}\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@PostMapping\n");
codeBuilder.append("\tpublic ResponseEntity<?> createEntity(@RequestBody Entity entity) {\n");
codeBuilder.append("\t\tservice.createEntity(entity);\n");
codeBuilder.append("\t\treturn ResponseEntity.status(HttpStatus.CREATED).build();\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@PutMapping(\"/{id}\")\n");
codeBuilder.append("\tpublic ResponseEntity<?> updateEntity(@PathVariable Long id, @RequestBody Entity entity) {\n");
codeBuilder.append("\t\tservice.updateEntity(id, entity);\n");
codeBuilder.append("\t\treturn ResponseEntity.ok().build();\n");
codeBuilder.append("\t}\n\n");
codeBuilder.append("\t@DeleteMapping(\"/{id}\")\n");
codeBuilder.append("\tpublic ResponseEntity<?> deleteEntity(@PathVariable Long id) {\n");
codeBuilder.append("\t\tservice.deleteEntity(id);\n");
codeBuilder.append("\t\treturn ResponseEntity.ok().build();\n");
codeBuilder.append("\t}\n");
codeBuilder.append("}\n");
return codeBuilder.toString();
}
private static String generateMapperXmlCode() {
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
codeBuilder.append("<mapper namespace=\"").append(BACKEND_PACKAGE).append(".Repository\">\n");
// 添加SQL语句...
codeBuilder.append("</mapper>");
return codeBuilder.toString();
}
private static String generateBasicCrudPageCode() {
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.append("<template>\n");
codeBuilder.append("\t<div>\n");
// 添加页面内容...
codeBuilder.append("\t</div>\n");
codeBuilder.append("</template>\n\n");
codeBuilder.append("<script>\n");
codeBuilder.append("export default {\n");
codeBuilder.append("\t// 页面逻辑...\n");
codeBuilder.append("}\n");
codeBuilder.append("</script>");
return codeBuilder.toString();
}
private static void saveToFile(String code, String packagePath, String fileName) {
try {
String directoryPath = packagePath.replace(".", "/");
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
FileWriter writer = new FileWriter(directoryPath + "/" + fileName);
writer.write(code);
writer.close();
System.out.println("Generated " + packagePath + "." + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}