JAVA与MySQL实现银行管理系统

一、项目描述

基于Java与MySQL的银行管理系统,实现了银行核心业务功能,包括账户管理、存款取款、转账交易、余额查询等。系统采用分层架构设计,具有良好的扩展性和维护性。

二、技术栈

后端语言: Java 17或21

数据库: MySQL 8.0.28或更高版本

构建工具: Maven3.9.9或更高版本

三、系统特点

  1. 1、完整的银行业务流程实现
  2. 2、事务处理和数据一致性保障
  3. 3、清晰的系统架构和代码规范
  4. 4、完善的错误处理和日志记录

四、系统架构设计

  1. 1、 表示层 (Presentation Layer) ↓
    1. 2、 业务逻辑层 (Business Logic Layer) ↓
      1. 3、 数据访问层 (Data Access Layer) ↓

        1. 4、 数据库层 (Database Layer)
      2. org.hlx ├── pojo / # 数据模型层

      3. ├── dao / # 数据访问层

      4. ├── service / # 业务逻辑层

      5. ├── util / # 数据库连接层

      6. ├── TestBank.java # 主程序入口

五、数据库设计

数据库表结构

(1) 账户表 (users)

|-------------|---------------|----------------------------|------------|
| 字段名 | 类型 | 约束 | 说明 |
| id | INT | AUTO_INCREMENT PRIMARY KEY | 账号,主键 |
| username | VARCHAR(50) | NOT NULL | 账户姓名 |
| password | VARCHAR(50) | NOT NULL | 账户密码 |
| balance | DECIMAL(15,2) | DEFAULT 0.00 | 账户余额 |
| createdTime | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 开户时间 |
| updateTime | TIMESTAMP | DEFAULT | 修改时间 |

(2) 交易记录表 (transactions)

|------------------|---------------|-----------------------------------------------------------------|------------|
| 字段名 | 类型 | 约束 | 说明 |
| id | INT | AUTO_INCREMENT PRIMARY KEY | 交易ID |
| user_id | INT | FOREIGN KEY | 关联账号 |
| type | ENUM | NOT NULL ('deposit', 'withdraw', 'transfer_in', 'transfer_out') | 交易类型 |
| amount | DECIMAL(15,2) | NULL | 交易金额 |
| balace_after | DECIMAL(15,2) | NULL | 当前余额 |
| description | VARCHAR(255) | NULL | 交易描述 |
| transaction_time | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | 交易时间 |

实体关系图

users(1) ←→ (N) transactions ↑ (主键) (外键)

六、核心功能模块

1、开户功能

2、登录功能

3、交易处理模块 (存款,取款)

4、转账业务模块

5、交易记录模块

七、实体类设计

1、User类

java 复制代码
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author : HLX
 * @ClassName :User
 * @date : 2025/12/10 11:11
 * @Version :1.0
 * @Description: TODO
 * @modyified By :
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String username;
    private String password;
    private BigDecimal balance;
    private Date createdTime;
    private Date updateTime;



    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User(String username, BigDecimal balance, Date createdTime) {
        this.username = username;
        this.balance = balance;
        this.createdTime = createdTime;
    }

    public User(int id, BigDecimal balance) {
        this.id = id;
        this.balance = balance;
    }
}

2、Transactions类

java 复制代码
package org.hlx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author : HLX
 * @ClassName :Transactions
 * @date : 2025/12/10 11:14
 * @Version :1.0
 * @Description: TODO
 * @modyified By :
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Transactions {
    private int id;
    private int userId;  //对应于数据库表的字段 user_id
    private String type;
    private BigDecimal amount;
    private BigDecimal balanceAfter; //balance_after
    private String description;
    private Date transactionTime;
    private User user;

    public Transactions(int userId, String type, BigDecimal amount, BigDecimal balanceAfter, String description, Date transactionTime) {
        this.userId = userId;
        this.type = type;
        this.amount = amount;
        this.balanceAfter = balanceAfter;
        this.description = description;
        this.transactionTime = transactionTime;
    }

    public Transactions(int userId, String type, BigDecimal amount, BigDecimal balanceAfter) {
        this.userId = userId;
        this.type = type;
        this.amount = amount;
        this.balanceAfter = balanceAfter;
        this.description = description;
    }

    public Transactions(int userId, String type, BigDecimal amount, String description) {
        this.userId = userId;
        this.type = type;
        this.amount = amount;
        this.description = description;
    }

    public Transactions(int userId, String type, BigDecimal amount, BigDecimal balanceAfter, String description) {
        this.userId = userId;
        this.type = type;
        this.amount = amount;
        this.balanceAfter = balanceAfter;
        this.description = description;
    }
}

八、数据访问层设计

1、UsersDao接口

java 复制代码
package org.hlx.dao;

import org.hlx.pojo.Transactions;
import org.hlx.pojo.User;

import java.sql.SQLException;

/**
 * @author : HLX
 * @ClassName :UserDao
 * @date : 2025/12/10 11:19
 * @Version :1.0
 * @Description: TODO
 * @modyified By :
 */
public interface UserDao {

    /**
     * 添加用户信息
     * @param user
     * @return
     * @throws Exception
     */
    int addUser(User user) throws Exception;

    /**
     * 根据用户名和密码查询用户信息
     * @param user
     * @return
     * @throws Exception
     */
    User loginUser(User user) throws Exception;

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     * @throws Exception
     */
    User getUserById(int id) throws Exception;

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     * @throws Exception
     */
    User getUserByName(String username) throws Exception;

    /**
     * 存款操作
     * @param user
     * @return
     * @throws Exception
     */
    int deposit(User user) throws Exception;

    /**
     * 取款操作
     * @param user
     * @return
     * @throws Exception
     */
    int withdraw(User user) throws Exception;


    /**
     * 转账操作
     * @param user
     * @return
     * @throws Exception
     */
    int transfer(User user,String uname) throws Exception;

    /**
     * 删除用户和交易记录
     * @param id
     * @return
     * @throws Exception
     */
    int deleteTransAndUser(int id) throws Exception;

}

2、TransactionsDao接口

java 复制代码
package org.hlx.dao;

import org.hlx.pojo.Transactions;
import org.hlx.pojo.User;

import java.util.List;

/**
 * @author : HLX
 * @ClassName :TransactionsDao
 * @date : 2025/12/10 11:35
 * @Version :1.0
 * @Description: TODO
 * @modyified By :
 */
public interface TransactionsDao {


    /**
     * 保存交易记录
     * @param transactions
     * @return
     */
    int saveTransaction(Transactions transactions) throws Exception;

    /**
     * 根据用户id查询交易记录
     * @param userId
     * @return
     * @throws Exception
     */
    List<Transactions> getTransactionByUserId(int userId) throws Exception;
}

数据层实现接口的类(impl包省略)

九、关键技术实现

1、数据库连接管理

采用线程连接池管理数据库连接,确保连接的高效复用,引入连接池提升性能。

2、事务处理机制

在取款,转账等复杂业务中实现事务一致性保证

十、系统运行

总结:

系统采用分层架构,各模块职责明确,具有良好的可维护性和扩展性。通过合理的数据库设计和事务处理机制,确保了数据的一致性和系统的稳定性。

相关推荐
1024奇点8 小时前
【仓颉语言入门 · 第二篇 第 22 课】
开发语言·ide·开源
Warren2Lynch9 小时前
C4 模型详解:利用 Visual Paradigm 可视化软件架构的综合指南
开发语言·uml
天河归来9 小时前
第三方接口不稳定,如何让核心任务继续完成
java·大数据
Knight_AL9 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言
FRing0310 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
幽灵诶10 小时前
Mysql(基于测试的学习)
数据库·mysql
by2099910 小时前
C++ 智能指针的所有权与使用
开发语言·c++·经验分享
砚底藏山河11 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融
IT召唤狮11 小时前
开源企业级零信任项目Pangolin,零信任能挡住PHP核弹漏洞吗?
开发语言·开源·php
知已难寻11 小时前
03 · OpenStack的计算管理 Nova
运维·服务器·开发语言·云原生·云计算·openstack·开源软件