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

feat:为seata_account_micro_service账户微服务模块编写业务代码

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

+ 24 - 0
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/SeataAccountMicroServiceApplication.java

@@ -0,0 +1,24 @@
+package space.anyi.seataAccountMicroService;
+
+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.cloud.openfeign.EnableFeignClients;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: SeataAccountMicroServiceApplication
+ * @Author: 杨逸
+ * @Data:2025/9/21 10:50
+ * @Description:
+ */
+@MapperScan("space.anyi.seataAccountMicroService.mapper")
+@SpringBootApplication
+@EnableFeignClients
+@EnableDiscoveryClient
+public class SeataAccountMicroServiceApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(SeataAccountMicroServiceApplication.class,args);
+    }
+}

+ 25 - 0
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/controller/AccountController.java

@@ -0,0 +1,25 @@
+package space.anyi.seataAccountMicroService.controller;
+
+import anyi.sapce.common.entity.ResponseResult;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import space.anyi.seataAccountMicroService.service.AccountService;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: AccountController
+ * @Author: 杨逸
+ * @Data:2025/9/21 10:59
+ * @Description:
+ */
+@RestController
+public class AccountController {
+    @Autowired
+    private AccountService accountService;
+
+    public ResponseResult reduce(@RequestParam("userId")Long userId, @RequestParam("money")Integer money){
+        accountService.reduce(userId,money);
+        return ResponseResult.okResult(200,"扣减账户余额OK");
+    }
+}

+ 76 - 0
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/domain/Account.java

@@ -0,0 +1,76 @@
+package space.anyi.seataAccountMicroService.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 account
+ */
+@TableName(value ="account")
+@Data
+public class Account implements Serializable {
+    /**
+     *
+     */
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    /**
+     *
+     */
+    private Long userId;
+
+    /**
+     * 账户金额
+     */
+    private Integer money;
+
+    @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;
+        }
+        Account other = (Account) that;
+        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
+            && (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
+            && (this.getMoney() == null ? other.getMoney() == null : this.getMoney().equals(other.getMoney()));
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
+        result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
+        result = prime * result + ((getMoney() == null) ? 0 : getMoney().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(", userId=").append(userId);
+        sb.append(", money=").append(money);
+        sb.append(", serialVersionUID=").append(serialVersionUID);
+        sb.append("]");
+        return sb.toString();
+    }
+}

+ 19 - 0
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/mapper/AccountMapper.java

@@ -0,0 +1,19 @@
+package space.anyi.seataAccountMicroService.mapper;
+
+import space.anyi.seataAccountMicroService.domain.Account;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+* @author 杨逸
+* @description 针对表【account】的数据库操作Mapper
+* @createDate 2025-09-21 10:52:56
+* @Entity generator.domain.Account
+*/
+public interface AccountMapper extends BaseMapper<Account> {
+
+    void reduce(Long userId, Integer money);
+}
+
+
+
+

+ 14 - 0
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/service/AccountService.java

@@ -0,0 +1,14 @@
+package space.anyi.seataAccountMicroService.service;
+
+import space.anyi.seataAccountMicroService.domain.Account;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+* @author 杨逸
+* @description 针对表【account】的数据库操作Service
+* @createDate 2025-09-21 10:52:56
+*/
+public interface AccountService extends IService<Account> {
+
+    void reduce(Long userId, Integer money);
+}

+ 30 - 0
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/service/impl/AccountServiceImpl.java

@@ -0,0 +1,30 @@
+package space.anyi.seataAccountMicroService.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import lombok.extern.slf4j.Slf4j;
+import space.anyi.seataAccountMicroService.domain.Account;
+import space.anyi.seataAccountMicroService.service.AccountService;
+import space.anyi.seataAccountMicroService.mapper.AccountMapper;
+import org.springframework.stereotype.Service;
+
+/**
+* @author 杨逸
+* @description 针对表【account】的数据库操作Service实现
+* @createDate 2025-09-21 10:52:56
+*/
+@Service
+@Slf4j
+public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
+    implements AccountService{
+
+    @Override
+    public void reduce(Long userId, Integer money) {
+        log.info("扣款开始");
+        getBaseMapper().reduce(userId, money);
+        log.info("扣款结束");
+    }
+}
+
+
+
+

+ 21 - 0
seata_ccount_micro_service/src/main/resources/mapper/AccountMapper.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.seataAccountMicroService.mapper.AccountMapper">
+
+    <resultMap id="BaseResultMap" type="space.anyi.seataAccountMicroService.domain.Account">
+            <id property="id" column="id" jdbcType="BIGINT"/>
+            <result property="userId" column="user_id" jdbcType="BIGINT"/>
+            <result property="money" column="money" jdbcType="INTEGER"/>
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        id,user_id,money
+    </sql>
+    <update id="reduce">
+        UPDATE account
+        SET money = money - #{money}
+        WHERE user_id = #{userId};
+    </update>
+</mapper>