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

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

十、系统运行

总结:

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

相关推荐
Rick19935 小时前
Java 接口高并发优化方案
java·性能优化·高并发
瑶总迷弟5 小时前
Python入门第7章:用户输入和 while 、for循环
开发语言·python·microsoft
不愿透露姓名的大鹏5 小时前
MySQL慢查询日志实战优化指南
linux·服务器·数据库·mysql·adb
qq_628515765 小时前
Java实现pdf导出
java·vue.js·react.js·pdf
Allen_LVyingbo5 小时前
量子计算Dirac Notation基本教学—从零基础到读懂量子信息论文(上)
开发语言·数据结构·架构·健康医疗·量子计算
无巧不成书02185 小时前
Java变量初始化全攻略:2026最新规范+新手避坑实战
java·开发语言·java基础·java变量初始化·java语法规范·var关键字
爱分享的阿Q5 小时前
技术饱和度视角下的编程语言选择:一场关于供需博弈的深度思考
java·python·go
Highcharts.js5 小时前
企业级可视化生态系统|关于Highcharts集成的前端框架、后端编程语言与生态
开发语言·javascript·python·前端框架·编辑器·编程语言·highcharts
我头发多我先学5 小时前
C++ STL list 原理到模拟实现
开发语言·c++·list