# transaction-demo **Repository Path**: codefarmerj/transaction-demo ## Basic Information - **Project Name**: transaction-demo - **Description**: Spring Boot + MyBatis + 声明式事务 实现一个 转账功能,确保“扣款 + 加款”要么都成功,要么都失败。 - **Primary Language**: Java - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-13 - **Last Updated**: 2026-01-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Based on the code map provided, I'll create a comprehensive README.md for this Spring Boot transaction demo project: # Transaction Demo A Spring Boot application demonstrating transaction management with MyBatis, featuring REST API endpoints for account operations including money transfers with rollback capabilities. ## Overview This project demonstrates proper transaction management in Spring Boot applications using: - **Spring Boot** - Core application framework - **MyBatis** - SQL mapping framework for database operations - **Spring Transactions** - Declarative transaction management with rollback functionality ## Project Structure ### Main Components **Application Entry** - `TransactionDemoApplication.java` - Main Spring Boot application class **Controllers** - `TestController.java` - REST API endpoints for account operations - `BasicController.java` - Basic web controller demonstrating form handling and data binding - `PathVariableController.java` - Demonstrates URL path variable parsing **Service Layer** - `AccountService.java` - Business logic with transaction management - `transfer()` - Money transfer between accounts with rollback on failure - `queryBalance()` - Query account balance **Data Layer** - `AccountMapper.java` - MyBatis mapper interface - `AccountMapper.xml` - SQL mapping configuration - `Account.java` - Account entity model **Configuration** - `application.yml` - Spring Boot configuration - `pom.xml` - Maven dependencies ## Key Features ### Transaction Management The `AccountService` uses Spring's `@Transactional` annotation to ensure data integrity: ```java @Transactional(rollbackFor = Exception.class) public void transfer(Long fromId, Long toId, Double amount) ``` This ensures that if any exception occurs during the transfer process, all changes are rolled back to maintain database consistency. ### REST API Endpoints **Account Operations** - `GET /api/transfer?fromId={fromId}&toId={toId}&amount={amount}` - Transfer money between accounts - `GET /api/balance?accountId={accountId}` - Query account balance **Web Demos** - `GET /hello?name={name}` - Greeting endpoint with default parameter - `GET /user` - Returns user object as JSON - `POST /save_user` - Saves user object from form data - `GET /html` - Returns HTML page - `GET /user/{userId}/roles/{roleId}` - Path variable demonstration - `GET /javabeat/{regexp1}` - Regex path variable demonstration ## Setup Instructions ### Prerequisites - JDK 8 or higher - Maven 3.6+ - MySQL database ### Database Setup Execute the SQL script located in `src/main/resources/sql/testdb.sql` to create the necessary database tables and test data. ### Configuration Configure your database connection in `src/main/resources/application.yml`: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database username: your_username password: your_password ``` ### Build and Run ```bash # Build the project mvn clean package # Run the application java -jar target/transaction-demo.jar ``` The application will start on `http://localhost:8080` by default. ## Usage Examples ### Transfer Money ```bash curl "http://localhost:8080/api/transfer?fromId=1&toId=2&amount=100.0" ``` ### Check Balance ```bash curl "http://localhost:8080/api/balance?accountId=1" ``` ### Greeting ```bash curl "http://localhost:8080/hello?name=John" ``` ## Technology Stack - **Framework**: Spring Boot - **Database**: MySQL - **ORM**: MyBatis - **Build Tool**: Maven - **Web**: Spring MVC (Thymeleaf supported) ## License This project is open source and available for learning and development purposes.