图书管理系统至少含有两个增删改查操作,至少含有一个非增删改查业务需求,采用至少三层Web项目架构的后端详细代码
时间: 2024-03-10 13:46:58 浏览: 106
由于这是一个比较复杂的代码需求,我无法在这里给出完整的代码。但我可以给您提供一个大致的代码框架,供您参考:
1. 数据库设计
- 图书表(book):id、书名、作者、出版社、出版时间、价格等字段;
- 用户表(user):id、用户名、密码、角色等字段;
- 借阅记录表(borrowing):id、用户id、图书id、借阅时间、归还时间等字段。
2. 三层Web项目架构
- 数据访问层(Data Access Layer,DAL):负责与数据库进行交互,封装数据库操作的方法;
- 业务逻辑层(Business Logic Layer,BLL):负责处理业务逻辑,调用DAL层的方法进行数据的增删改查等操作;
- 表现层(Presentation Layer,PL):负责与用户进行交互,展现数据,接受用户的请求并传递给BLL层进行处理。
3. 后端代码实现
DAL层
```python
import mysql.connector
class BookDAL:
def __init__(self, config):
self.conn = mysql.connector.connect(**config)
self.cursor = self.conn.cursor()
def __del__(self):
self.conn.close()
# 获取所有图书
def get_all_books(self):
sql = "SELECT * FROM book"
self.cursor.execute(sql)
return self.cursor.fetchall()
# 添加图书
def add_book(self, book):
sql = "INSERT INTO book (name, author, publisher, publish_date, price) VALUES(%s, %s, %s, %s, %s)"
self.cursor.execute(sql, (book['name'], book['author'], book['publisher'], book['publish_date'], book['price']))
self.conn.commit()
return self.cursor.lastrowid
# 修改图书信息
def update_book(self, book):
sql = "UPDATE book SET name=%s, author=%s, publisher=%s, publish_date=%s, price=%s WHERE id=%s"
self.cursor.execute(sql, (book['name'], book['author'], book['publisher'], book['publish_date'], book['price'], book['id']))
self.conn.commit()
return self.cursor.rowcount
# 删除图书
def delete_book(self, book_id):
sql = "DELETE FROM book WHERE id=%s"
self.cursor.execute(sql, (book_id,))
self.conn.commit()
return self.cursor.rowcount
class UserDAL:
def __init__(self, config):
self.conn = mysql.connector.connect(**config)
self.cursor = self.conn.cursor()
def __del__(self):
self.conn.close()
# 获取所有用户
def get_all_users(self):
sql = "SELECT * FROM user"
self.cursor.execute(sql)
return self.cursor.fetchall()
# 添加用户
def add_user(self, user):
sql = "INSERT INTO user (username, password, role) VALUES(%s, %s, %s)"
self.cursor.execute(sql, (user['username'], user['password'], user['role']))
self.conn.commit()
return self.cursor.lastrowid
# 修改用户信息
def update_user(self, user):
sql = "UPDATE user SET username=%s, password=%s, role=%s WHERE id=%s"
self.cursor.execute(sql, (user['username'], user['password'], user['role'], user['id']))
self.conn.commit()
return self.cursor.rowcount
# 删除用户
def delete_user(self, user_id):
sql = "DELETE FROM user WHERE id=%s"
self.cursor.execute(sql, (user_id,))
self.conn.commit()
return self.cursor.rowcount
class BorrowingDAL:
def __init__(self, config):
self.conn = mysql.connector.connect(**config)
self.cursor = self.conn.cursor()
def __del__(self):
self.conn.close()
# 获取所有借阅记录
def get_all_borrowings(self):
sql = "SELECT * FROM borrowing"
self.cursor.execute(sql)
return self.cursor.fetchall()
# 添加借阅记录
def add_borrowing(self, borrowing):
sql = "INSERT INTO borrowing (user_id, book_id, borrowing_date, return_date) VALUES(%s, %s, %s, %s)"
self.cursor.execute(sql, (borrowing['user_id'], borrowing['book_id'], borrowing['borrowing_date'], borrowing['return_date']))
self.conn.commit()
return self.cursor.lastrowid
# 修改借阅记录信息
def update_borrowing(self, borrowing):
sql = "UPDATE borrowing SET user_id=%s, book_id=%s, borrowing_date=%s, return_date=%s WHERE id=%s"
self.cursor.execute(sql, (borrowing['user_id'], borrowing['book_id'], borrowing['borrowing_date'], borrowing['return_date'], borrowing['id']))
self.conn.commit()
return self.cursor.rowcount
# 删除借阅记录
def delete_borrowing(self, borrowing_id):
sql = "DELETE FROM borrowing WHERE id=%s"
self.cursor.execute(sql, (borrowing_id,))
self.conn.commit()
return self.cursor.rowcount
```
BLL层
```python
from DAL import BookDAL, UserDAL, BorrowingDAL
class BookBLL:
def __init__(self, config):
self.dal = BookDAL(config)
# 获取所有图书
def get_all_books(self):
return self.dal.get_all_books()
# 添加图书
def add_book(self, book):
return self.dal.add_book(book)
# 修改图书信息
def update_book(self, book):
return self.dal.update_book(book)
# 删除图书
def delete_book(self, book_id):
return self.dal.delete_book(book_id)
class UserBLL:
def __init__(self, config):
self.dal = UserDAL(config)
# 获取所有用户
def get_all_users(self):
return self.dal.get_all_users()
# 添加用户
def add_user(self, user):
return self.dal.add_user(user)
# 修改用户信息
def update_user(self, user):
return self.dal.update_user(user)
# 删除用户
def delete_user(self, user_id):
return self.dal.delete_user(user_id)
class BorrowingBLL:
def __init__(self, config):
self.dal = BorrowingDAL(config)
# 获取所有借阅记录
def get_all_borrowings(self):
return self.dal.get_all_borrowings()
# 添加借阅记录
def add_borrowing(self, borrowing):
return self.dal.add_borrowing(borrowing)
# 修改借阅记录信息
def update_borrowing(self, borrowing):
return self.dal.update_borrowing(borrowing)
# 删除借阅记录
def delete_borrowing(self, borrowing_id):
return self.dal.delete_borrowing(borrowing_id)
```
PL层
```python
from flask import Flask, request, jsonify
from BLL import BookBLL, UserBLL, BorrowingBLL
app = Flask(__name__)
config = {
'user': 'root',
'password': '123456',
'host': 'localhost',
'database': 'library'
}
book_bll = BookBLL(config)
user_bll = UserBLL(config)
borrowing_bll = BorrowingBLL(config)
@app.route('/books', methods=['GET'])
def get_all_books():
books = book_bll.get_all_books()
return jsonify(books)
@app.route('/books', methods=['POST'])
def add_book():
book = request.json
book_id = book_bll.add_book(book)
return jsonify({'id': book_id})
@app.route('/books', methods=['PUT'])
def update_book():
book = request.json
result = book_bll.update_book(book)
return jsonify({'affected_rows': result})
@app.route('/books/<int:book_id>', methods=['DELETE'])
def delete_book(book_id):
result = book_bll.delete_book(book_id)
return jsonify({'affected_rows': result})
@app.route('/users', methods=['GET'])
def get_all_users():
users = user_bll.get_all_users()
return jsonify(users)
@app.route('/users', methods=['POST'])
def add_user():
user = request.json
user_id = user_bll.add_user(user)
return jsonify({'id': user_id})
@app.route('/users', methods=['PUT'])
def update_user():
user = request.json
result = user_bll.update_user(user)
return jsonify({'affected_rows': result})
@app.route('/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
result = user_bll.delete_user(user_id)
return jsonify({'affected_rows': result})
@app.route('/borrowings', methods=['GET'])
def get_all_borrowings():
borrowings = borrowing_bll.get_all_borrowings()
return jsonify(borrowings)
@app.route('/borrowings', methods=['POST'])
def add_borrowing():
borrowing = request.json
borrowing_id = borrowing_bll.add_borrowing(borrowing)
return jsonify({'id': borrowing_id})
@app.route('/borrowings', methods=['PUT'])
def update_borrowing():
borrowing = request.json
result = borrowing_bll.update_borrowing(borrowing)
return jsonify({'affected_rows': result})
@app.route('/borrowings/<int:borrowing_id>', methods=['DELETE'])
def delete_borrowing(borrowing_id):
result = borrowing_bll.delete_borrowing(borrowing_id)
return jsonify({'affected_rows': result})
if __name__ == '__main__':
app.run(debug=True)
```
以上是一个大致的代码框架,供您参考。如果您有任何问题或疑问,欢迎随时与我联系。
阅读全文