From 8f39d5456a17e1b35cc6acac9c4f326d5c15f80d Mon Sep 17 00:00:00 2001 From: Orangeeee Date: Tue, 22 Sep 2026 15:14:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Edatabases.py=E6=8A=8A?= =?UTF-8?q?=E6=89=80=E6=9C=89=E5=BB=BA=E8=A1=A8=E5=9F=BA=E7=B1=BB=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=88=90=E4=B8=80=E4=B8=AA=EF=BC=8C=E6=89=8D=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E9=93=BE=E6=8E=A5=E5=A4=96=E9=94=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 +- Teachers/model/tea_model.py | 3 +- .../model/class_management_model.py | 3 +- databases.py | 35 +++++++++++++++++++ main.py | 5 +++ scores/model/score_model.py | 3 +- stu_jiuye/model.py | 7 ++-- students/main.py | 18 ++++++---- students/model/students_model.py | 14 +++++--- 9 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 databases.py diff --git a/.env b/.env index 8158c7f..30b7315 100644 --- a/.env +++ b/.env @@ -1,7 +1,7 @@ # .env(放项目根目录,每个人自己建,不提交 Git) # 用来修改数据库账户密码 DB_USER=root -DB_PASSWORD= +DB_PASSWORD=420821 DB_HOST=localhost DB_PORT=3306 DB_NAME=student_manage_system \ No newline at end of file diff --git a/Teachers/model/tea_model.py b/Teachers/model/tea_model.py index 68a09c4..1e8bdca 100644 --- a/Teachers/model/tea_model.py +++ b/Teachers/model/tea_model.py @@ -1,3 +1,4 @@ +from databases import engine_all,Base_all,Session from Teachers.database import * from datetime import datetime from sqlalchemy import * @@ -7,7 +8,7 @@ class Gender(str,enum.Enum): m = '男' f = '女' -class Tea(Base): +class Tea(Base_all): __tablename__ = 'teachers' id = Column(Integer , primary_key=True diff --git a/class_management/model/class_management_model.py b/class_management/model/class_management_model.py index af7126c..cacb649 100644 --- a/class_management/model/class_management_model.py +++ b/class_management/model/class_management_model.py @@ -1,9 +1,10 @@ +from databases import engine_all,Base_all,Session from sqlalchemy import Column, Integer, String, Date, ForeignKey from sqlalchemy.dialects.mysql import DATETIME from datetime import datetime from class_management.database import Base -class ClassInfo(Base): +class ClassInfo(Base_all): __tablename__ = 'classes' id = Column(Integer diff --git a/databases.py b/databases.py new file mode 100644 index 0000000..3057c3b --- /dev/null +++ b/databases.py @@ -0,0 +1,35 @@ +# databases链接数据库 + +from sqlalchemy import * +from sqlalchemy.orm import sessionmaker,declarative_base + +# 导入.env参数 +import os +from dotenv import load_dotenv +load_dotenv( ) +DB_USER = os.getenv("DB_USER", "root") +DB_PASSWORD = os.getenv("DB_PASSWORD", "") +DB_HOST = os.getenv("DB_HOST", "localhost") +DB_PORT = os.getenv("DB_PORT", "3306") +DB_NAME = os.getenv("DB_NAME", "student_manage_system") + +db_url = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}?charset=utf8mb4" + +engine_all = create_engine(db_url) + +Base_all = declarative_base() + +Session = sessionmaker(bind=engine_all + ,autoflush=False + ,autocommit=False) + +def get_db(): + db = Session() + try: + yield db + finally: + db.close() + + + + diff --git a/main.py b/main.py index ccadca8..7dda036 100644 --- a/main.py +++ b/main.py @@ -20,3 +20,8 @@ def main(): if __name__ == "__main__": import uvicorn uvicorn.run("main:app", host="0.0.0.0", port=59000) + """ + IPv4 地址 . . . . . . . . . . . . : 192.168.6.40 + 子网掩码 . . . . . . . . . . . . : 255.255.255.0 + 默认网关. . . . . . . . . . . . . : 192.168.6.1 + """ \ No newline at end of file diff --git a/scores/model/score_model.py b/scores/model/score_model.py index 0320984..b375943 100644 --- a/scores/model/score_model.py +++ b/scores/model/score_model.py @@ -1,7 +1,8 @@ +from databases import engine_all,Base_all,Session from scores.database import Base from datetime import datetime from sqlalchemy import DateTime,Column,Integer,String,Float -class Score(Base): +class Score(Base_all): __tablename__='scores' id = Column(Integer ,primary_key = True diff --git a/stu_jiuye/model.py b/stu_jiuye/model.py index 7d5c3e5..213a344 100644 --- a/stu_jiuye/model.py +++ b/stu_jiuye/model.py @@ -1,3 +1,4 @@ +from databases import engine_all,Base_all,Session from sqlalchemy import ForeignKey from stu_jiuye.database import DATETIME,Base,Column,Integer,String,DATE @@ -30,7 +31,7 @@ from datetime import datetime # ) -class Employments(Base): #学生就业信息基类 +class Employments(Base_all): #学生就业信息基类 __tablename__ = 'Employments' #表名 id = Column(Integer #主键,自增主键 , primary_key=True @@ -68,7 +69,7 @@ class Employments(Base): #学生就 ) is_deleted = Column(Integer, default=0) #是否为删除信息,默认为0 -class Company(Base): #公司信息基类 +class Company(Base_all): #公司信息基类 __tablename__ = 'Company' #表名 id = Column(Integer , primary_key=True @@ -90,7 +91,7 @@ class Company(Base): #公司信 ) is_deleted = Column(Integer, default=0) -class Address(Base): +class Address(Base_all): __tablename__ = 'Address' id = Column(Integer #地址id,自增主键 , primary_key=True diff --git a/students/main.py b/students/main.py index b18abd2..8dddae6 100644 --- a/students/main.py +++ b/students/main.py @@ -5,17 +5,23 @@ from students.model import students_model from contextlib import asynccontextmanager # 合并接口 -Students_API = APIRouter() -Students_API.include_router(s_api) - -# 自测接口 @asynccontextmanager -async def lifespan(app: FastAPI): +async def S_Router(Students_API: APIRouter): Base.metadata.create_all(engine1) yield engine1.dispose() -app = FastAPI(title="学生管理系统",lifespan=lifespan) # 这里必须把上面写的加上 +Students_API = APIRouter(lifespan=S_Router) +Students_API.include_router(s_api) + +# 自测接口 +@asynccontextmanager +async def S_Fastapi(app: FastAPI): + Base.metadata.create_all(engine1) + yield + engine1.dispose() + +app = FastAPI(title="学生管理系统",lifespan=S_Fastapi) # 这里必须把上面写的加上 app.include_router(s_api,tags=["students"]) diff --git a/students/model/students_model.py b/students/model/students_model.py index bf25289..f5c5e52 100644 --- a/students/model/students_model.py +++ b/students/model/students_model.py @@ -1,13 +1,15 @@ # students_model处理数据库模型 import datetime -from students.databases import * +from databases import engine_all,Base_all,Session +from sqlalchemy import * +# from students.databases import * from datetime import datetime from sqlalchemy.dialects.mysql import DATETIME from students.schema.students_request import SexEnum , EducationEnum -from sqlalchemy import Enum +from sqlalchemy import Enum,ForeignKey,Integer,String -class Students(Base): +class Students(Base_all): __tablename__ = 'students' id = Column(Integer , primary_key=True @@ -53,10 +55,12 @@ class Students(Base): # 外键字段 class_id = Column(Integer - , comment='学生班级' - , nullable=False ) + , ForeignKey("classes.id") + , comment='学生班级' + , nullable=False ) advisor_id = Column(Integer + , ForeignKey("teachers.id") , comment='顾问ID') # 额外字段