MySQL - [19] 关于个人负债为主题的数据库设计

HOUHUILIN / 2024-08-29 / 原文

天生我材必有用,千金散尽还复来。

 

一、开发环境

序号 名称 版本描述
1 JDK 1.8.0_401
2 数据库 MySQL Community Server 8.0.37
3 数据库客户端 DBeaver 21.0.2.202104042040
4 开发工具 IntelliJ IDEA Community Edition 2023.3.4

 

 

二、数据库设计

2.1、数据库(credit_card_db)

CREATE DATABASE IF NOT EXISTS credit_card_db;
USE credit_card_db;

 

2.2、用户表(Users)

存储用户的基本信息

序号 字段名称 字段类型 字段约束 字段注释 备注
1 user_id INT PRIMARY KEY 用户id 主键自增
2 username VARCHAR(255) NOT NULL 用户名  
3 password VARCHAR(255) NOT NULL 密码  
4 email VARCHAR(255) NOT NULL 邮箱  
5 regiseter_time TIMESTAMP   注册时间  
6 login_time TIMESTAMP   登录时间  
7 is_deleted INT   逻辑删除标志 0:删除,1:未删除
8          
CREATE TABLE Users (
    user_id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL
);

 

2.3、账户表(Accounts)

存储用户的各个账户信息,包括账户类型和名称

序号 字段名称 字段类型 字段约束 字段注释 备注
1 account_id INT PRIMARY KEY 账户id  
2 user_id INT   用户id  
3 account_type_id INT NOT NULL 账户类型id  
4 account_name VARCHAR(255)   账户名称  
5          
CREATE TABLE Accounts (
    account_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    account_type ENUM('信用卡', '花呗', '京东金条', '京东白条') NOT NULL,
    account_name VARCHAR(255),
    FOREIGN KEY (user_id) REFERENCES Users(user_id)
);

 

2.4、额度表(Limits)

存储每个账户的额度信息,包括总额度、固定额度和临时额度。

序号 字段名称 字段类型 字段约束 字段注释 备注
1 limit_id INT PRIMARY KEY 额度id AUTO_INCREMENT
2 account_id INT   账户id  
3 total_limit DECIMAL(15,2)   总额度  
4 fixed_limit DECIMAL(15,2)      
5 temporary_limit DECIMAL(15,2)   临时额度  
6 last_updated TIMESTAMP      
7          
CREATE TABLE Limits (
    limit_id INT AUTO_INCREMENT PRIMARY KEY,
    account_id INT,
    total_limit DECIMAL(15, 2) NOT NULL,
    fixed_limit DECIMAL(15, 2) NOT NULL,
    temporary_limit DECIMAL(15, 2),
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (account_id) REFERENCES Accounts(account_id)
);

 

2.5、交易表(Transactions)

记录所有的交易信息,包括消费、还款和分期。

序号 字段名称 字段类型 字段约束 字段注释 备注
1 transaction_id INT PRIMARY KEY    
2 account_id INT   账户id  
3 amount DECIMAL(15, 2)   交易金额  
4 transaction_type INT   交易类型 1:消费;2:还款;3:消费
5 transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP 交易日期  
6 status INT DEFAULT 0 交易状态 0:待处理;1:已完成
7          
CREATE TABLE Transactions (
    transaction_id INT AUTO_INCREMENT PRIMARY KEY,
    account_id INT,
    amount DECIMAL(15, 2) NOT NULL,
    transaction_type INT NOT NULL,
    transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    status INT NOT NULL,
    FOREIGN KEY (account_id) REFERENCES Accounts(account_id)
);

2.6、账单表(Bills)

记录每个账户的账单信息,包括账单日期、总金额、待还金额和已还金额

序号 字段名称 字段类型 字段约束 字段注释 备注
1 bill_id INT PRIMARY KEY  账单id  
2 account_id INT    账号id  
3 bill_date DATE NOT NULL  账单日期  
4 total_amount DECIMAL(15, 2) NOT NULL  账单总额  
5 amount_due DECIMAL(15, 2) NOT NULL  待还金额  
6 amount_paid DECIMAL(15, 2)    已还金额  
7 status INT NOT NULL  账单状态 0:未支付;1:已支付
8          
CREATE TABLE Bills (
    bill_id INT AUTO_INCREMENT PRIMARY KEY,
    account_id INT,
    bill_date DATE NOT NULL,
    total_amount DECIMAL(15, 2) NOT NULL,
    amount_due DECIMAL(15, 2) NOT NULL,
    amount_paid DECIMAL(15, 2),
    status INT NOT NULL,
    FOREIGN KEY (account_id) REFERENCES Accounts(account_id)
);

 

2.7、分期表(Installments)

记录分期交易的详细信息,包括每期的金额和状态

序号 字段名称 字段类型 字段约束 字段注释 备注
1 installment_id INT PRIMARY KEY 分期id  
2 transaction_id INT   交易id  
3 installment_amount DECIMAL(15, 2) NOT NULL 分期金额  
4 due_date DATE NOT NULL 分期日期  
5 per_installment_amount DECIMAL(15, 2) NOT NULL 每期金额  
6 per_installment_interest DECIMAL(15, 2) NOT NULL 每期利息  
5 status INT NOT NULL 状态 0:未支付;1:已支付
6          
CREATE TABLE Installments (
    installment_id INT AUTO_INCREMENT PRIMARY KEY,
    transaction_id INT,
    installment_amount DECIMAL(15, 2) NOT NULL,
    due_date DATE NOT NULL,
    status INT NOT NULL,
    FOREIGN KEY (transaction_id) REFERENCES Transactions(transaction_id)
);

 

2.8、账户类型表(AccountsType)

序号 字段名称 字段类型 字段约束 字段注释 备注
1 account_type_id INT PRIMARY KEY 账户类型id  
2 account_type VARCHAR(255)   账户类型  
3          
CREATE TABLE AccountsType(
    account_type_id INT PRIMARY KEY,
    account_type_name VARCHAR(255)
)

 

 

三、Springboot & MybatisPlus

3.1、添加依赖(pom.xml)

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Boot Starter Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- MySQL Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- MyBatis Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.2</version>
    </dependency>

    <!-- Lombok (可选,用于简化代码) -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </version>
</dependencies>

3.2、配置数据库连接(application.yml)

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/credit_card_db?useSSL=false&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

 

3.3、创建实体类

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

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

@Data
@TableName("users")
public class User {
    @TableId(type = IdType.AUTO)
    private Long userId;
    private String username;
    private String email;
    private String password;
}

// 其他实体类(Accounts, Limits, Transactions, Bills, Installments)类似创建

 

3.4、创建Mapper接口

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

// 其他Mapper接口(AccountMapper, LimitMapper, TransactionMapper, BillMapper, InstallmentMapper)类似创建

 

3.5、创建Service接口和实现类

import com.baomidou.mybatisplus.extension.service.IService;

public interface UserService extends IService<User> {
}

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

// 其他Service接口和实现类(AccountService, LimitService, TransactionService, BillService, InstallmentService)类似创建

 

3.6、创建Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getById(id);
    }

    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);
    }

    // 其他业务接口(AccountController, LimitController, TransactionController, BillController, InstallmentController)类似创建
}

 

3.7、运行项目

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CreditCardApplication {
    public static void main(String[] args) {
        SpringApplication.run(CreditCardApplication.class, args);
    }
}

 

TODO:未完待续...

 

 

 

— 业精于勤荒于嬉,行成于思毁于随 —