转账:https://mp.weixin.qq.com/s/4P0HM4e59rSGwEb-DKwb_g
@Data
@TableName("user_account")
@AllArgsConstructor
@NoArgsConstructor
public class UserAccount implements Serializable {
private static final long serialVersionUID = 6909533252826367496L;
/**
* 账户编号
*/
@TableId
private String accountNo;
/**
* 账户名称
*/
private String accountName;
/**
* 账户余额
*/
private BigDecimal accountBalance;
}
public interface UserAccountService {
/**
* 跨库转账
* @param sourceAccountNo 转出账户
* @param targetSourceNo 转入账户
* @param bigDecimal 金额
*/
void transferAccounts(String sourceAccountNo, String targetSourceNo,BigDecimal transferAmount);
}
package com.tong.service.impl;
import com.tong.entity.UserAccount;
import com.tong.mapper1.UserAccountMapper1;
import com.tong.mapper2.UserAccountMapper2;
import com.tong.service.IUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
/**
* <p>
* 服务实现类
* </p>
*
* @author itbaizhan
* @since 05-13
*/
@Service
public class UserAccountServiceImpl implements IUserAccountService {
@Autowired
private UserAccountMapper1 userAccountMapper1;
@Autowired
private UserAccountMapper2 userAccountMapper2;
/**
* 跨库转账
* @param sourceAccountNo 源账户
* @param targetSourceNo 目标账户
* @param bigDecimal 金额
*/
@Transactional
@Override
public void transofer(String sourceAccountNo, String targetSourceNo, BigDecimal bigDecimal) {
// 1. 查询原账户
UserAccount sourceUserAccount = userAccountMapper1.selectById(sourceAccountNo);
// 2. 查询目标账户
UserAccount targetUserAccount = userAccountMapper2.selectById(targetSourceNo);
// 3. 判断转入账户和转出账户是否为空
if (sourceAccountNo != null && targetUserAccount != null){
// 4. 判断转出账户是否余额不足
if (sourceUserAccount.getAccountBalance().compareTo(bigDecimal) < 0){
throw new RuntimeException("余额不足");
}
// 5.更新金额
sourceUserAccount.setAccountBalance(sourceUserAccount.getAccountBalance().subtract(bigDecimal));
// 6.张三账户减金额
userAccountMapper1.updateById(sourceUserAccount);
System.out.println(10/0);
// 7.更新金额
targetUserAccount.setAccountBalance(targetUserAccount.getAccountBalance().add(bigDecimal));
// 8.张三账户减金额
userAccountMapper2.updateById(targetUserAccount);
}
}
}
```cpp
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for cancel_log
-- ----------------------------
DROP TABLE IF EXISTS `cancel_log`;
CREATE TABLE `cancel_log` (
`tx_no` varchar(64) CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci NOT NULL COMMENT '全局事务编号',
`create_time` datetime(0) NULL DEFAULT NULL
COMMENT '创建时间',
PRIMARY KEY (`tx_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = 'Cancel
阶段执行的日志记录' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of cancel_log
-- ----------------------------
-- ----------------------------
-- Table structure for confirm_log
-- ----------------------------
DROP TABLE IF EXISTS `confirm_log`;
CREATE TABLE `confirm_log` (
`tx_no` varchar(64) CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci NOT NULL COMMENT '全局事务编号',
`create_time` datetime(0) NULL DEFAULT NULL
COMMENT '创建时间',
PRIMARY KEY (`tx_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = 'Confirm
阶段执行的日志记录' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of confirm_log
-- ----------------------------
-- ----------------------------
-- Table structure for try_log
-- ----------------------------
DROP TABLE IF EXISTS `try_log`;
CREATE TABLE `try_log` (
`tx_no` varchar(64) CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci NOT NULL COMMENT '全局事务编号',
`create_time` datetime(0) NULL DEFAULT
CURRENT_TIMESTAMP(0) COMMENT '创建时间'
,
PRIMARY KEY (`tx_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = 'Try阶段
执行的日志记录' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of try_log
-- ----------------------------
-- ----------------------------
-- Table structure for user_account
-- ----------------------------
DROP TABLE IF EXISTS `user_account`;
CREATE TABLE `user_account` (
`account_no` varchar(64) CHARACTER SET
utf8mb4 COLLATE utf8mb4_general_ci NOT NULL
COMMENT '账户编号',
`account_name` varchar(50) CHARACTER SET
utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT
NULL COMMENT '账户名称',
`account_balance` decimal(10, 2) NULL DEFAULT
NULL COMMENT '账户余额',
`transfer_amount` decimal(10, 2) NULL DEFAULT
NULL COMMENT '转账金额',
PRIMARY KEY (`account_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = '账户信
息' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user_account
-- ----------------------------
INSERT INTO `user_account` VALUES ('1001',
'张三', 10000.00, 0.00);
SET FOREIGN_KEY_CHECKS = 1;
在192.168.66.100服务器的MySQL命令行执行如下命令创建转入银行数据库和数据表。
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
– Table structure for cancel_log
DROP TABLE IF EXISTS cancel_log
;
CREATE TABLE cancel_log
(
tx_no
varchar(64) CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci NOT NULL COMMENT ‘全局事务编号’,
create_time
datetime(0) NULL DEFAULT NULL
COMMENT ‘创建时间’,
PRIMARY KEY (tx_no
) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = ‘Cancel阶段执行的日志记录’ ROW_FORMAT = Dynamic;
– Records of cancel_log
– Table structure for confirm_log
DROP TABLE IF EXISTS confirm_log
;
CREATE TABLE confirm_log
(
tx_no
varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT ‘全局事务编号’,
create_time
datetime(0) NULL DEFAULT NULL
COMMENT ‘创建时间’,
PRIMARY KEY (tx_no
) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = ‘Confirm
阶段执行的日志记录’ ROW_FORMAT = Dynamic;
– Records of confirm_log
– Table structure for try_log
DROP TABLE IF EXISTS try_log
;
CREATE TABLE try_log
(
tx_no
varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT ‘全
局事务编号’,
create_time
datetime(0) NULL DEFAULT NULL
COMMENT ‘创建时间’,
PRIMARY KEY (tx_no
) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = ‘Try阶段
执行的日志记录’ ROW_FORMAT = Dynamic;
– Records of try_log
– Table structure for user_account
DROP TABLE IF EXISTS user_account
;
CREATE TABLE user_account
(
account_no
varchar(64) CHARACTER SET
utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT ‘账户编号’,
account_name
varchar(50) CHARACTER SET
utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT
NULL COMMENT ‘账户名称’,
account_balance
decimal(10, 2) NULL DEFAULT
NULL COMMENT ‘账户余额’,
transfer_amount
decimal(10, 2) NULL DEFAULT
NULL COMMENT ‘转账金额’,
PRIMARY KEY (account_no
) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = ‘账户信息’ ROW_FORMAT = Dynamic;
– Records of user_account
INSERT INTO user_account
VALUES (‘1002’,‘李四’, 10000.00, 0.00);
SET FOREIGN_KEY_CHECKS = 1;