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

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

十、系统运行

总结:

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

相关推荐
疯狂的喵12 小时前
C++编译期多态实现
开发语言·c++·算法
2301_7657031413 小时前
C++中的协程编程
开发语言·c++·算法
m0_7487080513 小时前
实时数据压缩库
开发语言·c++·算法
invicinble13 小时前
对于Mysql深入理解
数据库·mysql
lly20240613 小时前
jQuery Mobile 表格
开发语言
惊讶的猫13 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
jmxwzy13 小时前
Spring全家桶
java·spring·rpc
Halo_tjn14 小时前
基于封装的专项 知识点
java·前端·python·算法
m0_7482331714 小时前
30秒掌握C++核心精髓
开发语言·c++
Fleshy数模14 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言