27 lines
773 B
Python
27 lines
773 B
Python
from sqlalchemy import create_engine
|
|
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")
|
|
|
|
class_management_url = "mysql+pymysql://root:123456@localhost:3306/student_manage_system?charset=utf8mb4"
|
|
|
|
engine = create_engine(class_management_url)
|
|
SessionLocal = sessionmaker(bind=engine,autocommit=False, autoflush=False)
|
|
Base = declarative_base()
|
|
|
|
# 获取数据库会话依赖
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try :
|
|
yield db
|
|
finally:
|
|
db.close()
|