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、事务处理机制

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

十、系统运行

总结:

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

相关推荐
哦虎!31 分钟前
【JavaEE】网络初识
java
for_ever_love__1 小时前
MySQL ORDER BY 排序优化:从 Using filesort 到索引有序扫描(含双路 / 单路排序原理)
java·mysql·优化·存储·by·order·filesort
程序猿追2 小时前
kotlin-retry 接入鸿蒙后,怎样确认它真的会停止重试
开发语言·kotlin·harmonyos
我是人✓2 小时前
Java文件上传三方式对比:从传统到SpringBoot
java·开发语言
智能直播2 小时前
网络RTMP拉流不卡顿、声音不同步,一文讲透缓冲区、时间戳
开发语言·ffmpeg·php·音视频·视频编解码
欣栀♡2 小时前
JVM 频繁 Full GC 先看日志括号里的 cause,jstat 的 FGC 在 JDK 8 上会骗人
java·jvm
IT研究室2 小时前
最新计算机毕业设计选题推荐-基于spring boot的葡萄酒文化宣传网站-网站-文档指导-Java-springboot
java·spring boot·课程设计
慧都小项2 小时前
MyEclipse 中创建和测试 JAX-RS REST 接口:从项目配置到 REST Explorer
java·restful·myeclipse·jax-rs
Wang's Blog2 小时前
Java 项目实战: 交友项目 - 入门案例:编写 Dubbo 服务提供者
java·dubbo·交友
墨云安全2 小时前
内网资产无法暴露到公网,怎么做安全检测?2026 年内网渗透测试的落地路径
开发语言·安全·网络安全·渗透测试·php