mybatis事务-支付流程笔记-含建表代码

相关表:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for goods_info
-- ----------------------------
DROP TABLE IF EXISTS `goods_info`;
CREATE TABLE `goods_info`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
  `goods_stock` int(0) NULL DEFAULT NULL COMMENT '库存',
  `goods_price` bigint(0) NULL DEFAULT NULL COMMENT '价格',
  `goods_unit` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '1包2箱',
  `version` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `create_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
  `update_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 30 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of goods_info
-- ----------------------------
INSERT INTO `goods_info` VALUES (10, 'D手机', 98, 25, '1', '2', '2022-08-10 11:12:37', '2022-08-15 10:19:08');
INSERT INTO `goods_info` VALUES (20, 'BB机', 100, 25, '1', '1', '2022-08-10 11:12:48', '2022-08-15 10:19:08');
INSERT INTO `goods_info` VALUES (30, 'CC笔记本', 100, 25, '1', '1', '2022-08-10 11:12:50', '2022-08-15 10:19:08');

-- ----------------------------
-- Table structure for goods_info_log
-- ----------------------------
DROP TABLE IF EXISTS `goods_info_log`;
CREATE TABLE `goods_info_log`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `user_id` int(0) NULL DEFAULT NULL COMMENT '用户id-冗余-为了方便查询',
  `order_id` int(0) NULL DEFAULT NULL COMMENT '订单编号',
  `old_stock` int(0) NULL DEFAULT NULL COMMENT '下单前数量',
  `goods_num` int(0) NULL DEFAULT NULL COMMENT '订单下单数量',
  `new_stock` int(0) NULL DEFAULT NULL COMMENT '下单后数量',
  `create_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间(出问题检查用)',
  `update_time` timestamp(0) NULL DEFAULT NULL COMMENT '修改时间(出问题检查用)',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of goods_info_log
-- ----------------------------
INSERT INTO `goods_info_log` VALUES (1, 200, 3, 100, 2, 98, '2022-08-10 11:37:17', NULL);
INSERT INTO `goods_info_log` VALUES (2, 200, 6, 98, 10, 88, '2022-08-15 10:04:30', NULL);

-- ----------------------------
-- Table structure for order_info
-- ----------------------------
DROP TABLE IF EXISTS `order_info`;
CREATE TABLE `order_info`  (
  `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '订单id',
  `user_id` int(0) NULL DEFAULT NULL COMMENT '用户id',
  `goods_id` int(0) NULL DEFAULT NULL COMMENT '商品id',
  `goods_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称-是否必须加?',
  `goods_num` int(0) NULL DEFAULT NULL COMMENT '商品数量',
  `goods_unit` char(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品单位(个包箱)',
  `goods_price` bigint(0) NULL DEFAULT NULL,
  `total_price` bigint(0) NULL DEFAULT NULL,
  `old_account` bigint(0) NULL DEFAULT NULL COMMENT '用户账户原金额',
  `new_account` bigint(0) NULL DEFAULT NULL COMMENT '用户账户新金额',
  `order_status` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '订单状态(0-已下单1-已付款9-已退款)同学讨论',
  `create_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '下单时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of order_info
-- ----------------------------
INSERT INTO `order_info` VALUES (3, 200, 10, 'AA手机', 2, '', 25, 50, NULL, NULL, '1', '2022-08-15 09:48:25');

-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `age` int(0) NULL DEFAULT NULL,
  `account` bigint(0) NULL DEFAULT NULL,
  `version` int(0) NULL DEFAULT NULL,
  `last_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `hashed_password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 501 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, '李新3', 20, 1000, 6, 'aa1', '888888');
INSERT INTO `users` VALUES (3, '王新', 22, 1000, 6, 'aa2', '888888');
INSERT INTO `users` VALUES (4, '李三', 27, 1000, 6, 'aa3', '888888');
INSERT INTO `users` VALUES (200, '李33', 22, 1000, 6, '李mike99', '999999');
INSERT INTO `users` VALUES (312, 'abc', 20, 1000, 6, 'abc_def', '999999');
INSERT INTO `users` VALUES (322, '李四娘', 0, 1000, 6, NULL, '999999');
INSERT INTO `users` VALUES (500, '李500', 55, 1000, 6, '李_555', '999999');
INSERT INTO `users` VALUES (501, '李666', 0, 1000, 6, NULL, '999999');

SET FOREIGN_KEY_CHECKS = 1;

 

*、JDBC+scanner。
*、完成示例:转账(最简单):张三给李四转账200或2000元。
user_account(id,name,account)
id name account
10 张三 1000
20 李四 1000
要求:最低余额不能不少于0
*、要用try..catch..finally

*、自动提交,即没有事务: conn.setAutoCommit(true)
*、关闭自动提交,即开启事务: conn.setAutoCommit(false)

*、执行多次SQL,然后提交事务,如果出现异常,在catch语句中:回滚事务。

*、在finally中:关闭各种资源
================
流程:
*、请输入来源账户:10
*、请输入目标账户:20
*、请输入要转账的金额:200或2000或其它
(回车执行)
开启事务
update user_account set ... where id=10;
update user_account set ... where id=20;
提交事务或回滚事务
关闭资源

=====实际支付流程==(课堂笔记),然后用户单击“支付”以后的后台执行流程。

用户单击支付(WX):
1、(前置条件):公司运维在经过领导授权的前提下,会给研发人员相关的商户信息,包括KEY。
2、代码:
1、调用WX或ZFB的接口 (百度:文档),要求:面试时要会讲过程(看资料,步骤整理到思维导图)
2、如果不成功,通常会重试3次,3次以后还不成功,则提示:支付失败,请稍后查询。 (面试时讲)
3、如果成功,则往下面3张表中插入数据:
/*
select * from users;


select * from goods_info;
select * from order_info;
select * from goods_info_log;
select *from user_info;

 


-- 案例:200号用户下单,购买手机(10)和笔记本(11)的全流程:

--假设用户的编号是:200 (生成订单:无事务) 第1条SQL
insert into order_info(user_id,goods_id,goods_name,goods_num,goods_price,goods_unit,order_status)
values(200,10,'AA手机',2,25,'部','1');

select * from order_info;

--为了运营能够查询用户下单那一个时刻的基本信息,此字段要保留(运营)
--可以为财务审计留下数据,提高数据的可信度。

-- 先查询库存和版本号(多个用户都得到相同的数据)
--开启事务
-- 修改库存(加锁)
select * from goods_info where id=10; (一个商品查一次) 第2条SQL
还买了另一个商品(下再的步骤都相同)
select * from goods_info where id=11; (一个商品查一次) 第3条SQL

现在能拿到 id=10 stock=100 version=1
select * from users where id=200; (一个用户一次) 第4条SQL
现在能拿到 id=200 account=1000 version=6

-- update goods_info set goods_stock=100-2 where id=10 ;
update goods_info set goods_stock=100-2,version=version+1 where id=10 and version=1; (一个商品改一次) 第5条SQL
update goods_info set goods_stock=100-3,version=version+1 where id=20 and version=1; (一个商品改一次) 第6条SQL


--修改用户余额(一个用户一次) 第7条SQL
-- update goods_info set goods_stock=100-2 where id=10 ;
update users set account=1000-25,version=version+1 where id=10 and version=1;

-- 增加库存日志(一个商品记一次) 第8条SQL
insert into goods_info_log(order_id,user_id,goods_id,old_stock,goods_num,new_stock)
values(2,200,10,100,2,98);
insert into goods_info_log(order_id,user_id,goods_id,old_stock,goods_num,new_stock)
values(2,200,11,100,3,98);

 

乐观锁:
---假设另一个用户
-- update goods_info set goods_stock=100+2000,version=version+1 where id=10 and version=1;


--提交事务
--出错则回滚事务
--关闭资源

-- 在order_info中,商品名称:goods_name要还是不要?还是必须要,还是可以不要?

 

 

 


两种:
1、是用标记的Mybatis方式。
2、是用自定义SQL
总结:这个场景,用哪种方案更适合?
*/

posted @ 2022-08-15 10:38  xiaoyongdata  阅读(87)  评论(0编辑  收藏  举报