20250311 一个简单的下单流程
This commit is contained in:
commit
292f15d3ee
52
liang-springcloud-alibaba-goods-service/pom.xml
Normal file
52
liang-springcloud-alibaba-goods-service/pom.xml
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>liang-springcloud-alibaba-service</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>liang-springcloud-alibaba-goods-service</artifactId>
|
||||||
|
<name>Archetype - liang-springcloud-alibaba-goods-service</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 健康信息 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- nacos注册 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MyBatis-Plus -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
|
<version>3.4.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 如果你使用的是MySQL数据库,还需要添加MySQL驱动的依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.33</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.liang.sca.goods;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class LiangSpringcloudAlibabaGoodsApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(LiangSpringcloudAlibabaGoodsApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.liang.sca.goods.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Snowflake;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.liang.sca.goods.mapper.GoodsInfoMapper;
|
||||||
|
import com.liang.sca.goods.model.GoodsInfo;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/goods")
|
||||||
|
public class GoodsController {
|
||||||
|
|
||||||
|
@Value("${spring.application.name}:${server.port}")
|
||||||
|
String info;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GoodsInfoMapper goodsInfoMapper;
|
||||||
|
|
||||||
|
@RequestMapping("/demo")
|
||||||
|
public String demo(){
|
||||||
|
return "this is : " + info;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/snowflake")
|
||||||
|
public String snowflake(){
|
||||||
|
Snowflake snowflake = IdUtil.getSnowflake();
|
||||||
|
return snowflake.nextIdStr();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/infos")
|
||||||
|
public List<Map<String, Object>> queryInfos(){
|
||||||
|
QueryWrapper queryWrapper = new QueryWrapper();
|
||||||
|
queryWrapper.eq("c_goods_name", "星见雅");
|
||||||
|
List<Map<String,Object>> results = goodsInfoMapper.selectMaps(queryWrapper);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/buy/{goodsId}/{counts}")
|
||||||
|
public void buyGodds(@PathVariable BigInteger goodsId, @PathVariable int counts){
|
||||||
|
GoodsInfo goodsInfo = goodsInfoMapper.selectById(goodsId);
|
||||||
|
int nowStore = goodsInfo.getGoodsStore() - counts;
|
||||||
|
goodsInfo.setGoodsStore(nowStore);
|
||||||
|
goodsInfoMapper.updateById(goodsInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.liang.sca.goods.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.liang.sca.goods.model.GoodsInfo;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface GoodsInfoMapper extends BaseMapper<GoodsInfo> {
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.liang.sca.goods.model;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
@TableName("tb_goods_info")
|
||||||
|
public class GoodsInfo implements Serializable {
|
||||||
|
|
||||||
|
@TableId("n_goods_id")
|
||||||
|
private BigInteger goodsId;
|
||||||
|
|
||||||
|
@TableField("c_goods_name")
|
||||||
|
private String goodsName;
|
||||||
|
|
||||||
|
@TableField("c_goods_price")
|
||||||
|
private BigDecimal goodsPrice;
|
||||||
|
|
||||||
|
@TableField("n_goods_store")
|
||||||
|
private Integer goodsStore;
|
||||||
|
|
||||||
|
public Integer getGoodsStore() {
|
||||||
|
return goodsStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGoodsStore(Integer goodsStore) {
|
||||||
|
this.goodsStore = goodsStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigInteger getGoodsId() {
|
||||||
|
return goodsId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGoodsId(BigInteger goodsId) {
|
||||||
|
this.goodsId = goodsId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGoodsName() {
|
||||||
|
return goodsName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGoodsName(String goodsName) {
|
||||||
|
this.goodsName = goodsName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getGoodsPrice() {
|
||||||
|
return goodsPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGoodsPrice(BigDecimal goodsPrice) {
|
||||||
|
this.goodsPrice = goodsPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
server:
|
||||||
|
port: 7002
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: goods-service
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: http://154.12.80.119:8848 #配置Nacos地址
|
||||||
|
datasource:
|
||||||
|
url: jdbc:mysql://154.12.80.119:33061/goods_db
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
jackson:
|
||||||
|
serialization:
|
||||||
|
FAIL_ON_EMPTY_BEANS: false
|
63
liang-springcloud-alibaba-order-service/pom.xml
Normal file
63
liang-springcloud-alibaba-order-service/pom.xml
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>liang-springcloud-alibaba-service</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>liang-springcloud-alibaba-order-service</artifactId>
|
||||||
|
<name>Archetype - liang-springcloud-alibaba-order-service</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 健康信息 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MyBatis-Plus -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
|
<version>3.4.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 如果你使用的是MySQL数据库,还需要添加MySQL驱动的依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.33</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.36</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.liang.sca.order;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
@EnableFeignClients
|
||||||
|
@SpringBootApplication
|
||||||
|
public class LiangSpringcloudAlibabaOrderApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(LiangSpringcloudAlibabaOrderApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.liang.sca.order.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import com.liang.sca.order.mapper.OrderInfoMapper;
|
||||||
|
import com.liang.sca.order.model.OrderInfo;
|
||||||
|
import com.liang.sca.order.service.GoodsInfoService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/order")
|
||||||
|
public class OrderInfoController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OrderInfoMapper orderInfoMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GoodsInfoService goodsInfoService;
|
||||||
|
|
||||||
|
@RequestMapping("/buy/{counts}")
|
||||||
|
public String buyGoods(@PathVariable int counts){
|
||||||
|
List<Map<String, Object>> maps = goodsInfoService.queryGoodsInfo();
|
||||||
|
if(CollectionUtil.isNotEmpty(maps)){
|
||||||
|
Map<String, Object> goodsInfo = maps.get(0);
|
||||||
|
BigInteger goodsId = BigInteger.valueOf((long)goodsInfo.get("goodsId"));
|
||||||
|
String goodsName = goodsInfo.get("goodsName").toString();
|
||||||
|
OrderInfo orderInfo = new OrderInfo();
|
||||||
|
goodsInfoService.buyGoods(goodsId, counts);
|
||||||
|
return "商品:"+goodsName+",下单数量:"+counts+"件下单成功";
|
||||||
|
} else {
|
||||||
|
return "没找到商品";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.liang.sca.order.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.liang.sca.order.model.OrderInfo;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.liang.sca.order.model;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("tb_order_info")
|
||||||
|
public class OrderInfo {
|
||||||
|
|
||||||
|
@TableId("n_order_id")
|
||||||
|
private BigInteger orderId;
|
||||||
|
@TableField("c_clnt_name")
|
||||||
|
private String clntName;
|
||||||
|
@TableField("c_good_id")
|
||||||
|
private String goodId;
|
||||||
|
@TableField("n_buy_nums")
|
||||||
|
private Integer buyNums;
|
||||||
|
@TableField("n_price")
|
||||||
|
private BigDecimal price;
|
||||||
|
@TableField("c_pay_status")
|
||||||
|
private String payStatus;
|
||||||
|
@TableField("n_actul_pay")
|
||||||
|
private BigDecimal actulPay;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.liang.sca.order.service;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@FeignClient("goods-service")
|
||||||
|
public interface GoodsInfoService {
|
||||||
|
|
||||||
|
@GetMapping("/goods/infos")
|
||||||
|
List<Map<String, Object>> queryGoodsInfo();
|
||||||
|
|
||||||
|
@GetMapping("/goods/buy/{goodsId}/{counts}")
|
||||||
|
void buyGoods(@PathVariable BigInteger goodsId, @PathVariable int counts);
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
server:
|
||||||
|
port: 7003
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: order-service
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: http://154.12.80.119:8848 #配置Nacos地址
|
||||||
|
datasource:
|
||||||
|
url: jdbc:mysql://154.12.80.119:33061/order_db
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
43
pom.xml
Normal file
43
pom.xml
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.4.6</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>liang-springcloud-alibaba-service</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>Archetype - liang-springcloud-alibaba-service</name>
|
||||||
|
<modules>
|
||||||
|
<module>liang-springcloud-alibaba-goods-service</module>
|
||||||
|
<module>liang-springcloud-alibaba-order-service</module>
|
||||||
|
</modules>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<!-- SpringCloud Alibaba -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||||
|
<version>2021.1</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>2020.0.3</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user