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

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

十、系统运行

总结:

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

相关推荐
ERROR:992 小时前
野路子:把海量文档一次性转换成多个PPT
开发语言·人工智能·c#
CodeCraft Studio2 小时前
国产化Excel开发组件Spire.XLS教程:以Python编程方式在Excel中高亮重复值
开发语言·python·excel·spire.xls·excel自动化·excel高亮重复值·python处理excel
Han.miracle2 小时前
Spring WebMVC入门实战:从概念到连接建立全解析
java·spring boot·spring·springmvc
Savvy..2 小时前
RabbitMQ
java·rabbitmq·java-rabbitmq
TT哇2 小时前
Spring Boot 项目中关于文件上传与访问的配置方案
java·spring boot·后端
峥嵘life2 小时前
Android16 EDLA 认证测试BTS过程介绍
android·java·linux
木千2 小时前
Qt中关于QTableWidget成员函数selectedItems和itemAt(QPoint)无法获取无数据Item的处理方式
开发语言·qt
残花月伴2 小时前
天机学堂-day5(互动问答)
java·spring boot·后端
小鸡吃米…2 小时前
Python - 命令行参数
开发语言·python