From 849bc54d26356a291fe552599fba69c8b84b1064 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Tue, 11 Feb 2025 19:46:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(seata):=20=E6=B7=BB=E5=8A=A0=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E4=BA=8B=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../atguigu/account/SeataAccountMainApplication.java | 3 ++- .../account/service/impl/AccountServiceImpl.java | 5 ++++- .../atguigu/business/SeataBusinessMainApplication.java | 1 - .../business/service/impl/BusinessServiceImpl.java | 7 ++++--- .../com/atguigu/order/SeataOrderMainApplication.java | 3 ++- .../atguigu/order/service/impl/OrderServiceImpl.java | 10 ++++++---- .../atguigu/storage/SeataStorageMainApplication.java | 3 ++- .../storage/service/impl/StorageServiceImpl.java | 2 ++ 8 files changed, 22 insertions(+), 12 deletions(-) diff --git a/service/seata-account/src/main/java/com/atguigu/account/SeataAccountMainApplication.java b/service/seata-account/src/main/java/com/atguigu/account/SeataAccountMainApplication.java index a7f5b35..ea8ea90 100644 --- a/service/seata-account/src/main/java/com/atguigu/account/SeataAccountMainApplication.java +++ b/service/seata-account/src/main/java/com/atguigu/account/SeataAccountMainApplication.java @@ -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 diff --git a/service/seata-account/src/main/java/com/atguigu/account/service/impl/AccountServiceImpl.java b/service/seata-account/src/main/java/com/atguigu/account/service/impl/AccountServiceImpl.java index 82c70b0..ea0c2af 100644 --- a/service/seata-account/src/main/java/com/atguigu/account/service/impl/AccountServiceImpl.java +++ b/service/seata-account/src/main/java/com/atguigu/account/service/impl/AccountServiceImpl.java @@ -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); } } diff --git a/service/seata-business/src/main/java/com/atguigu/business/SeataBusinessMainApplication.java b/service/seata-business/src/main/java/com/atguigu/business/SeataBusinessMainApplication.java index 6f78f84..6fde23f 100644 --- a/service/seata-business/src/main/java/com/atguigu/business/SeataBusinessMainApplication.java +++ b/service/seata-business/src/main/java/com/atguigu/business/SeataBusinessMainApplication.java @@ -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 { diff --git a/service/seata-business/src/main/java/com/atguigu/business/service/impl/BusinessServiceImpl.java b/service/seata-business/src/main/java/com/atguigu/business/service/impl/BusinessServiceImpl.java index 99ec9b4..fbb9a4f 100644 --- a/service/seata-business/src/main/java/com/atguigu/business/service/impl/BusinessServiceImpl.java +++ b/service/seata-business/src/main/java/com/atguigu/business/service/impl/BusinessServiceImpl.java @@ -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. 创建订单 } } diff --git a/service/seata-order/src/main/java/com/atguigu/order/SeataOrderMainApplication.java b/service/seata-order/src/main/java/com/atguigu/order/SeataOrderMainApplication.java index 621eaf2..4effcbb 100644 --- a/service/seata-order/src/main/java/com/atguigu/order/SeataOrderMainApplication.java +++ b/service/seata-order/src/main/java/com/atguigu/order/SeataOrderMainApplication.java @@ -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 diff --git a/service/seata-order/src/main/java/com/atguigu/order/service/impl/OrderServiceImpl.java b/service/seata-order/src/main/java/com/atguigu/order/service/impl/OrderServiceImpl.java index de7bc2f..f32dadc 100644 --- a/service/seata-order/src/main/java/com/atguigu/order/service/impl/OrderServiceImpl.java +++ b/service/seata-order/src/main/java/com/atguigu/order/service/impl/OrderServiceImpl.java @@ -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; } } diff --git a/service/seata-storage/src/main/java/com/atguigu/storage/SeataStorageMainApplication.java b/service/seata-storage/src/main/java/com/atguigu/storage/SeataStorageMainApplication.java index e8a6541..0d30eba 100644 --- a/service/seata-storage/src/main/java/com/atguigu/storage/SeataStorageMainApplication.java +++ b/service/seata-storage/src/main/java/com/atguigu/storage/SeataStorageMainApplication.java @@ -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); } diff --git a/service/seata-storage/src/main/java/com/atguigu/storage/service/impl/StorageServiceImpl.java b/service/seata-storage/src/main/java/com/atguigu/storage/service/impl/StorageServiceImpl.java index 91e5f5c..347bcc7 100644 --- a/service/seata-storage/src/main/java/com/atguigu/storage/service/impl/StorageServiceImpl.java +++ b/service/seata-storage/src/main/java/com/atguigu/storage/service/impl/StorageServiceImpl.java @@ -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 {