# program02-Account **Repository Path**: lsq-eternally/program02-account ## Basic Information - **Project Name**: program02-Account - **Description**: 模拟银行账户转账业务,完成账户之间资金转移。使用事务管理来保证数据一致性。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-03-16 - **Last Updated**: 2025-09-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 一、项目需求 - 创建账户实体(Account),包含账户编号、账户名和余额。 - 使用 Spring Boot + JPA 实现账户的资金转账操作。 - 运用 Spring 事务管理,设置事务的隔离级别为 `REPEATABLE_READ`,确保多次读取结果一致。 - 提供两个 API 接口:查询账户余额和转账功能(保证事务的原子性)。 ## 二、项目核心功能 1. **账户余额查询**:通过账户编号查询对应账户的余额。 2. **资金转账**:实现从一个账户向另一个账户转账,确保转账过程的原子性。 ## 三、项目环境 - **项目名称**:Program02 - **编程语言**:Java - **项目类型**:Maven - **项目组**:com.example - **项目 SDK**:JDK 11 - **运行环境**:Spring Boot 2.7.0 - **数据库**:MySQL ## 四、项目依赖 - Spring Boot Starter Web - Spring Boot Starter Data JPA - Lombok - MySQL Connector - Spring Boot Starter Test - Spring Boot Maven Plugin ## 五、项目结构 ```plaintext Program02 ├─main │ ├─java │ │ └─com │ │ └─example │ │ │ BankTransferSystemApplication.java │ │ │ │ │ ├─controller │ │ │ AccountController.java │ │ │ │ │ ├─entity │ │ │ Account.java │ │ │ │ │ ├─repository │ │ │ AccountRepository.java │ │ │ │ │ └─service │ │ AccountService.java │ │ │ └─resources │ application.properties │ └─test └─java ``` ## 六、数据库设计 ### 账户表(Account) | 字段名 | 类型 | 是否为空 | 描述 | | ------------ | ------------ | -------- | -------- | | id | 主键(自增) | 否 | 账户编号 | | account_name | 字符串 | 否 | 账户名 | | balance | 数值类型 | 否 | 账户余额 | ## 七、接口调用 API ### 1. 查询账户余额 - **请求 URL**:`GET /api/accounts/{id}/balance` - 请求参数: - `id`:账户编号 - **响应数据**: ```json { "id": 1, "accountName": "张三", "balance": 1000.00 } ``` ### 2. 转账功能 - **请求 URL**:`POST /api/accounts/transfer` - **请求参数(JSON 格式)**: ```json { "fromAccountId": 1, "toAccountId": 2, "amount": 200.00 } ``` - **响应数据**: ```json { "message": "转账成功" } ``` ## 八、测试用例(使用 Postman) ### 1. 查询账户余额 - **请求方式**:GET - **请求 URL**:`http://localhost:8080/api/accounts/1/balance` - **预期响应**:返回对应账户的余额信息。 ### 2. 转账功能 - **请求方式**:POST - **请求 URL**:`http://localhost:8080/api/accounts/transfer` - **请求体(JSON 格式)**: ```json { "fromAccountId": 1, "toAccountId": 2, "amount": 200.00 } ``` - **预期响应**:返回 `{"message": "转账成功"}`,同时检查数据库中两个账户的余额是否正确更新。 ## 九、核心业务代码 ### 1. 账户实体类(Account.java) ```java @Data @Entity public class Account { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String accountName; private double balance; } ``` ### 2. 账户服务类(AccountService.java) ```java import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import java.util.Optional; @Service public class AccountService { @Transactional(isolation = Isolation.REPEATABLE_READ) public void transfer(Long fromAccountId, Long toAccountId, double amount) { // 实现转账逻辑 } public Optional findById(Long id) { // 根据账户编号查询账户信息 return null; } } ``` ### 3. 账户控制器类(AccountController.java) ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Optional; @RestController @RequestMapping("/api/accounts") public class AccountController { @Autowired private AccountService accountService; @GetMapping("/{id}/balance") public Account getBalance(@PathVariable Long id) { Optional account = accountService.findById(id); return account.orElse(null); } @PostMapping("/transfer") public String transfer(@RequestBody TransferRequest request) { accountService.transfer(request.getFromAccountId(), request.getToAccountId(), request.getAmount()); return "{\"message\": \"转账成功\"}"; } } class TransferRequest { private Long fromAccountId; private Long toAccountId; private double amount; // Getters and Setters } ```