From 004fd75a464ea6bd94756efb067cbc5409a7d2fb Mon Sep 17 00:00:00 2001 From: zxl <2290782958@qq.com> Date: Sat, 12 Sep 2026 16:31:37 +0800 Subject: [PATCH] first --- .idea/.gitignore | 10 +++++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ api/__init__.py | 0 dao/__init__.py | 0 database.py | 38 +++++++++++++++++++ scheme/__init__.py | 0 8 files changed, 68 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 api/__init__.py create mode 100644 dao/__init__.py create mode 100644 database.py create mode 100644 scheme/__init__.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b6b1ecf --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 已忽略包含查询文件的默认文件夹 +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ddffa47 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dao/__init__.py b/dao/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/database.py b/database.py new file mode 100644 index 0000000..c7bea1f --- /dev/null +++ b/database.py @@ -0,0 +1,38 @@ +# database.py +# 本文件负责配置数据库连接、创建引擎、会话工厂,并提供依赖注入函数 + +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker,declarative_base + +# 1. 配置 MySQL 数据库连接 URL +# 格式:mysql+pymysql://用户名:密码@主机:端口/数据库名?编码 +# 请将下面的 'root', '123456', 'localhost', '3306', 'test_db' 替换为你自己的实际信息 +SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:123456@localhost:3306/test_db" + +# 2. 创建数据库引擎 +# - pool_pre_ping=True 表示每次从连接池取出连接前先 ping 一下,防止使用已断开的连接 +engine = create_engine( + SQLALCHEMY_DATABASE_URL, + pool_pre_ping=True, + echo=True # 设置为 True 会在控制台打印所有 SQL 语句,便于调试,生产环境可关闭 +) + +# 3. 创建会话工厂 +# autocommit=False:不自动提交,需要手动 commit +# autoflush=False:不自动 flush,在查询前会自动 flush,一般保持默认 +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +# 4. 创建声明式基类,所有模型类都继承自它 +Base = declarative_base() + +# 5. 依赖注入函数:用于 FastAPI 路由中获取数据库会话 +def get_db(): + """ + 每次请求创建一个数据库会话,请求结束后关闭。 + 这个函数会作为 Depends 的参数注入到路由中。 + """ + db = SessionLocal() # 创建一个会话实例 + try: + yield db # 将会话交给路由函数使用 + finally: + db.close() # 无论是否发生异常,最后都会关闭会话,释放连接 \ No newline at end of file diff --git a/scheme/__init__.py b/scheme/__init__.py new file mode 100644 index 0000000..e69de29