Переглянути джерело

feat:编写seataStorageMicroService服务相关的代码

yang yi 1 рік тому
батько
коміт
bce8c51c17

+ 1 - 1
pom.xml

@@ -19,7 +19,7 @@
         <maven.compiler.source>17</maven.compiler.source>
         <maven.compiler.target>17</maven.compiler.target>
         <spring.boot.version>2.7.17</spring.boot.version>
-        <spring.cloud.version>2021.0.6</spring.cloud.version>
+        <spring.cloud.version>2021.0.5</spring.cloud.version>
         <spring.cloud.alibaba.version>2021.0.5.0</spring.cloud.alibaba.version>
         <mysql.connector.version>8.3.0</mysql.connector.version>
         <log4j.version>1.2.17</log4j.version>

+ 14 - 4
seata_storage_micro_service/pom.xml

@@ -20,6 +20,20 @@
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>com.alibaba</groupId>
+                    <artifactId>druid</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>com.alibaba</groupId>
+                    <artifactId>fastjson</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>com.github.ben-manes.caffeine</groupId>
+                    <artifactId>caffeine</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <!--nacos服务发现的依赖-->
         <dependency>
@@ -40,10 +54,6 @@
             <groupId>com.mysql</groupId>
             <artifactId>mysql-connector-j</artifactId>
         </dependency>
-        <dependency>
-            <groupId>com.baomidou</groupId>
-            <artifactId>mybatis-plus-boot-starter</artifactId>
-        </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>

+ 26 - 0
seata_storage_micro_service/src/main/java/space/anyi/seataStorageMicroService/SeataStorageMicroServiceApplication.java

@@ -0,0 +1,26 @@
+package space.anyi.seataStorageMicroService;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: SeataStorageMicroServiceApplication
+ * @Author: 杨逸
+ * @Data:2025/9/20 16:53
+ * @Description:
+ */
+@MapperScan("space.anyi.seataStorageMicroService.mapper")
+@EnableFeignClients
+@EnableDiscoveryClient
+//排除数据源的自动装配,使用seata的数据源
+@SpringBootApplication()
+public class SeataStorageMicroServiceApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(SeataStorageMicroServiceApplication.class,args);
+    }
+}

+ 26 - 0
seata_storage_micro_service/src/main/java/space/anyi/seataStorageMicroService/controller/StorageController.java

@@ -0,0 +1,26 @@
+package space.anyi.seataStorageMicroService.controller;
+
+import anyi.sapce.common.entity.ResponseResult;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+import space.anyi.seataStorageMicroService.service.StorageService;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: StorageController
+ * @Author: 杨逸
+ * @Data:2025/9/20 16:59
+ * @Description:
+ */
+@RestController
+public class StorageController {
+    @Autowired
+    private StorageService storageService;
+    //扣减库存
+    @PostMapping("/storage/reduce")
+    public ResponseResult reduce(Long productId, Integer nums){
+        storageService.reduce(productId,nums);
+        return ResponseResult.okResult("扣减库存成功ok",null);
+    }
+}

+ 76 - 0
seata_storage_micro_service/src/main/java/space/anyi/seataStorageMicroService/domain/Storage.java

@@ -0,0 +1,76 @@
+package space.anyi.seataStorageMicroService.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import lombok.Data;
+
+/**
+ *
+ * @TableName storage
+ */
+@TableName(value ="storage")
+@Data
+public class Storage implements Serializable {
+    /**
+     *
+     */
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    /**
+     *
+     */
+    private Long productId;
+
+    /**
+     * 库存量
+     */
+    private Integer amount;
+
+    @TableField(exist = false)
+    private static final long serialVersionUID = 1L;
+
+    @Override
+    public boolean equals(Object that) {
+        if (this == that) {
+            return true;
+        }
+        if (that == null) {
+            return false;
+        }
+        if (getClass() != that.getClass()) {
+            return false;
+        }
+        Storage other = (Storage) that;
+        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
+            && (this.getProductId() == null ? other.getProductId() == null : this.getProductId().equals(other.getProductId()))
+            && (this.getAmount() == null ? other.getAmount() == null : this.getAmount().equals(other.getAmount()));
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
+        result = prime * result + ((getProductId() == null) ? 0 : getProductId().hashCode());
+        result = prime * result + ((getAmount() == null) ? 0 : getAmount().hashCode());
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append(getClass().getSimpleName());
+        sb.append(" [");
+        sb.append("Hash = ").append(hashCode());
+        sb.append(", id=").append(id);
+        sb.append(", productId=").append(productId);
+        sb.append(", amount=").append(amount);
+        sb.append(", serialVersionUID=").append(serialVersionUID);
+        sb.append("]");
+        return sb.toString();
+    }
+}

+ 21 - 0
seata_storage_micro_service/src/main/java/space/anyi/seataStorageMicroService/mapper/StorageMapper.java

@@ -0,0 +1,21 @@
+package space.anyi.seataStorageMicroService.mapper;
+
+import org.apache.ibatis.annotations.Mapper;
+import space.anyi.seataStorageMicroService.domain.Storage;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+* @author 杨逸
+* @description 针对表【storage】的数据库操作Mapper
+* @createDate 2025-09-20 16:56:26
+* @Entity generator.domain.Storage
+*/
+@Mapper
+public interface StorageMapper extends BaseMapper<Storage> {
+
+    void reduce(Long productId, Integer nums);
+}
+
+
+
+

+ 14 - 0
seata_storage_micro_service/src/main/java/space/anyi/seataStorageMicroService/service/StorageService.java

@@ -0,0 +1,14 @@
+package space.anyi.seataStorageMicroService.service;
+
+import space.anyi.seataStorageMicroService.domain.Storage;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+* @author 杨逸
+* @description 针对表【storage】的数据库操作Service
+* @createDate 2025-09-20 16:56:26
+*/
+public interface StorageService extends IService<Storage> {
+
+    void reduce(Long productId, Integer nums);
+}

+ 30 - 0
seata_storage_micro_service/src/main/java/space/anyi/seataStorageMicroService/service/impl/StorageServiceImpl.java

@@ -0,0 +1,30 @@
+package space.anyi.seataStorageMicroService.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import lombok.extern.slf4j.Slf4j;
+import space.anyi.seataStorageMicroService.domain.Storage;
+import space.anyi.seataStorageMicroService.service.StorageService;
+import space.anyi.seataStorageMicroService.mapper.StorageMapper;
+import org.springframework.stereotype.Service;
+
+/**
+* @author 杨逸
+* @description 针对表【storage】的数据库操作Service实现
+* @createDate 2025-09-20 16:56:26
+*/
+@Slf4j
+@Service
+public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage>
+    implements StorageService{
+
+    @Override
+    public void reduce(Long productId, Integer nums) {
+        log.info("扣减库存开始");
+        getBaseMapper().reduce(productId,nums);
+        log.info("扣减库存结束");
+    }
+}
+
+
+
+

+ 21 - 0
seata_storage_micro_service/src/main/resources/mapper/StorageMapper.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="space.anyi.seataStorageMicroService.mapper.StorageMapper">
+
+    <resultMap id="BaseResultMap" type="space.anyi.seataStorageMicroService.domain.Storage">
+            <id property="id" column="id" jdbcType="BIGINT"/>
+            <result property="productId" column="product_id" jdbcType="BIGINT"/>
+            <result property="amount" column="amount" jdbcType="INTEGER"/>
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        id,product_id,amount
+    </sql>
+    <update id="reduce">
+        UPDATE storage
+        SET amount = amount - #{nums}
+        WHERE product_id = #{productId}
+    </update>
+</mapper>