feat(seata): 初始化
This commit is contained in:
parent
ab4d9aa70f
commit
b59f70afbe
|
@ -1,13 +0,0 @@
|
|||
package cn.bunny;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
|
@ -28,6 +28,6 @@ public class Knife4jConfig {
|
|||
// 管理员相关分类接口
|
||||
@Bean
|
||||
public GroupedOpenApi groupedOpenAdminApi() {
|
||||
return GroupedOpenApi.builder().group("默认请求接口").pathsToMatch("/api/**").build();
|
||||
return GroupedOpenApi.builder().group("默认请求接口").pathsToMatch("/**").build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
CREATE DATABASE IF NOT EXISTS `storage_db`;
|
||||
USE `storage_db`;
|
||||
DROP TABLE IF EXISTS `storage_tbl`;
|
||||
CREATE TABLE `storage_tbl`
|
||||
(
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`commodity_code` varchar(255) DEFAULT NULL,
|
||||
`count` int(11) DEFAULT 0,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY (`commodity_code`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8;
|
||||
INSERT INTO storage_tbl (commodity_code, count)
|
||||
VALUES ('P0001', 100);
|
||||
INSERT INTO storage_tbl (commodity_code, count)
|
||||
VALUES ('B1234', 10);
|
||||
|
||||
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
|
||||
DROP TABLE IF EXISTS `undo_log`;
|
||||
CREATE TABLE `undo_log`
|
||||
(
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) NOT NULL,
|
||||
`context` varchar(128) NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime NOT NULL,
|
||||
`log_modified` datetime NOT NULL,
|
||||
`ext` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
CREATE DATABASE IF NOT EXISTS `order_db`;
|
||||
USE `order_db`;
|
||||
DROP TABLE IF EXISTS `order_tbl`;
|
||||
CREATE TABLE `order_tbl`
|
||||
(
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` varchar(255) DEFAULT NULL,
|
||||
`commodity_code` varchar(255) DEFAULT NULL,
|
||||
`count` int(11) DEFAULT 0,
|
||||
`money` int(11) DEFAULT 0,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8;
|
||||
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
|
||||
DROP TABLE IF EXISTS `undo_log`;
|
||||
CREATE TABLE `undo_log`
|
||||
(
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) NOT NULL,
|
||||
`context` varchar(128) NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime NOT NULL,
|
||||
`log_modified` datetime NOT NULL,
|
||||
`ext` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
||||
|
||||
CREATE DATABASE IF NOT EXISTS `account_db`;
|
||||
USE `account_db`;
|
||||
DROP TABLE IF EXISTS `account_tbl`;
|
||||
CREATE TABLE `account_tbl`
|
||||
(
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` varchar(255) DEFAULT NULL,
|
||||
`money` int(11) DEFAULT 0,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8;
|
||||
INSERT INTO account_tbl (user_id, money)
|
||||
VALUES ('1', 10000);
|
||||
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
|
||||
DROP TABLE IF EXISTS `undo_log`;
|
||||
CREATE TABLE `undo_log`
|
||||
(
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`branch_id` bigint(20) NOT NULL,
|
||||
`xid` varchar(100) NOT NULL,
|
||||
`context` varchar(128) NOT NULL,
|
||||
`rollback_info` longblob NOT NULL,
|
||||
`log_status` int(11) NOT NULL,
|
||||
`log_created` datetime NOT NULL,
|
||||
`log_modified` datetime NOT NULL,
|
||||
`ext` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
|
||||
) ENGINE = InnoDB
|
||||
AUTO_INCREMENT = 1
|
||||
DEFAULT CHARSET = utf8;
|
|
@ -1,33 +0,0 @@
|
|||
package cn.bunny.config;
|
||||
|
||||
import io.swagger.v3.oas.models.ExternalDocumentation;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Contact;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.info.License;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springdoc.core.models.GroupedOpenApi;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class Knife4jConfig {
|
||||
@Bean
|
||||
public OpenAPI openAPI() {
|
||||
// 作者等信息
|
||||
Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://z-bunny.cn");
|
||||
// 使用协议
|
||||
License license = new License().name("MIT").url("https://MUT.com");
|
||||
// 相关信息
|
||||
Info info = new Info().title("Bunny-Order").description("权限管理模板").version("v1.0.0").contact(contact).license(license).termsOfService("MIT");
|
||||
|
||||
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
|
||||
}
|
||||
|
||||
// 管理员相关分类接口
|
||||
@Bean
|
||||
public GroupedOpenApi groupedOpenAdminApi() {
|
||||
return GroupedOpenApi.builder().group("默认请求接口").pathsToMatch("/api/**").build();
|
||||
}
|
||||
}
|
|
@ -12,6 +12,10 @@
|
|||
<modules>
|
||||
<module>cloud-demo1</module>
|
||||
<module>cloud-demo2</module>
|
||||
<module>seata-account</module>
|
||||
<module>seata-business</module>
|
||||
<module>seata-order</module>
|
||||
<module>seata-storage</module>
|
||||
</modules>
|
||||
<name>service</name>
|
||||
<url>https://maven.apache.org</url>
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cn.bunny</groupId>
|
||||
<artifactId>service</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>seata-account</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.atguigu.account;
|
||||
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
|
||||
|
||||
@MapperScan("com.atguigu.account.mapper")
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class SeataAccountMainApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataAccountMainApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.atguigu.account.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
*
|
||||
* @TableName account_tbl
|
||||
*/
|
||||
@Data
|
||||
public class AccountTbl implements Serializable {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String userId;
|
||||
|
||||
private Integer money;
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.atguigu.account.controller;
|
||||
|
||||
import com.atguigu.account.service.AccountService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class AccountRestController {
|
||||
|
||||
|
||||
@Autowired
|
||||
AccountService accountService;
|
||||
/**
|
||||
* 扣减账户余额
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/debit")
|
||||
public String debit(@RequestParam("userId") String userId,
|
||||
@RequestParam("money") int money){
|
||||
accountService.debit(userId, money);
|
||||
return "account debit success";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.atguigu.account.mapper;
|
||||
|
||||
import com.atguigu.account.bean.AccountTbl;
|
||||
|
||||
/**
|
||||
* @author lfy
|
||||
* @description 针对表【account_tbl】的数据库操作Mapper
|
||||
* @createDate 2025-01-08 18:32:50
|
||||
* @Entity com.atguigu.account.bean.AccountTbl
|
||||
*/
|
||||
public interface AccountTblMapper {
|
||||
|
||||
int deleteByPrimaryKey(Long id);
|
||||
|
||||
int insert(AccountTbl record);
|
||||
|
||||
int insertSelective(AccountTbl record);
|
||||
|
||||
AccountTbl selectByPrimaryKey(Long id);
|
||||
|
||||
int updateByPrimaryKeySelective(AccountTbl record);
|
||||
|
||||
int updateByPrimaryKey(AccountTbl record);
|
||||
|
||||
void debit(String userId, int money);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.atguigu.account.service;
|
||||
|
||||
public interface AccountService {
|
||||
|
||||
/**
|
||||
* 从用户账户中扣减
|
||||
* @param userId 用户id
|
||||
* @param money 扣减金额
|
||||
*/
|
||||
void debit(String userId, int money);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.atguigu.account.service.impl;
|
||||
|
||||
import com.atguigu.account.mapper.AccountTblMapper;
|
||||
import com.atguigu.account.service.AccountService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AccountServiceImpl implements AccountService {
|
||||
|
||||
@Autowired
|
||||
AccountTblMapper accountTblMapper;
|
||||
@Override
|
||||
public void debit(String userId, int money) {
|
||||
// 扣减账户余额
|
||||
accountTblMapper.debit(userId,money);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
spring:
|
||||
application:
|
||||
name: seata-account
|
||||
datasource:
|
||||
url: jdbc:mysql://192.168.3.133:3306/account_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||
username: root
|
||||
password: "123456"
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132:8848
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
server:
|
||||
port: 10000
|
||||
|
||||
mybatis:
|
||||
mapper-locations: classpath:mapper/*.xml
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
<?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="com.atguigu.account.mapper.AccountTblMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.atguigu.account.bean.AccountTbl">
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="userId" column="user_id" jdbcType="VARCHAR"/>
|
||||
<result property="money" column="money" jdbcType="INTEGER"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,user_id,money
|
||||
</sql>
|
||||
|
||||
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from account_tbl
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</select>
|
||||
|
||||
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
|
||||
delete from account_tbl
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</delete>
|
||||
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.atguigu.account.bean.AccountTbl" useGeneratedKeys="true">
|
||||
insert into account_tbl
|
||||
( id,user_id,money
|
||||
)
|
||||
values (#{id,jdbcType=INTEGER},#{userId,jdbcType=VARCHAR},#{money,jdbcType=INTEGER}
|
||||
)
|
||||
</insert>
|
||||
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.atguigu.account.bean.AccountTbl" useGeneratedKeys="true">
|
||||
insert into account_tbl
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="userId != null">user_id,</if>
|
||||
<if test="money != null">money,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id,jdbcType=INTEGER},</if>
|
||||
<if test="userId != null">#{userId,jdbcType=VARCHAR},</if>
|
||||
<if test="money != null">#{money,jdbcType=INTEGER},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<update id="updateByPrimaryKeySelective" parameterType="com.atguigu.account.bean.AccountTbl">
|
||||
update account_tbl
|
||||
<set>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="money != null">
|
||||
money = #{money,jdbcType=INTEGER},
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<update id="updateByPrimaryKey" parameterType="com.atguigu.account.bean.AccountTbl">
|
||||
update account_tbl
|
||||
set
|
||||
user_id = #{userId,jdbcType=VARCHAR},
|
||||
money = #{money,jdbcType=INTEGER}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<update id="debit">
|
||||
update account_tbl
|
||||
set money = money - #{money,jdbcType=INTEGER}
|
||||
where user_id = #{userId,jdbcType=VARCHAR}
|
||||
</update>
|
||||
</mapper>
|
|
@ -0,0 +1,40 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cn.bunny</groupId>
|
||||
<artifactId>service</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>seata-business</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,15 @@
|
|||
package com.atguigu.business;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class SeataBusinessMainApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataBusinessMainApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.atguigu.business.controller;
|
||||
|
||||
import com.atguigu.business.service.BusinessService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class PurchaseRestController {
|
||||
|
||||
@Autowired
|
||||
BusinessService businessService;
|
||||
|
||||
|
||||
/**
|
||||
* 购买
|
||||
* @param userId 用户ID
|
||||
* @param commodityCode 商品编码
|
||||
* @param orderCount 数量
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/purchase")
|
||||
public String purchase(@RequestParam("userId") String userId,
|
||||
@RequestParam("commodityCode") String commodityCode,
|
||||
@RequestParam("count") int orderCount){
|
||||
businessService.purchase(userId, commodityCode, orderCount);
|
||||
return "business purchase success";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.atguigu.business.service;
|
||||
|
||||
public interface BusinessService {
|
||||
|
||||
/**
|
||||
* 采购
|
||||
* @param userId 用户id
|
||||
* @param commodityCode 商品编号
|
||||
* @param orderCount 购买数量
|
||||
*/
|
||||
void purchase(String userId, String commodityCode, int orderCount);
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.atguigu.business.service.impl;
|
||||
|
||||
import com.atguigu.business.service.BusinessService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
@Service
|
||||
public class BusinessServiceImpl implements BusinessService {
|
||||
@Override
|
||||
public void purchase(String userId, String commodityCode, int orderCount) {
|
||||
//TODO 1. 扣减库存
|
||||
|
||||
//TODO 2. 创建订单
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
spring:
|
||||
application:
|
||||
name: seata-business
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132:8848
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
server:
|
||||
port: 11000
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cn.bunny</groupId>
|
||||
<artifactId>service</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>seata-order</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.atguigu.order;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
|
||||
@MapperScan("com.atguigu.order.mapper")
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class SeataOrderMainApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataOrderMainApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.atguigu.order.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @TableName order_tbl
|
||||
*/
|
||||
@Data
|
||||
public class OrderTbl implements Serializable {
|
||||
private Integer id;
|
||||
|
||||
private String userId;
|
||||
|
||||
private String commodityCode;
|
||||
|
||||
private Integer count;
|
||||
|
||||
private Integer money;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.atguigu.order.controller;
|
||||
|
||||
|
||||
import com.atguigu.order.bean.OrderTbl;
|
||||
import com.atguigu.order.service.OrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class OrderRestController {
|
||||
|
||||
@Autowired
|
||||
OrderService orderService;
|
||||
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
* @param userId
|
||||
* @param commodityCode
|
||||
* @param orderCount
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/create")
|
||||
public String create(@RequestParam("userId") String userId,
|
||||
@RequestParam("commodityCode") String commodityCode,
|
||||
@RequestParam("count") int orderCount)
|
||||
{
|
||||
OrderTbl tbl = orderService.create(userId, commodityCode, orderCount);
|
||||
return "order create success = 订单id:【"+tbl.getId()+"】";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.atguigu.order.mapper;
|
||||
|
||||
import com.atguigu.order.bean.OrderTbl;
|
||||
|
||||
/**
|
||||
* @author lfy
|
||||
* @description 针对表【order_tbl】的数据库操作Mapper
|
||||
* @createDate 2025-01-08 18:34:18
|
||||
* @Entity com.atguigu.order.bean.OrderTbl
|
||||
*/
|
||||
public interface OrderTblMapper {
|
||||
|
||||
int deleteByPrimaryKey(Long id);
|
||||
|
||||
int insert(OrderTbl record);
|
||||
|
||||
int insertSelective(OrderTbl record);
|
||||
|
||||
OrderTbl selectByPrimaryKey(Long id);
|
||||
|
||||
int updateByPrimaryKeySelective(OrderTbl record);
|
||||
|
||||
int updateByPrimaryKey(OrderTbl record);
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.atguigu.order.service;
|
||||
|
||||
import com.atguigu.order.bean.OrderTbl;
|
||||
|
||||
public interface OrderService {
|
||||
/**
|
||||
* 创建订单
|
||||
* @param userId 用户id
|
||||
* @param commodityCode 商品编码
|
||||
* @param orderCount 商品数量
|
||||
*/
|
||||
OrderTbl create(String userId, String commodityCode, int orderCount);
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.atguigu.order.service.impl;
|
||||
|
||||
import com.atguigu.order.bean.OrderTbl;
|
||||
import com.atguigu.order.mapper.OrderTblMapper;
|
||||
import com.atguigu.order.service.OrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Autowired
|
||||
OrderTblMapper orderTblMapper;
|
||||
|
||||
@Override
|
||||
public OrderTbl create(String userId, String commodityCode, int orderCount) {
|
||||
//1、计算订单价格
|
||||
int orderMoney = calculate(commodityCode, orderCount);
|
||||
|
||||
//TODO 2、扣减账户余额
|
||||
|
||||
//3、保存订单
|
||||
OrderTbl orderTbl = new OrderTbl();
|
||||
orderTbl.setUserId(userId);
|
||||
orderTbl.setCommodityCode(commodityCode);
|
||||
orderTbl.setCount(orderCount);
|
||||
orderTbl.setMoney(orderMoney);
|
||||
|
||||
orderTblMapper.insert(orderTbl);
|
||||
|
||||
return orderTbl;
|
||||
}
|
||||
|
||||
// 计算价格
|
||||
private int calculate(String commodityCode, int orderCount) {
|
||||
return 9*orderCount;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
spring:
|
||||
application:
|
||||
name: seata-order
|
||||
datasource:
|
||||
url: jdbc:mysql://192.168.3.133:3306/order_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132:8848
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
|
||||
server:
|
||||
port: 12000
|
||||
mybatis:
|
||||
mapper-locations: classpath:mapper/*.xml
|
|
@ -0,0 +1,83 @@
|
|||
<?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="com.atguigu.order.mapper.OrderTblMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.atguigu.order.bean.OrderTbl">
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="userId" column="user_id" jdbcType="VARCHAR"/>
|
||||
<result property="commodityCode" column="commodity_code" jdbcType="VARCHAR"/>
|
||||
<result property="count" column="count" jdbcType="INTEGER"/>
|
||||
<result property="money" column="money" jdbcType="INTEGER"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,user_id,commodity_code,
|
||||
count,money
|
||||
</sql>
|
||||
|
||||
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from order_tbl
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</select>
|
||||
|
||||
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
|
||||
delete from order_tbl
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</delete>
|
||||
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.atguigu.order.bean.OrderTbl"
|
||||
useGeneratedKeys="true">
|
||||
insert into order_tbl
|
||||
( id,user_id,commodity_code
|
||||
,count,money)
|
||||
values (#{id,jdbcType=INTEGER},#{userId,jdbcType=VARCHAR},#{commodityCode,jdbcType=VARCHAR}
|
||||
,#{count,jdbcType=INTEGER},#{money,jdbcType=INTEGER})
|
||||
</insert>
|
||||
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.atguigu.order.bean.OrderTbl" useGeneratedKeys="true">
|
||||
insert into order_tbl
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="userId != null">user_id,</if>
|
||||
<if test="commodityCode != null">commodity_code,</if>
|
||||
<if test="count != null">count,</if>
|
||||
<if test="money != null">money,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id,jdbcType=INTEGER},</if>
|
||||
<if test="userId != null">#{userId,jdbcType=VARCHAR},</if>
|
||||
<if test="commodityCode != null">#{commodityCode,jdbcType=VARCHAR},</if>
|
||||
<if test="count != null">#{count,jdbcType=INTEGER},</if>
|
||||
<if test="money != null">#{money,jdbcType=INTEGER},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<update id="updateByPrimaryKeySelective" parameterType="com.atguigu.order.bean.OrderTbl">
|
||||
update order_tbl
|
||||
<set>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="commodityCode != null">
|
||||
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="count != null">
|
||||
count = #{count,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="money != null">
|
||||
money = #{money,jdbcType=INTEGER},
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<update id="updateByPrimaryKey" parameterType="com.atguigu.order.bean.OrderTbl">
|
||||
update order_tbl
|
||||
set
|
||||
user_id = #{userId,jdbcType=VARCHAR},
|
||||
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||
count = #{count,jdbcType=INTEGER},
|
||||
money = #{money,jdbcType=INTEGER}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
</mapper>
|
|
@ -0,0 +1,49 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cn.bunny</groupId>
|
||||
<artifactId>service</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>seata-storage</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,16 @@
|
|||
package com.atguigu.storage;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
@MapperScan("com.atguigu.storage.mapper")
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class SeataStorageMainApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SeataStorageMainApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.atguigu.storage.bean;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @TableName storage_tbl
|
||||
*/
|
||||
@Data
|
||||
public class StorageTbl implements Serializable {
|
||||
private Integer id;
|
||||
|
||||
private String commodityCode;
|
||||
|
||||
private Integer count;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.atguigu.storage.controller;
|
||||
|
||||
|
||||
import com.atguigu.storage.service.StorageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class StorageRestController {
|
||||
|
||||
@Autowired
|
||||
StorageService storageService;
|
||||
|
||||
@GetMapping("/deduct")
|
||||
public String deduct(@RequestParam("commodityCode") String commodityCode,
|
||||
@RequestParam("count") Integer count) {
|
||||
|
||||
storageService.deduct(commodityCode, count);
|
||||
return "storage deduct success";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.atguigu.storage.mapper;
|
||||
|
||||
import com.atguigu.storage.bean.StorageTbl;
|
||||
|
||||
/**
|
||||
* @author lfy
|
||||
* @description 针对表【storage_tbl】的数据库操作Mapper
|
||||
* @createDate 2025-01-08 18:35:07
|
||||
* @Entity com.atguigu.storage.bean.StorageTbl
|
||||
*/
|
||||
public interface StorageTblMapper {
|
||||
|
||||
int deleteByPrimaryKey(Long id);
|
||||
|
||||
int insert(StorageTbl record);
|
||||
|
||||
int insertSelective(StorageTbl record);
|
||||
|
||||
StorageTbl selectByPrimaryKey(Long id);
|
||||
|
||||
int updateByPrimaryKeySelective(StorageTbl record);
|
||||
|
||||
int updateByPrimaryKey(StorageTbl record);
|
||||
|
||||
void deduct(String commodityCode, int count);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.atguigu.storage.service;
|
||||
|
||||
public interface StorageService {
|
||||
|
||||
|
||||
/**
|
||||
* 扣除存储数量
|
||||
* @param commodityCode 商品编码
|
||||
* @param count 数量
|
||||
*/
|
||||
void deduct(String commodityCode, int count);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.atguigu.storage.service.impl;
|
||||
|
||||
import com.atguigu.storage.mapper.StorageTblMapper;
|
||||
import com.atguigu.storage.service.StorageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StorageServiceImpl implements StorageService {
|
||||
|
||||
@Autowired
|
||||
StorageTblMapper storageTblMapper;
|
||||
|
||||
@Override
|
||||
public void deduct(String commodityCode, int count) {
|
||||
storageTblMapper.deduct(commodityCode, count);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
spring:
|
||||
application:
|
||||
name: seata-storage
|
||||
datasource:
|
||||
url: jdbc:mysql://192.168.3.133:3306/storage_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132:8848
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
server:
|
||||
port: 13000
|
||||
|
||||
mybatis:
|
||||
mapper-locations: classpath:mapper/*.xml
|
|
@ -0,0 +1,72 @@
|
|||
<?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="com.atguigu.storage.mapper.StorageTblMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.atguigu.storage.bean.StorageTbl">
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="commodityCode" column="commodity_code" jdbcType="VARCHAR"/>
|
||||
<result property="count" column="count" jdbcType="INTEGER"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,commodity_code,count
|
||||
</sql>
|
||||
|
||||
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from storage_tbl
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</select>
|
||||
|
||||
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
|
||||
delete from storage_tbl
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</delete>
|
||||
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.atguigu.storage.bean.StorageTbl" useGeneratedKeys="true">
|
||||
insert into storage_tbl
|
||||
( id,commodity_code,count
|
||||
)
|
||||
values (#{id,jdbcType=INTEGER},#{commodityCode,jdbcType=VARCHAR},#{count,jdbcType=INTEGER}
|
||||
)
|
||||
</insert>
|
||||
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.atguigu.storage.bean.StorageTbl" useGeneratedKeys="true">
|
||||
insert into storage_tbl
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="commodityCode != null">commodity_code,</if>
|
||||
<if test="count != null">count,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id,jdbcType=INTEGER},</if>
|
||||
<if test="commodityCode != null">#{commodityCode,jdbcType=VARCHAR},</if>
|
||||
<if test="count != null">#{count,jdbcType=INTEGER},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<update id="updateByPrimaryKeySelective" parameterType="com.atguigu.storage.bean.StorageTbl">
|
||||
update storage_tbl
|
||||
<set>
|
||||
<if test="commodityCode != null">
|
||||
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="count != null">
|
||||
count = #{count,jdbcType=INTEGER},
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<update id="updateByPrimaryKey" parameterType="com.atguigu.storage.bean.StorageTbl">
|
||||
update storage_tbl
|
||||
set
|
||||
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||
count = #{count,jdbcType=INTEGER}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<update id="deduct">
|
||||
update storage_tbl
|
||||
set count = count - #{count}
|
||||
where commodity_code = #{commodityCode}
|
||||
</update>
|
||||
</mapper>
|
Loading…
Reference in New Issue