python: more Layer Architecture and its Implementation in Python and mysql 8.0

news/2024/11/23 13:14:53/

mysql 8.0

drop table DuStudentList;#学生表create table DuStudentList
(StudentId INT NOT NULL AUTO_INCREMENT comment'主键id',  #自动增加,StudentName nvarchar(50) comment'学生姓名',StudentNO varchar(50) comment'学号',                    #学号StudentBirthday datetime comment'学生生日',                  #学生生日     primary key(StudentId)
) comment='学生表';

Model:

"""
StudentListInfo.py
学生类
date 2023-06-16
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""import datetime
from datetime import date
import sys
import os
import Commonclass StudentList(object):"""学生类"""def __init__(self,StudentId:int,StudentName:str, StudentNO:str,StudentBirthday:datetime.datetime,age:int):""":param StudentName::param StudentNO::param StudentBirthday:"""self._StudentName=StudentNameself._StudentNO=StudentNOself._StudentBirthday=StudentBirthdayself._StudentId=StudentIdself._age=age #date.today().year-StudentBirthday.year #date.today().year-StudentBirthday.year #Common.Commond.calculateAge(date(StudentBirthday.year,StudentBirthday.month,StudentBirthday.day))'''def __init__(self,StudentId:int,StudentName:str, StudentNO:str,StudentBirthday:datetime.datetime):""":param StudentName::param StudentNO::param StudentBirthday:"""self._StudentName=StudentNameself._StudentNO=StudentNOself._StudentBirthday=StudentBirthdayself._StudentId=StudentIdself._age=0 #date.today().year-StudentBirthday.year #date.today().year-StudentBirthday.year #Common.Commond.calculateAge(date(StudentBirthday.year,StudentBirthday.month,StudentBirthday.day))'''def __del__(self):""":return:"""print(f"geovindu")def setStudentName(self,StudentName):""":param StudentName::return:"""self._StudentName = StudentNamedef getStudentName(self):""":return:"""return self._StudentNamedef setStudentNO(self,StudentNO):""":param StudentNO::return:"""self._StudentNO=StudentNOdef getStudentNO(self):""":return:"""return self._StudentNOdef setStudentId(self,StudentId):""":param StudentId::return:"""self._StudentId=StudentIddef getStudentId(self):""":return:"""return  self._StudentIddef setStudentBirthday(self,StudentBirthday):""":param StudentBirthday::return:"""self._StudentBirthday = StudentBirthdaydage =date.today().year-StudentBirthday.year# Common.Commond.calculate_age(StudentBirthday)self._age=dagedef getStudentBirthday(self):""":return:"""return self._StudentBirthdaydef setAge(self,age):""":param age::return:"""dage=1 #Common.Commond.calculate_age(StudentBirthday)self._age = agedef getAge(self):""":return:"""return self._agedef __str__(self):""":return:"""return f"{self._StudentId},{self._StudentName},{self._StudentNO},{self._StudentBirthday}{self._age}"

DAL:

"""
StudentDALListDAL.py
数据业务处理层 Data Access Layer (DAL)
SQL Server 数据库操作
date 2023-06-21
edit: Geovin Du,geovindu, 涂聚文
ide: PyCharm 2023.1 python 11
"""import os
import sys
from pathlib import Path
import re
import pymssql  #sql server
import Model.StudentListInfo
import DBUtility.MySqlHelper
import Interface.IStudentListclass StudentDal(Interface.IStudentList.IStudentList):"""数据业务处理层  学生数据库连接可以放在这里,通过配置读取数据连接参数"""myms=DBUtility.MySqlHelper.MySqlHelper()def __init__(self):"""构造函数,方法:param strserver::param struser::param strpwd::param strdatabase:"""self._strserver = ""self._struser = ""self._strpwd = ""self._strdatabase =""def selectSql(cls):"""查询数据 self._strserver, self._struser, self._strpwd, self._strdatabase:return:"""row=cls.myms.execute('select *,TIMESTAMPDIFF(hour,StudentBirthday,now())/8766 as Age from DuStudentList;')return rowdef selectSqlOrder(cls,order:str)->list:""":param order:  studenName desc/asc:return:"""students=[]strsql=f"select * from DuStudentList order by {order};"row=cls.myms.execute(f'select *,TIMESTAMPDIFF(hour,StudentBirthday,now())/8766 as Age from DuStudentList order by {order};')return rowdef selectIdSql(cls,StudentId:int):""":param StudentId: 主键ID:return:"""row=cls.myms.execute(f'select * from DuStudentList where StudentId={StudentId};')return rowdef selectProc(cls):"""存储过程:return:"""args = ()row = cls.myms.executeCallProc("proc_Select_StudentListAll",args)return rowdef selectIdProc(cls,StudentId:int):"""存储过程:param StudentId: 主键ID:return:"""args = (StudentId,)row = cls.myms.executeCallProc('proc_Select_StudentList', args)return rowdef addSql(cls,info:Model.StudentListInfo.StudentList):"""添加,要考虑添加返回ID值:param info:学生实体类:return:"""column=("StudentName","StudentNO","StudentBirthday")vales=[info.getStudentName(),info.getStudentNO(),info.getStudentBirthday()]cls.myms.insertByColumnaAndValues("DuStudentList",column,vales)def addProc(cls,info:Model.StudentListInfo.StudentList):"""添加,要考虑添加返回ID值:param info:学生实体类:return:"""args=(info.getStudentName(),info.getStudentNO(),info.getStudentBirthday())cls.myms.insertCallProc("proc_Insert_StudentList",args)def addOutProc(cls,info:Model.StudentListInfo.StudentList):"""添加,要考虑添加返回ID值:param info:学生实体类:return: 返回增加的学生的ID"""id = 0try:outid =('int',) #输出,元组类型print(info)args = (info.getStudentName(), info.getStudentNO(), info.getStudentBirthday(),outid)print(args)result=cls.myms.insertOutCallProc("proc_Insert_StudentListOutput", args)print(result)id = result[0]except Exception as ex:print(ex)return iddef editSql(cls,info:Model.StudentListInfo.StudentList):""":param info:学生实体类:return:"""args = {"StudentName":f"{info.getStudentName()}","StudentNO":f"{info.getStudentNO()}","StudentBirthday":f"{info.getStudentBirthday()}"}  #"StudentId":6where = f"StudentId={info.getStudentId()}" ##print(args,where)cls.myms.updateByKeyValues("DuStudentList",where,args)def editProc(cls, info: Model.StudentListInfo.StudentList):""":param info: 学生实体类:return:"""args = (info.getStudentId(),info.getStudentName(),info.getStudentNO(),info.getStudentBirthday())cls.myms.updateProc("proc_Update_StudentList",args)def delSql(cls,StudentId:int):"""sql语句删除:param StudentId: 主键ID:return:"""where={f"StudentId":StudentId}cls.myms.deleteByKeyValues("DuStudentList",where)def delProc(cls, studentId):"""删除 存储过程 删除多个ID,后面增加:param StudentId: 主键ID:return:"""args =studentIdcls.myms.deleteProc("proc_Delete_StudentList",args)

IDAL:

"""
IStudentList.py
接口层 Interface Data Access Layer
IDAL(Interface Data Access Layer)DAL的接口层
date 2023-06-19
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""
from __future__ import annotations
from abc import ABC, abstractmethod
import os
import sys
import Model.StudentListInfoclass IStudentList(ABC):""""""@classmethoddef __subclasshook__(cls, subclass):return (hasattr(subclass, 'load_data_source') andcallable(subclass.load_data_source) andhasattr(subclass, 'extract_text') andcallable(subclass.extract_text) orNotImplemented)@abstractmethoddef selectSql(cls):""":return:"""pass@abstractmethoddef selectSqlOrder(cls, order: str) -> list:""":param order::return:"""pass@abstractmethoddef selectIdSql(cls, StudentId: int):""":param StudentId::return:"""pass@abstractmethoddef selectProc(cls):""":return:"""pass@abstractmethoddef selectIdProc(cls, StudentId: int):""":param StudentId::return:"""pass@abstractmethoddef addSql(cls, info: Model.StudentListInfo.StudentList):""":param info::return:"""pass@abstractmethoddef addProc(cls, info: Model.StudentListInfo.StudentList):""":param info::return:"""pass@abstractmethoddef addOutProc(cls, info: Model.StudentListInfo.StudentList):""":param info::return:"""pass@abstractmethoddef editSql(cls, info: Model.StudentListInfo.StudentList):""":param info::return:"""pass@abstractmethoddef editProc(cls, info: Model.StudentListInfo.StudentList):""":param info::return:"""pass@abstractmethoddef delSql(cls, StudentId: int):""":param StudentId::return:"""pass@abstractmethoddef delProc(cls, studentId):""":param studentId::return:"""pass

factory:

@abstractmethoddef createStudentList(self)->Interface.IStudentList.IStudentList:"""生成(创建)接口:return:"""dals= Interface.IStudentList.IStudentListdal=DAL.StudentListDAL.StudentDal()dals=dalreturn dals

BLL:

"""
StudentListBLL.py
业务层 Business Logic Layer (BLL)
date 2023-06-19
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""import os
import sys
from pathlib import Path
import re
import pymssql  #sql server
from datetime import date
import DAL.StudentListDAL
#import DAL.ConfigDAL
import Model.StudentListInfo
import Interface.IStudentList
import Factory.AbstractFactoryclass StudentBll(object):"""学生信息操作业务类"""dal=Factory.AbstractFactory.AbstractFactory.createStudentList#dal =DAL.StudentListDAL.StudentDal() #Factory.AbstractFactory.AbstractFactory.createStudentList()#"""类属性 操作DAL"""def __init__(self):""""""self._name = "geovindu"def __del__(self):print(f"{self._name}挂失了")def selectSql(cls)->list:"""元组数据:return: list 学生列表"""students = []data = cls.dal().selectSql()stus = list(data)  # 如C# 强制转换'''for a in data:for i in a:print("II",i[0],i[1],i[2],i[3],i[4])'''print(stus)for ii in stus:for i in ii:students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3],i[4]))return studentsdef selectSqlOrder(cls, order: str)->list:"""元组数据:param order: studenName desc/asc:return:"""studentsorder = []students=[]data = cls.dal().selectSqlOrder(order)(studentsorder) = data # 如C# 强制转换'''for i in range(len(studentsorder)):print("rrr",type(studentsorder[i]))for duobj in studentsorder[i]:print(type(duobj))print(duobj)'''for obj in studentsorder:for i in obj:students.append(Model.StudentListInfo.StudentList(i[0], i[1], i[2], i[3],i[4]))return studentsdef selectIdSql(cls,StudentId:int)->list:""":param StudentId:学生ID:return:"""students = []data = cls.dal().selectIdSql(StudentId)students=datafor ii in students:for i in ii:students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3]))return studentsdef selectProc(cls):""":return:"""students=[]data = cls.dal().selectProc()for i in data:students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3],i[4]))return studentsdef selectIdProc(cls,StudentId:int)->list:""":param StudentId::return:"""students = []data = cls.dal().selectIdProc(StudentId)for i in data:students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3]))return studentsdef addSql(cls,info:Model.StudentListInfo.StudentList):""":param info:学生实体类:return:"""cls.dal.addSql(info)def addProc(cls,info:Model.StudentListInfo.StudentList):""":param info:学生实体类:return:"""#print(info)cls.dal().addProc(info)def addOutProc(cls,info:Model.StudentListInfo.StudentList)->int:""":param info: 学生实体类:return: 返回增加的学生ID"""print(info)return cls.dal().addOutProc(info)def editSql(cls,info:Model.StudentListInfo.StudentList):""":param info:学生实体类:return:"""#print(info)cls.dal().editSql(info)def editProc(cls, info: Model.StudentListInfo.StudentList):""":param info:学生实体类:return:"""cls.dal().editProc(info)def delSql(cls, StudentId: int):""":param StudentId::return:"""cls.dal().delSql(StudentId)def delProc(cls, StudentId):""":param StudentId::return:"""cls.dal().delProc(StudentId)

调用:

stubll = BLL.StudentListBLL.StudentBll()#dd=stubll.addOutProc(StudentListInfo.StudentList(0,"杜村3", "010", "2007-05-28"))#print("dd",dd)#查询学生sl=stubll.selectSql()for s in sl:print(s)#outid=stubll.addOutProc(Model.StudentListInfo.StudentList(0,"陈三","006",datetime.datetime(2007,3,14),5))#print(outid)

更多查阅个人博客园:

®Geovin Du Dream Park™ - 博客园 (cnblogs.com)icon-default.png?t=N5K3https://www.cnblogs.com/geovindu/


http://www.ppmy.cn/news/706123.html

相关文章

PHP:提交表单之后出现本页面刷新的情况,导致路径参数消失

问题: 提交表单前 提交表单后 刷新之后,参数消失 解决办法 在提交表单的方法中写入代码携带参数的代码 样例: 实施 header(Location: table_detial.php?table_name.$table_name.);

净水市场新发展 慕辰净水推出全屋净水定制

净水市场新发展 慕辰净水推出全屋净水定制 近年来水污染、空气污染等环境问题日益严峻,给人们的健康带来极大的困扰,“喝一杯健康的水”成为人们迫切的需求。据统计有90%以上的城市水域也遭到污染,这也让人们对生活和饮用水的健康安全问题给予…

制约净水器覆盖率低的因素有哪些净水器哪个牌子好

净水器发展得那么火,为啥覆盖率还是上升缓慢? 净水器行业虽然进入火爆阶段,但不可否认的是市场的覆盖率是很低的,全国的覆盖率才4%左右远远低于发达的国家。我们都知道 净水器从上个世界80年代就进入中国市场,到现在已…

为什么需要全屋净水,喝的水干净不就可以了?

家用净水器近些年虽然普及率高,但新泩全屋净水很多用户都并不了解,很多都是在装修或暖通公司推荐下才听说的,至于有没有必要?如何选择?如何配置?完全没有概念! 据世界卫生组织(WHO)的相关研究资…

反渗透和和超滤究竟有和不同 今天终于讲清楚了

反渗透和和超滤究竟有和不同 今天终于讲清楚了 随着净水器的普及,众多“概念化”的宣传,让消费者卷入了“漩涡”,超滤反渗透傻傻分不清,分辨不出到底该买哪种净水器,哪种适合自己。下面,就来为小伙伴们详解…

【“宏孩儿”入门】通过宏定义将类型和函数结构解耦

想象这样一个场景,我们有一个表结构体StudentInfo,同时用数组存储它作为一张表,我们需要提供一套根据Key来增删改查的函数操作这张表。 (这里可能有聪明的小伙伴会想到为什么不用哈希表来存储,这样不就自带一套根据Key来增删改查函…

TIM快捷键和pycharm格式化快捷键冲突问题

TIM快捷键和pycharm格式化快捷键冲突问题 每次敲完代码,想要格式化时,当我同时按下Ctrl Alt L时,代码并没有格式化,而是会弹出TIM的锁定页面,气煞我也,然后,我就去求助博学的度老师&#xff0…

【Redis】不卡壳的 Redis 学习之路:事务

本文是Redis系列第三篇,前两篇欢迎移步 【Redis】不卡壳的 Redis 学习之路:从十大数据类型开始入手_AQin1012的博客-CSDN博客关于Redis的数据类型,各个文章总有些小不同,我们这里讨论的是Redis 7.0,为确保准确&#x…