银行示例库 使用说明
1 银行业务系统设计
系统包含以下实体,以模拟真实的银行业务系统:
- 客户 (Customers):存储客户个人信息。
- 账户 (Accounts):管理客户账户(如储蓄账户、支票账户)。
- 交易 (Transactions):记录账户的交易(存款、取款、转账)。
- 分行 (Branches):表示银行分行信息。
- 员工 (Employees):管理银行员工。
- 贷款 (Loans):跟踪客户贷款。
- 银行卡 (Cards):管理与账户关联的借记卡/信用卡。
2 ER 图描述
ER 图包括以下实体及其关系:
- 客户:包含 customer_id、first_name、last_name、email、phone、address、date_of_birth 等属性。
- 账户:通过 customer_id 关联客户,包含 account_id、account_type、balance、branch_id、created_at 等属性。
- 交易:通过 account_id 关联账户,包含 transaction_id、amount、transaction_type、transaction_date 等属性。
- 分行:包含 branch_id、name、address 等属性。
- 员工:通过 branch_id 关联分行,包含 employee_id、name、position 等属性。
- 贷款:通过 customer_id 和 account_id 关联客户和账户,包含 loan_id、amount、interest_rate、status 等属性。
- 银行卡:通过 account_id 关联账户,包含 card_id、card_number、card_type、expiry_date 等属性。
关系:
- 一个客户可以有多个账户(1:N)。
- 一个账户可以有多个交易(1:N)。
- 一个分行可以有多个账户和员工(1:N)。
- 一个客户可以有多个贷款(1:N)。
- 一个账户可以有多个银行卡(1:N)。
3 数据导入
3.1 数据库创建
dropdb -U system banking;
createdb -U system banking;
3.2 二进制格式导入
sys_restore -U system -d banking -c -Fc /install/banking_data.dmp
3.3 文本格式导入
#kingbase>
ksql banking system
#test>
\i /install/banking_data.sql
4 应用程序
4.1 上传后端应用程序与前端页面
#kingbase>
ls -ltra /install/main.py
ls -ltra /install/index.html
4.2 运行后端
- 安装 FastAPI 和 Uvicorn:
pip install fastapi uvicorn。 - 启动 FastAPI 服务:
- #kingbase>
- cd /install
uvicorn main:app --reload - API 地址:
http://localhost:8000。
4.3 运行前端
- 将
index.html保存到本地并在浏览器中打开。 - 或者使用简单 HTTP 服务器运行:
- #kingbase>
- cd /install
python -m http.server 8080 - 访问地址:
http://localhost:8080。
4.4 使用管理界面
- 前端界面支持:
- 查看客户列表(支持分页:上一页/下一页)。
- 按姓名或邮箱搜索客户。
- 通过表单添加新客户。
- 删除客户。
- 后端 API 支持客户、账户和交易的增删改查操作,可扩展前端以管理其他实体。
以下展示如何使用 curl 工具调用之前提供的 FastAPI 后端接口(main.py)来执行银行业务系统的增删改查操作。接口运行在 http://localhost:8000,以下示例涵盖客户(Customers)、账户(Accounts)和交易(Transactions)的常见操作,包括分页查询、搜索等功能。每个示例都包括请求和可能的响应。
5 API 接口说明
5.1 客户(Customers)接口示例
5.1.1 创建客户(POST /customers/)
请求:
#kingbase>
curl -X POST "http://localhost:8000/customers/" \
-H "Content-Type: application/json" \
-d '{
"first_name": "张",
"last_name": "伟",
"email": "zhang.wei@example.com",
"phone": "13812345678",
"address": "北京市朝阳区幸福路1号",
"date_of_birth": "1985-05-20"
}'
响应(成功):
{
"customer_id": 1,
"first_name": "张",
"last_name": "伟",
"email": "zhang.wei@example.com",
"phone": "13812345678",
"address": "北京市朝阳区幸福路1号",
"date_of_birth": "1985-05-20",
"created_at": "2025-07-15T12:30:00.123456"
}
5.1.2 查询客户列表(GET /customers/,支持分页和搜索)
请求(分页:第1页,每页10条,搜索“张”):
#kingbase>
curl -X GET "http://localhost:8000/customers/?page=1&per_page=10&search=张"
响应(成功,返回匹配的客户列表):
[
{
"customer_id": 1,
"first_name": "张",
"last_name": "伟",
"email": "zhang.wei@example.com",
"phone": "13812345678",
"address": "北京市朝阳区幸福路1号",
"date_of_birth": "1985-05-20",
"created_at": "2025-07-15T12:30:00.123456"
}
]
5.1.3 获取单个客户(GET /customers/{id})
请求:
#kingbase>
curl -X GET "http://localhost:8000/customers/1"
响应(成功):
{
"customer_id": 1,
"first_name": "张",
"last_name": "伟",
"email": "zhang.wei@example.com",
"phone": "13812345678",
"address": "北京市朝阳区幸福路1号",
"date_of_birth": "1985-05-20",
"created_at": "2025-07-15T12:30:00.123456"
}
响应(失败,客户不存在):
{
"detail": "客户未找到"
}
5.1.4 更新客户(PUT /customers/{id})
请求:
#kingbase>
curl -X PUT "http://localhost:8000/customers/1" \
-H "Content-Type: application/json" \
-d '{
"first_name": "张",
"last_name": "伟",
"email": "zhang.wei2@example.com",
"phone": "13987654321",
"address": "北京市海淀区幸福路2号",
"date_of_birth": "1985-05-20"
}'
响应(成功):
{
"customer_id": 1,
"first_name": "张",
"last_name": "伟",
"email": "zhang.wei2@example.com",
"phone": "13987654321",
"address": "北京市海淀区幸福路2号",
"date_of_birth": "1985-05-20",
"created_at": "2025-07-15T12:30:00.123456"
}
5.1.5 删除客户(DELETE /customers/{id})
请求:
#kingbase>
curl -X DELETE "http://localhost:8000/customers/1"
响应(成功):
{
"message": "客户已删除"
}
5.2 账户(Accounts)接口示例
5.2.1 创建账户(POST /accounts/)
请求:
#kingbase>
curl -X POST "http://localhost:8000/accounts/" \
-H "Content-Type: application/json" \
-d '{
"customer_id": 1,
"branch_id": 1,
"account_type": "savings",
"balance": 1000.00
}'
响应(成功):
{
"account_id": 1,
"customer_id": 1,
"branch_id": 1,
"account_type": "savings",
"balance": 1000.00,
"created_at": "2025-07-15T12:30:00.123456"
}
5.2.2 查询账户列表(GET /accounts/,支持分页和搜索)
请求(分页:第1页,每页10条,搜索“savings”):
#kingbase>
curl -X GET "http://localhost:8000/accounts/?page=1&per_page=10&search=savings"
响应(成功):
[
{
"account_id": 1,
"customer_id": 1,
"branch_id": 1,
"account_type": "savings",
"balance": 1000.00,
"created_at": "2025-07-15T12:30:00.123456"
}
]
5.3 交易(Transactions)接口示例
5.3.1 创建交易(POST /transactions/)
请求:
#kingbase>
curl -X POST "http://localhost:8000/transactions/" \
-H "Content-Type: application/json" \
-d '{
"account_id": 1,
"transaction_type": "deposit",
"amount": 500.00,
"description": "存款"
}'
响应(成功):
{
"transaction_id": 1,
"account_id": 1,
"transaction_type": "deposit",
"amount": 500.00,
"description": "存款",
"transaction_date": "2025-07-15T12:30:00.123456"
}
5.3.2 查询交易列表(GET /transactions/,支持分页和搜索)
请求(分页:第1页,每页10条,搜索“deposit”):
curl -X GET "http://localhost:8000/transactions/?page=1&per_page=10&search=deposit"
响应(成功):
[
{
"transaction_id": 1,
"account_id": 1,
"transaction_type": "deposit",
"amount": 500.00,
"description": "存款",
"transaction_date": "2025-07-15T12:30:00.123456"
}
]