《48小时极速开发:Python+MySQL 学生信息管理系统架构实战揭秘》

Python项目实践:学生信息管理系统

1. 项目概述

1.1 项目背景

开发周期:2天(需求分析0.5天 + 开发1天 + 测试0.5天)

技术栈:Python 3.9 + MySQL + 面向对象编程

核心价值

  • 实现学生信息的全生命周期管理
  • 采用分层架构设计(表示层/业务层/数据层)
  • 数据库驱动的高效数据持久化方案

1.2 系统架构

graph TD subgraph 系统架构 A[命令行界面] -->|调用| B[业务逻辑层] B -->|CRUD操作| C[数据访问层] C -->|连接池| D[(MySQL数据库)] end style A fill:#ff6666,stroke:#000 style B fill:#6699ff,stroke:#000 style C fill:#66ff66,stroke:#000 style D fill:#ff9966,stroke:#000

2. 核心模块实现

2.1 数据模型层(student.py

2.1.1 设计要点:
  • 符合POJO规范的实体类
  • 完善的类型注解
  • 可序列化的数据结构
python 复制代码
class Student:
    """
    学生领域模型(数据实体)
    
    职责:
    1. 封装学生核心属性
    2. 提供数据格式转换方法
    
    设计规范:
    - 符合POJO规范
    - 类型注解完整
    - 支持序列化操作
    
    Attributes:
        name (str): 学生姓名
        age (int): 学生年龄
        mobile (str): 联系电话,格式校验待实现
    """
    def __init__(self, name: str, age: int, mobile: str):
        self.name = name
        self.age = age
        self.mobile = mobile
        
    def __str__(self) -> str:
        return f'{self.name}, {self.age}, {self.mobile}'
        
    def to_dict(self) -> dict:
        """转换为字典格式用于数据库操作"""
        return {
            'name': self.name,
            'age': self.age,
            'mobile': self.mobile
        }

2.2 业务逻辑层(studentManager.py

2.2.1 架构设计:
  • 采用单例模式确保数据一致性
  • 数据库连接池管理
  • 事务安全的CRUD操作
2.2.2 核心接口:
python 复制代码
class StudentManager:
    """
    学生管理系统核心类
    采用单例模式确保全局唯一实例
    """
    _instance = None
    
    def __new__(cls):
        if not cls._instance:
            cls._instance = super().__new__(cls)
            cls._instance.__init__()
        return cls._instance
    
    def __init__(self):
        """初始化数据库连接池"""
        self.conn_pool = mysql.connector.pooling.MySQLConnectionPool(
            pool_name="student_pool",
            pool_size=5,
            host='localhost',
            user='root',
            password='password',
            database='student_db'
        )

### 2.3 数据持久化层(database.py)

#### 设计要点:
- 使用连接池管理数据库连接
- 事务安全的SQL操作
- 完善的错误处理机制

​```python
class StudentManager(object):
    # 定义一个__init__魔术方法,用于初始化数据
    def __init__(self):
        """
        初始化学生管理系统
        
        组件:
        - 内存缓存:student_list
        - 数据库连接池
        - 事务管理器
        """
        self.student_list = []  # 内存数据缓存
        self._init_db_connection()

    # 定义load_student()方法
    def load_student(self):
        try:
            with open('student.data', 'r', encoding='utf-8') as f:
                for line in f:
                    name, age, mobile = line.strip().split(',')
                    student = Student(name, int(age), mobile)
                    self.student_list.append(student)
        except FileNotFoundError:
            print('未找到学员数据文件,将创建新文件')
        except Exception as e:
            print(f'加载学员信息失败:{e}')

    # 定义静态show_help()方法
    @staticmethod
    def show_help():
        print('-' * 40)
        print('Cyber4K 信息管理系统V1.0')
        print('1.添加学员信息')
        print('2.删除学员信息')
        print('3.修改学员信息')
        print('4.查询学员信息')
        print('5.显示所有学员信息')
        print('6.保存学员信息')
        print('7.退出系统')
        print('-' * 40)

    def add_student(self):
        """
        添加学生信息(事务安全)
        
        流程:
        1. 收集用户输入
        2. 验证数据格式
        3. 执行数据库插入
        4. 更新内存缓存
        """
        try:
            name = input('请输入学员的姓名:')
            age = int(input('请输入学员的年龄:'))
            mobile = input('请输入学员的电话:')
            
            student = Student(name, age, mobile)
            self._validate_student(student)
            
            # 开启事务
            with self.conn_pool.get_connection() as conn:
                cursor = conn.cursor()
                cursor.execute(
                    "INSERT INTO students (name, age, mobile) VALUES (%s, %s, %s)",
                    (student.name, student.age, student.mobile)
                )
                conn.commit()
                
            self.student_list.append(student)
            print('学员信息已添加成功')
        except ValueError as e:
            print(f'输入格式错误:{e}')
        except Exception as e:
            print(f'添加失败:{e}')

    def del_student(self):
        # 提示用户输入要删除的学员姓名
        name = input('请输入要删除的学员姓名:')
        # 遍历学员列表
        for student in self.student_list:
            if student.name == name:
                self.student_list.remove(student)
                print(f'学员{name}已删除成功')
                return
        print(f'未找到名为{name}的学员')

    def mod_student(self):
        # 提示用户输入要修改的学员姓名
        name = input('请输入要修改的学员姓名:')
        # 遍历学员列表
        for student in self.student_list:
            if student.name == name:
                # 提示用户输入新的学员信息
                new_name = input(f'请输入新的姓名(原:{student.name}):')
                new_age = int(input(f'请输入新的年龄(原:{student.age}):'))
                new_mobile = input(f'请输入新的电话(原:{student.mobile}):')
                # 更新学员信息
                student.name = new_name
                student.age = new_age
                student.mobile = new_mobile
                print(f'学员{name}信息已修改成功')
                return
        print(f'未找到名为{name}的学员')

    def show_student(self):
        # 提示用户输入要查询的学员姓名
        name = input('请输入要查询的学员姓名:')
        # 遍历学员列表
        for student in self.student_list:
            if student.name == name:
                print(f'学员信息:姓名:{student.name}, 年龄:{student.age}, 电话:{student.mobile}')
                return
        print(f'未找到名为{name}的学员')

    def show_all(self):
        if not self.student_list:
            print('当前没有学员信息')
            return
        print('所有学员信息:')
        for student in self.student_list:
            print(f'姓名:{student.name}, 年龄:{student.age}, 电话:{student.mobile}')

    def save_student(self):
        try:
            with open('student.data', 'w', encoding='utf-8') as f:
                for student in self.student_list:
                    f.write(f'{student.name},{student.age},{student.mobile}\n')
            print('学员信息已保存成功')
        except Exception as e:
            print(f'保存学员信息失败:{e}')

    # 定义一个run()方法,专门用于实现对管理系统中各个功能调用
    def run(self):
        # 1、调用一个学员加载方法,用于加载文件中的所有学员信息,加载完成后,把得到的所有学员信息保存在student_list属性中
        self.load_student()
        # 2、显示帮助信息,提示用户输入要实现的功能编号
        while True:
            # 显示帮助信息
            self.show_help()
            # 提示用户输入要操作功能编号
            user_num = int(input('请输入要操作功能的编号:'))
            if user_num == 1:
                self.add_student()
            elif user_num == 2:
                self.del_student()
            elif user_num == 3:
                self.mod_student()
            elif user_num == 4:
                self.show_student()
            elif user_num == 5:
                self.show_all()
            elif user_num == 6:
                self.save_student()
            elif user_num == 7:
                print('感谢您使用Cyber4K 信息管理系统V1.0,欢迎下次使用!')
                break
            else:
                print('信息输入错误,请重新输入...')
2.2.3 编写add_student()学员添加方法实现
1. 需求

用户输入学员姓名、年龄、手机号,将学员添加到系统。

2. 步骤:
  • 用户输入姓名、年龄、手机号

  • 创建该学员对象(真正添加到列表中的是对象)

  • 将该学员对象添加到列表[] => append()

  • 代码:

python 复制代码
from student import Student  # 第一行添加
	...    
    ...
    ...
    def add_student(self):
        """
        添加学生信息(事务安全)
        
        流程:
        1. 收集用户输入
        2. 验证数据格式
        3. 执行数据库插入
        4. 更新内存缓存
        """
        try:
            name = input('请输入学员的姓名:')
            age = int(input('请输入学员的年龄:'))
            mobile = input('请输入学员的电话:')
            
            student = Student(name, age, mobile)
            self._validate_student(student)
            
            # 开启事务
            with self.conn_pool.get_connection() as conn:
                cursor = conn.cursor()
                cursor.execute(
                    "INSERT INTO students (name, age, mobile) VALUES (%s, %s, %s)",
                    (student.name, student.age, student.mobile)
                )
                conn.commit()
                
            self.student_list.append(student)
            print('学员信息已添加成功')
        except ValueError as e:
            print(f'输入格式错误:{e}')
        except Exception as e:
            print(f'添加失败:{e}')
2.2.4 编写del_student()学员删除方法实现
1. 需求

用户输入目标学员姓名,如果学员存在则删除该学员

2. 步骤
  • 用户输入目标学员姓名

  • 遍历学员数据列表,如果用户输入的学员姓名存在则删除,否则则提示该学员不存在

  • 代码:

python 复制代码
	def del_student(self):
        # 提示用户输入要删除的学员姓名
        name = input('请输入要删除的学员姓名:')
        # 对student_list属性(本质列表)进行遍历
        for i in self.student_list:
            if i.name == name:
                # 找到了要删除的学员,删除
                self.student_list.remove(i)
                print(f'学员{name}信息删除成功')
                break
        else:
            print('您要删除的学员不存在...')
2.2.5 编写mod_student()学员修改方法实现
  • 代码:
python 复制代码
	def mod_student(self):
        # 提示用户输入要修改的学员姓名
        name = input('请输入要修改的学员姓名:')
        # 对student_list属性进行遍历,判断要修改的学员姓名是否存在
        for i in self.student_list:
            if i.name == name:
                i.name = input('请输入修改后的学员姓名:')
                i.age = int(input('请输入修改后的学员年龄:'))
                i.mobile = input('请输入修改后的学员电话:')
                print(f'学员信息修改成功,修改后信息如下 => 学员姓名:{i.name},学员年龄:{i.age},学员电话:{i.mobile}')
                break
        else:
            print('您要修改的学员信息不存在...')
2.2.6 编写show_student()学员查询方法实现
  • 代码:
python 复制代码
	def show_student(self):
        # 提示用户输入要查询的学员姓名
        name = input('请输入要查询的学员姓名:')
        # 对student_list属性进行遍历
        for i in self.student_list:
            if i.name == name:
                print(i)
                break
        else:
            print('您要查找的学员信息不存在...')
2.2.7 编写show_all()方法查询所有学员实现
  • 代码:
python 复制代码
	def show_all(self):
        # 直接对列表进行遍历
        for i in self.student_list:
            print(i)

2.3 数据持久化实现

2.3.1 Database类实现 (database.py)
python 复制代码
import sqlite3
import mysql.connector
from typing import List, Optional, Tuple

class Database:
    def __init__(self, 
                 host: str = '192.168.11.132',
                 user: str = 'root',
                 password: str = 'root',
                 database: str = 'Cyber4K_student_information_management_system'):
        self.conn = mysql.connector.connect(
            host=host,
            user=user,
            password=password,
            database=database
        )
        self.cursor = self.conn.cursor()
        self._create_table()
    
    def _create_table(self):
        """创建学生表"""
        self.cursor.execute('''
            CREATE TABLE IF NOT EXISTS students (
                id INT AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(255) NOT NULL,
                age INT NOT NULL,
                mobile VARCHAR(20) NOT NULL
            )
        ''')
        self.conn.commit()
    
    def add_student(self, name: str, age: int, mobile: str) -> int:
        """添加学生"""
        self.cursor.execute(
            "INSERT INTO students (name, age, mobile) VALUES (%s, %s, %s)",
            (name, age, mobile)
        )
        self.conn.commit()
        return self.cursor.lastrowid
    
    def get_all_students(self) -> List[Tuple]:
        """获取所有学生"""
        self.cursor.execute("SELECT * FROM students")
        return self.cursor.fetchall()
    
    def get_student_by_name(self, name: str) -> Optional[Tuple]:
        """根据姓名查询学生"""
        self.cursor.execute("SELECT * FROM students WHERE name = %s", (name,))
        return self.cursor.fetchone()
    
    def update_student(self, student_id: int, name: str, age: int, mobile: str) -> bool:
        """更新学生信息"""
        self.cursor.execute(
            "UPDATE students SET name = %s, age = %s, mobile = %s WHERE id = %s",
            (name, age, mobile, student_id)
        )
        self.conn.commit()
        return self.cursor.rowcount > 0
    
    def delete_student(self, student_id: int) -> bool:
        """删除学生"""
        self.cursor.execute("DELETE FROM students WHERE id = %s", (student_id,))
        self.conn.commit()
        return self.cursor.rowcount > 0
    
    def search_students(self, name: Optional[str] = None, min_age: Optional[int] = None, 
                       max_age: Optional[int] = None) -> List[Tuple]:
        """根据条件查询学生"""
        query = "SELECT * FROM students WHERE 1=1"
        params = []
        
        if name:
            query += " AND name LIKE ?"
            params.append(f'%{name}%')
        if min_age is not None:
            query += " AND age >= ?"
            params.append(min_age)
        if max_age is not None:
            query += " AND age <= ?"
            params.append(max_age)
            
        self.cursor.execute(query, params)
        return self.cursor.fetchall()
    
    def close(self):
        """关闭数据库连接"""
        self.conn.close()
2.3.2 保存数据流程:
  • 从连接池获取连接

  • 开启事务

  • 批量执行INSERT

  • 异常回滚机制

python 复制代码
def save_student(self):
    """
    原子化保存所有学生数据到MySQL
    采用批量插入优化性能
    """
    conn = self.conn_pool.get_connection()
    try:
        with conn.cursor() as cursor:
            # 开启事务
            conn.start_transaction()
            
            # 清空现有数据
            cursor.execute('TRUNCATE TABLE students')
            
            # 批量插入
            sql = """
            INSERT INTO students (name, age, mobile) 
            VALUES (%(name)s, %(age)s, %(mobile)s)
            """
            data = [student.to_dict() for student in self.student_list]
            cursor.executemany(sql, data)
            
            # 提交事务
            conn.commit()
            print('[成功] 数据已持久化到MySQL')
            
    except Exception as e:
        conn.rollback()
        print(f'[错误] 数据保存失败: {e}')
    finally:
        conn.close()
2.3.3 数据加载优化:
  • 连接池管理
  • 自动建表
  • 类型安全转换
python 复制代码
def load_student(self):
    """
    从MySQL加载所有学生数据
    如果表不存在则自动创建
    """
    conn = self.conn_pool.get_connection()
    try:
        with conn.cursor(dictionary=True) as cursor:
            # 检查表是否存在
            cursor.execute("""
                SELECT table_name 
                FROM information_schema.tables 
                WHERE table_schema = 'student_db' 
                AND table_name = 'students'
            """)
            
            if not cursor.fetchone():
                # 自动建表
                cursor.execute("""
                    CREATE TABLE students (
                        id INT AUTO_INCREMENT PRIMARY KEY,
                        name VARCHAR(50) NOT NULL,
                        age INT CHECK (age > 0),
                        mobile VARCHAR(20)
                    ) ENGINE=InnoDB
                """)
                conn.commit()
                print('[提示] 已创建students表')
            
            # 加载数据
            cursor.execute('SELECT name, age, mobile FROM students')
            self.student_list = [
                Student(row['name'], row['age'], row['mobile'])
                for row in cursor.fetchall()
            ]
            
    except Exception as e:
        print(f'[错误] 数据加载失败: {e}')
    finally:
        conn.close()

3. 完整代码实现

3.1 student.py (数据模型层)

python 复制代码
# 定义一个Student类
class Student():
    # 定义魔术方法,用于初始化属性信息
    def __init__(self, name, age, mobile):
        self.name = name
        self.age = age
        self.mobile = mobile
    # 定义魔术方法,用于打印输出学员信息
    def __str__(self):
        return f'{self.name}, {self.age}, {self.mobile}'

3.2 database.py (数据持久化层)

python 复制代码
import sqlite3
import mysql.connector
from typing import List, Optional, Tuple

class Database:
    def __init__(self, 
                 host: str = '192.168.11.132',
                 user: str = 'root',
                 password: str = 'root',
                 database: str = 'Cyber4K_student_information_management_system'):
        self.conn = mysql.connector.connect(
            host=host,
            user=user,
            password=password,
            database=database
        )
        self.cursor = self.conn.cursor()
        self._create_table()
    
    def _create_table(self):
        """创建学生表"""
        self.cursor.execute('''
            CREATE TABLE IF NOT EXISTS students (
                id INT AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(255) NOT NULL,
                age INT NOT NULL,
                mobile VARCHAR(20) NOT NULL
            )
        ''')
        self.conn.commit()
    
    def add_student(self, name: str, age: int, mobile: str) -> int:
        """添加学生"""
        self.cursor.execute(
            "INSERT INTO students (name, age, mobile) VALUES (%s, %s, %s)",
            (name, age, mobile)
        )
        self.conn.commit()
        return self.cursor.lastrowid
    
    def get_all_students(self) -> List[Tuple]:
        """获取所有学生"""
        self.cursor.execute("SELECT * FROM students")
        return self.cursor.fetchall()
    
    def get_student_by_name(self, name: str) -> Optional[Tuple]:
        """根据姓名查询学生"""
        self.cursor.execute("SELECT * FROM students WHERE name = %s", (name,))
        return self.cursor.fetchone()

3.3 main.py (程序入口)

python 复制代码
from studentManager import StudentManager

if __name__ == '__main__':
    # 1. 实例化StudentManager对象
    manager = StudentManager()
    # 2. 调用run()方法,启动管理系统
    manager.run()

3.4 studentManager.py (业务逻辑层)

python 复制代码
from student import Student


class StudentManager(object):
    # 定义一个__init__魔术方法,用于初始化数据
    def __init__(self):
        """
        初始化学生管理系统
        
        组件:
        - 内存缓存:student_list
        - 数据库连接池
        - 事务管理器
        """
        self.student_list = []  # 内存数据缓存
        self._init_db_connection()

    # 定义load_student()方法
    def load_student(self):
        # 捕获异常
        try:
            f = open('student.data', 'r', encoding='utf-8')
        except:
            f = open('student.data', 'w', encoding='utf-8')
        else:
            # 如果文件存在,没有异常,则执行else语句
            content = f.read()
            # 把字符串转换为原数据类型[{}, {}, {}]
            data = eval(content)
            # 把列表中的所有字典 => 转换为对象
            self.student_list = [Student(i['name'], i['age'], i['mobile']) for i in data]

        finally:
            f.close()

    # 定义静态show_help()方法
    @staticmethod
    def show_help():
        print('-' * 40)
        print('openlab信息管理系统V2.0')
        print('1.添加学员信息')
        print('2.删除学员信息')
        print('3.修改学员信息')
        print('4.查询学员信息')
        print('5.显示所有学员信息')
        # V2.0新增功能
        print('6.保存学员信息')
        print('7.退出系统')
        print('-' * 40)

    def add_student(self):
        """
        添加学生信息(事务安全)
        
        流程:
        1. 收集用户输入
        2. 验证数据格式
        3. 执行数据库插入
        4. 更新内存缓存
        """
        try:
            name = input('请输入学员的姓名:')
            age = int(input('请输入学员的年龄:'))
            mobile = input('请输入学员的电话:')
            
            student = Student(name, age, mobile)
            self._validate_student(student)
            
            # 开启事务
            with self.conn_pool.get_connection() as conn:
                cursor = conn.cursor()
                cursor.execute(
                    "INSERT INTO students (name, age, mobile) VALUES (%s, %s, %s)",
                    (student.name, student.age, student.mobile)
                )
                conn.commit()
                
            self.student_list.append(student)
            print('学员信息已添加成功')
        except ValueError as e:
            print(f'输入格式错误:{e}')
        except Exception as e:
            print(f'添加失败:{e}')

    def del_student(self):
        # 提示用户输入要删除的学员姓名
        name = input('请输入要删除的学员姓名:')
        # 对student_list属性(本质列表)进行遍历
        for i in self.student_list:
            if i.name == name:
                # 找到了要删除的学员,删除
                self.student_list.remove(i)
                print(f'学员{name}信息删除成功')
                break
        else:
            print('您要删除的学员不存在...')

    def mod_student(self):
        # 提示用户输入要修改的学员姓名
        name = input('请输入要修改的学员姓名:')
        # 对student_list属性进行遍历,判断要修改的学员姓名是否存在
        for i in self.student_list:
            if i.name == name:
                i.name = input('请输入修改后的学员姓名:')
                i.age = int(input('请输入修改后的学员年龄:'))
                i.mobile = input('请输入修改后的学员电话:')
                print(f'学员信息修改成功,修改后信息如下 => 学员姓名:{i.name},学员年龄:{i.age},学员电话:{i.mobile}')
                break
        else:
            print('您要修改的学员信息不存在...')

    def show_student(self):
        # 提示用户输入要查询的学员姓名
        name = input('请输入要查询的学员姓名:')
        # 对student_list属性进行遍历
        for i in self.student_list:
            if i.name == name:
                print(i)
                break
        else:
            print('您要查找的学员信息不存在...')

    def show_all(self):
        # 直接对列表进行遍历
        for i in self.student_list:
            print(i)

    def save_student(self):
        # 打开文件
        f = open('student.data', 'w', encoding='utf-8')
        # 把列表中的对象转换为字典
        new_list = [i.__dict__ for i in self.student_list]
        # 文件读写(写入)
        f.write(str(new_list))
        # 关闭文件
        f.close()
        # 提示用户数据已经保存成功了
        print('学员信息保存成功')

    # 定义一个run()方法,专门用于实现对管理系统中各个功能调用
    def run(self):
        # 1、调用一个学员加载方法,用于加载文件中的所有学员信息,加载完成后,把得到的所有学员信息保存在student_list属性中
        self.load_student()
        # 2、显示帮助信息,提示用户输入要实现的功能编号
        while True:
            # 显示帮助信息
            self.show_help()
            # 提示用户输入要操作功能编号
            user_num = int(input('请输入要操作功能的编号:'))
            if user_num == 1:
                self.add_student()
            elif user_num == 2:
                self.del_student()
            elif user_num == 3:
                self.mod_student()
            elif user_num == 4:
                self.show_student()
            elif user_num == 5:
                self.show_all()
            elif user_num == 6:
                self.save_student()
            elif user_num == 7:
                print('感谢您使用openlab信息管理系统V2.0,欢迎下次使用!')
                break
            else:
                print('信息输入错误,请重新输入...')
相关推荐
Amo Xiang5 小时前
Python 解释器安装全攻略(适用于 Linux / Windows / macOS)
linux·windows·python·环境安装
程序员杰哥5 小时前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
浩皓素6 小时前
用Python开启游戏开发之旅
python
hello kitty w6 小时前
Python学习(6) ----- Python2和Python3的区别
开发语言·python·学习
互联网杂货铺8 小时前
功能测试、性能测试、安全测试详解
自动化测试·软件测试·python·功能测试·测试工具·性能测试·安全性测试
土豆杨6268 小时前
隐藏层-机器学习
python·机器学习
Dxy12393102169 小时前
DrissionPage调试工具:网页自动化与数据采集的革新利器
爬虫·python·drissionpage
不争先.10 小时前
URL 结构说明+路由(接口)的认识
python·pycharm·flask·apifox
(・Д・)ノ10 小时前
python打卡day44
人工智能·python·机器学习
胡西风_foxww10 小时前
Python 入门到进阶全指南:从语言特性到实战项目
开发语言·python·快速入门