feat(seata): 添加本地事务

This commit is contained in:
Bunny 2025-02-11 19:46:22 +08:00
parent b59f70afbe
commit 849bc54d26
8 changed files with 22 additions and 12 deletions

View File

@ -5,9 +5,10 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@MapperScan("com.atguigu.account.mapper")
@EnableDiscoveryClient
@SpringBootApplication

View File

@ -4,15 +4,18 @@ import com.atguigu.account.mapper.AccountTblMapper;
import com.atguigu.account.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
AccountTblMapper accountTblMapper;
@Override
public void debit(String userId, int money) {
// 扣减账户余额
accountTblMapper.debit(userId,money);
accountTblMapper.debit(userId, money);
}
}

View File

@ -4,7 +4,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class SeataBusinessMainApplication {

View File

@ -1,15 +1,16 @@
package com.atguigu.business.service.impl;
import com.alibaba.nacos.shaded.io.grpc.Grpc;
import com.atguigu.business.service.BusinessService;
import org.springframework.stereotype.Service;
@Grpc.TransportAttr
@Service
public class BusinessServiceImpl implements BusinessService {
@Override
public void purchase(String userId, String commodityCode, int orderCount) {
//TODO 1. 扣减库存
// TODO 1. 扣减库存
//TODO 2. 创建订单
// TODO 2. 创建订单
}
}

View File

@ -4,8 +4,9 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@MapperScan("com.atguigu.order.mapper")
@EnableDiscoveryClient
@SpringBootApplication

View File

@ -5,7 +5,9 @@ import com.atguigu.order.mapper.OrderTblMapper;
import com.atguigu.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class OrderServiceImpl implements OrderService {
@ -14,12 +16,12 @@ public class OrderServiceImpl implements OrderService {
@Override
public OrderTbl create(String userId, String commodityCode, int orderCount) {
//1计算订单价格
// 1计算订单价格
int orderMoney = calculate(commodityCode, orderCount);
//TODO 2扣减账户余额
// TODO 2扣减账户余额
//3保存订单
// 3保存订单
OrderTbl orderTbl = new OrderTbl();
orderTbl.setUserId(userId);
orderTbl.setCommodityCode(commodityCode);
@ -33,6 +35,6 @@ public class OrderServiceImpl implements OrderService {
// 计算价格
private int calculate(String commodityCode, int orderCount) {
return 9*orderCount;
return 9 * orderCount;
}
}

View File

@ -4,12 +4,13 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@MapperScan("com.atguigu.storage.mapper")
@EnableDiscoveryClient
@SpringBootApplication
public class SeataStorageMainApplication {
public static void main(String[] args) {
SpringApplication.run(SeataStorageMainApplication.class, args);
}

View File

@ -4,7 +4,9 @@ import com.atguigu.storage.mapper.StorageTblMapper;
import com.atguigu.storage.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class StorageServiceImpl implements StorageService {