From 4eac422e52a4d28fb651b75d0f054697c7a2c0fa Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期一, 09 二月 2026 15:14:13 +0800
Subject: [PATCH] 优化

---
 server/dmmall_service/src/main/java/com/doumee/core/utils/CodeGenerator.java |  213 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 213 insertions(+), 0 deletions(-)

diff --git a/server/dmmall_service/src/main/java/com/doumee/core/utils/CodeGenerator.java b/server/dmmall_service/src/main/java/com/doumee/core/utils/CodeGenerator.java
new file mode 100644
index 0000000..0a83b20
--- /dev/null
+++ b/server/dmmall_service/src/main/java/com/doumee/core/utils/CodeGenerator.java
@@ -0,0 +1,213 @@
+package com.doumee.core.utils;
+
+
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.sql.*;
+import java.util.Date;
+import java.util.*;
+
+// 婕旂ず渚嬪瓙锛屾墽琛� main 鏂规硶鎺у埗鍙拌緭鍏ユā鍧楄〃鍚嶅洖杞﹁嚜鍔ㄧ敓鎴愬搴旈」鐩洰褰曚腑
+public class CodeGenerator {
+    public static  String packName ="com.doumee";
+    // 璁剧疆杈撳嚭鐩綍
+    public static String rootFileDir = "D:/code/idea/initCode/diandongche/";
+    public static  String outputDirEntity =rootFileDir+"src/main/java/com/doumee/dao/business/model/"; // 杈撳嚭鐨勪唬鐮佹枃浠跺す璺緞
+    public static  String outputDirMapper = rootFileDir+"src/main/java/com/doumee/dao/business/"; // 杈撳嚭鐨勪唬鐮佹枃浠跺す璺緞
+    public static  String outputDirService =rootFileDir+"src/main/java/com/doumee/service/business/"; // 杈撳嚭鐨勪唬鐮佹枃浠跺す璺緞
+    public static  String outputDirServiceImpl = rootFileDir+"src/main/java/com/doumee/service/business/impl/"; // 杈撳嚭鐨勪唬鐮佹枃浠跺す璺緞
+    public static  String outputDirController =rootFileDir+"src/main/java/com/doumee/api/business/"; // 杈撳嚭鐨勪唬鐮佹枃浠跺す璺緞
+    public static  String outputDirDb =rootFileDir+"db/"; // 杈撳嚭鐨勪唬鐮佹枃浠跺す璺緞
+    public static void main(String[] args) {
+        // 閰嶇疆鏁版嵁搴撹繛鎺�
+        String url = "jdbc:mysql://192.168.0.211:3306/diandongche";
+        String username = "root";
+        String password = "Doumee@168";
+        String database = "diandongche";
+        List<String> tables = new ArrayList<>();
+        tables.add("refund");
+
+        for(String tableName : tables){
+            // 鑾峰彇琛ㄤ俊鎭苟鐢熸垚浠g爜
+            try {
+                System.out.println(tableName+"===================鐢熸垚寮�濮�===================");
+                Map<String, Object> tableInfo = getTableInfo(url, username, password, database, tableName);
+                generateCode(tableInfo);
+                System.out.println(tableName+"===================鐢熸垚缁撴潫===================");
+            }catch (Exception e){
+                System.err.println(tableName+"===================鐢熸垚寮傚父==================="+e.getMessage());
+            }
+        }
+
+    }
+
+
+    public static Map<String, Object> getTableInfo(String url, String username, String password, String database, String tableName) {
+        Map<String, Object> map = new HashMap<>();
+        try (Connection conn = DriverManager.getConnection(url, username, password)) {
+            // 鏌ヨ琛ㄦ敞閲�
+            String tableQuery = "SELECT table_comment FROM information_schema.tables WHERE table_schema = ? AND table_name = ?";
+            try (PreparedStatement ps = conn.prepareStatement(tableQuery)) {
+                ps.setString(1, database);
+                ps.setString(2, tableName);
+                ResultSet rs = ps.executeQuery();
+                if (rs.next()) {
+                    map.put("tableComment", rs.getString("table_comment"));
+                }
+            }
+            // 鏌ヨ瀛楁淇℃伅 auto_increment
+            String columnQuery = "SELECT extra,column_name, column_type, column_comment FROM information_schema.columns WHERE table_schema = ? AND table_name = ? order by ordinal_position";
+            List<Map<String, String>> columns = new ArrayList<>();
+            try (PreparedStatement ps = conn.prepareStatement(columnQuery)) {
+                ps.setString(1, database);
+                ps.setString(2, tableName);
+                ResultSet rs = ps.executeQuery();
+                int index = 1;
+                while (rs.next()) {
+                    String javaName = toCamelCase(rs.getString("column_name"));
+                    String getJavaName = javaName.substring(0,1).toUpperCase()+javaName.substring(1);
+                    Map<String, String> columnInfo = new HashMap<>();
+                    if(rs.getString("extra") !=null
+                            && rs.getString("extra").contains("auto_increment")){
+                        //濡傛灉鏄嚜澧為暱
+                        columnInfo.put("auto","1");  // 杞崲涓� Java 鍙橀噺鍚�
+                    }else{
+                        columnInfo.put("auto","0");  // 杞崲涓� Java 鍙橀噺鍚�
+                    }
+                    columnInfo.put("index",index++ +"");
+                    columnInfo.put("columnName", rs.getString("column_name"));
+                    columnInfo.put("javaName",javaName);  // 杞崲涓� Java 鍙橀噺鍚�
+                    columnInfo.put("getJavaName",getJavaName);  // 杞崲涓� Java 鍙橀噺鍚�
+                    columnInfo.put("javaType", sqlTypeToJavaType(rs.getString("column_type")));  // 杞崲涓� Java 绫诲瀷
+                    columnInfo.put("comment", rs.getString("column_comment"));
+                    columns.add(columnInfo);
+                }
+            }
+            map.put("columns", columns);
+            map.put("tableName", tableName);
+            map.put("entityName", toCamelCase(tableName));
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+        return map;
+    }
+
+    private static String sqlTypeToJavaType(String sqlType) {
+        sqlType = sqlType.toLowerCase();
+        if (sqlType.contains("int")) {
+            return "Integer";
+        } else if (sqlType.contains("bigint")) {
+            return "Long";
+        } else if (sqlType.contains("char") || sqlType.contains("text") || sqlType.contains("varchar")) {
+            return "String";
+        } else if (sqlType.contains("datetime") || sqlType.contains("timestamp")) {
+            return "Date";
+        } else if (sqlType.contains("date")) {
+            return "Date";
+        } else if (sqlType.contains("decimal") || sqlType.contains("double")) {
+            return "BigDecimal";
+        } else {
+            return "String";
+        }
+    }
+
+    private static String toCamelCase(String name) {
+        StringBuilder result = new StringBuilder();
+        String[] parts = name.split("_");
+        for (int i = 0; i < parts.length; i++) {
+            if (i == 0) {
+                result.append(parts[i].toLowerCase());
+            } else {
+                result.append(Character.toUpperCase(parts[i].charAt(0))).append(parts[i].substring(1).toLowerCase());
+            }
+        }
+        return result.toString();
+    }
+    private static void generateCode(Map<String, Object> tableInfo) {
+        // 璇诲彇琛ㄤ俊鎭�
+        String entityName = (String) tableInfo.get("entityName");
+        String tableName = (String) tableInfo.get("tableName");
+        String tableComment = (String) tableInfo.get("tableComment");
+        List<Map<String, String>> columns = (List<Map<String, String>>) tableInfo.get("columns");
+
+
+        // 璁剧疆 Velocity 閰嶇疆
+        VelocityEngine velocityEngine = new VelocityEngine();
+        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
+        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
+        velocityEngine.init();
+        // 閰嶇疆妯℃澘鐩綍
+        String baseName = entityName.substring(0, 1).toUpperCase() + entityName.substring(1);
+        // 鍒涘缓 Velocity 涓婁笅鏂�
+        Map<String,String> pack = new HashMap<>();
+        pack.put("Base",packName);
+        pack.put("Entity",packName+".dao.business.model");
+        pack.put("Controller",packName+".api.business");
+        pack.put("Mapper",packName+".dao.business");
+        pack.put("Service",packName+".service.business");
+        pack.put("ServiceImpl",packName+".service.business.impl");
+        VelocityContext context = new VelocityContext();
+        context.put("package",pack); // 璁剧疆鍖呰矾寰�
+        context.put("entityName", baseName);
+        context.put("entityNameLower", entityName);
+        context.put("entityNameLowerFull", entityName.toLowerCase());
+        context.put("tableName", tableName);
+        context.put("tableComment", tableComment);
+        context.put("columns", columns);
+        context.put("nowDate", DateUtil.getPlusTime2(new Date()));
+
+        // 鐢熸垚 Entity 鏂囦欢
+        generateFile(velocityEngine, context,  "entity.vm", baseName, outputDirEntity  , ".java");
+
+        // 鐢熸垚 Mapper 鏂囦欢
+        generateFile(velocityEngine, context,  "mapper.vm",  baseName, outputDirMapper , "Mapper.java");
+
+        // 鐢熸垚 Service 鎺ュ彛鏂囦欢
+        generateFile(velocityEngine, context,  "service.vm",  baseName, outputDirService  , "Service.java");
+
+        // 鐢熸垚 ServiceImpl 鏂囦欢
+        generateFile(velocityEngine, context,  "serviceImpl.vm",  baseName, outputDirServiceImpl + "" , "ServiceImpl.java");
+
+        // 鐢熸垚 Controller 鏂囦欢
+        generateFile(velocityEngine, context,  "controller.vm",  baseName, outputDirController  , "Controller.java");
+        generateFile(velocityEngine, context,  "db.vm",  baseName, outputDirDb  , "Permissions.sql");
+    }
+
+    private static void generateFile(VelocityEngine velocityEngine, VelocityContext context,  String templateName, String baseName,String outputFilePath,String endName)  {
+        // 鑾峰彇妯℃澘
+        File file = new File(outputFilePath);
+        if(!file.isDirectory()){
+            file.mkdirs();
+        }
+        System.out.println(file.isFile());
+        Template template = velocityEngine.getTemplate(  "templates/"+templateName);
+
+        // 鍒涘缓鏂囦欢杈撳嚭娴�
+        try{
+
+            System.out.println(outputFilePath+baseName+endName+"===================鐢熸垚寮�濮�===================");
+            File f = new File(outputFilePath+baseName+endName);
+            if(f.isFile()){
+                f.delete();
+            }
+            f.createNewFile();
+            // 灏嗕笂涓嬫枃鏁版嵁娓叉煋鍒版ā鏉夸腑
+            FileWriter writer = new FileWriter(outputFilePath+baseName+endName);
+            template.merge(context, writer);
+            writer.flush();
+            writer.close();
+            System.out.println(outputFilePath+baseName+endName+"===================鐢熸垚缁撴潫===================");
+        } catch (IOException e) {
+            System.err.println(outputFilePath+baseName+endName+"===================鐢熸垚寮傚父===================");
+            e.printStackTrace();
+        }
+    }
+
+}
\ No newline at end of file

--
Gitblit v1.9.3