jeecgboot 3.7.1 一键将生成好的代码复制到前后端项目目录,并执行SQL插入菜单的python脚本.
import shutil import os import mysql.connector from mysql.connector import Error base_dir = "D:/pro/JeecgBoot-v3.7.1" code_dir = "/gendcode" package_name = "meetrice" # MySQL 连接配置 config = { 'user': 'root', 'password': 'youpwd', 'host': 'localhost', 'database': 'jeecg-boot', 'raise_on_warnings': True } #复制目录 def copy_directory(src, dst): # 如果源目录不存在,直接返回 if not os.path.exists(src): print(f"源目录 {src} 不存在,跳过复制") return # 遍历源目录 for item in os.listdir(src): s = os.path.join(src, item) d = os.path.join(dst, item) if os.path.isdir(s): # 如果是目录,递归复制 if not os.path.exists(d): shutil.copytree(s, d, symlinks=False, ignore=shutil.ignore_patterns('*.sql')) else: copy_directory(s, d) # 递归调用以处理子目录 else: # 如果是文件,且不是 .sql 文件,则复制 if not item.lower().endswith('.sql'): shutil.copy2(s, d) # 获取最新的 SQL 文件 def get_latest_sql_file(directory): sql_files = [f for f in os.listdir(directory) if f.endswith('.sql')] if not sql_files: return None return max(sql_files, key=lambda f: os.path.getmtime(os.path.join(directory, f))) #执行sql def execute_sql_file(file_path): try: connection = mysql.connector.connect(**config) if connection.is_connected(): cursor = connection.cursor() # 读取 SQL 文件内容 with open(file_path, 'r',encoding='utf-8') as file: sql_script = file.read() # 执行 SQL 脚本 for result in cursor.execute(sql_script, multi=True): if result.with_rows: print("Rows produced by statement '{}':".format(result.statement)) print(result.fetchall()) else: print("Number of rows affected by statement '{}': {}".format( result.statement, result.rowcount)) connection.commit() print(f"SQL file {file_path} executed successfully") except Error as e: print(f"Error executing SQL file: {e}") finally: if connection.is_connected(): cursor.close() connection.close() print("MySQL connection is closed") # 定义源目录和目标目录的对应关系 directory_pairs = [ (base_dir + code_dir + "/src/main/java/org/jeecg/modules/demo/"+package_name+"/controller", base_dir+"/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/"+package_name+"/controller"), (base_dir + code_dir + "/src/main/java/org/jeecg/modules/demo/"+package_name+"/entity", base_dir+"/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/"+package_name+"/entity"), (base_dir + code_dir + "/src/main/java/org/jeecg/modules/demo/"+package_name+"/mapper", base_dir+"/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/"+package_name+"/mapper"), (base_dir + code_dir + "/src/main/java/org/jeecg/modules/demo/"+package_name+"/service", base_dir+"/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/"+package_name+"/service"), (base_dir + code_dir + "/src/main/java/org/jeecg/modules/demo/"+package_name+"/vo", base_dir+"/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/"+package_name+"/vo"), (base_dir + code_dir + "/src/main/java/org/jeecg/modules/demo/"+package_name+"/vue3", base_dir+"/jeecgboot-vue3/src/views/"+package_name+"/") ] # 遍历目录对并执行复制 for src, dst in directory_pairs: copy_directory(src, dst) print("所有复制操作已完成") # SQL 文件所在的目录 sql_directory = base_dir + code_dir + "/src/main/java/org/jeecg/modules/demo/"+package_name+"/vue3" # 获取最新的 SQL 文件 latest_sql_file = get_latest_sql_file(sql_directory) if latest_sql_file: file_path = os.path.join(sql_directory, latest_sql_file) print(f"Executing latest SQL file: {file_path}") execute_sql_file(file_path) else: print("No SQL files found in the specified directory")