springboot构建一个完整的响应式电商网站

在这里插入图片描述

项目演示:

整体功能

前端部分:

gitee地址:react 完整代码

后端部分:

pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>product-service</artifactId>

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.4</version>
        </dependency>
    </dependencies>
</project>

控制器代码:

/**
 * @author zxl
 * @date 4/15/22
 */
@RestController
@RequestMapping("/")
public class ProductController {

    private HashSet set = new HashSet();

    //private final AtomicInteger AMOUNT = new AtomicInteger();
    @GetMapping("/products")
    public HashMap products(){
        HashMap result = new HashMap();
        ArrayList list = new ArrayList();
        HashMap data = null;
        for (int i = 0; i < 8; i++) {
            data = new HashMap();
            data.put("id",i+1);
            data.put("imageUrl","https://cdn.chec.io/merchants/19661/assets/VFgCKovPcmNWYm4G|Screenshot 2020-11-25 at 11.01.24.png");
            data.put("name","第"+(i+1)+"个商品");
            data.put("price",45.5+i);
            data.put("description","<p style='color:red;'>商品描述</p>");
            list.add(data);
        }
        result.put("data",list);
        return result;
    }

    @GetMapping("/cart/{productId}/{amount}")
    public HashMap cart(@PathVariable String productId,@PathVariable Integer amount){
        HashMap result = new HashMap();
        //cartItem
        HashMap data = new HashMap();
        data.put("id",productId);
        // data.put("imageUrl","https://cdn.chec.io/merchants/18462/images/676785cedc85f69ab27c42c307af5dec30120ab75f03a9889ab29|u9 1.png");
        data.put("imageUrl","https://cdn.chec.io/merchants/19661/assets/VFgCKovPcmNWYm4G|Screenshot 2020-11-25 at 11.01.24.png");
        data.put("name",productId);
        data.put("price",45.5);
        data.put("description","<p style='color:red;'>商品描述</p>");
        data.put("totalPrice",45.5);
        data.put("quantity",amount);
        result.put("totalPrice",99999);
        set.add(data);
        //cart
        result.put("items",set);
        result.put("total_items",set.size());
        result.put("id",1234);
        return result;
    }

    @PutMapping("/cart/{productId}/{amount}")
    public HashMap updateCart(@PathVariable String productId,@PathVariable Integer amount){
        HashMap result = new HashMap();
        //cartItem
        HashMap data = new HashMap();
        //update cartItem
        for (Object o : set) {
            HashMap item = (HashMap) o;
            if(productId.equals(item.get("id"))){
                item.put("quantity",amount);
            }
        }
        result.put("totalPrice",99999);
        //set.add(data);
        //cart
        result.put("items",set);
        result.put("total_items",set.size());
        result.put("id",1234);
        return result;
    }

    @DeleteMapping("/cart/{productId}")
    public HashMap removeFromCart(@PathVariable String productId){
        HashMap result = new HashMap();
        //cartItem
        //remove item
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            HashMap item = (HashMap)iterator.next();
            if(productId.equals(item.get("id"))){
                 iterator.remove();
            }

        }

        result.put("totalPrice",99999);
        result.put("items",set);
        result.put("total_items",set.size());
        result.put("id",1234);
        return result;
    }
    @GetMapping("/carts")
    public HashMap carts(){
        HashMap result = new HashMap();
        result.put("totalPrice",99999);
        result.put("items",set);
        result.put("total_items",set.size());
        result.put("id",1234);
        return result;
    }

    @GetMapping("/carts/empty")
    public HashMap empty(){
        set.clear();
        HashMap result = new HashMap();
        result.put("totalPrice",99999);
        result.put("items",set);
        result.put("total_items",set.size());
        result.put("id",1234);
        return result;
    }

    @PostMapping("/carts/checkout")
    public HashMap checkout(@RequestBody  Shipping shipping){
        HashMap data = new HashMap();
        data.put("customer",shipping);
        set.clear();
        return data;
    }
}

application.yml

server:
  port: 19090
Spring Boot是一种快速开发应用程序的框架,非常适合构建响应式的Web服务和应用。借助Spring Boot,可以轻松地快速开发出一个水果电商系统。 首先,在构建一个Spring Boot应用程序前,需要有一个基本的项目结构,可以通过使用Spring Initializr创建一个Maven项目骨架。 其次,需要定义实体类以及持久层接口和类。对于水果电商系统,可以定义Fruit实体类,包含名称、价格、描述等属性,针对Fruit实体类可以定义FruitMapper接口和类,用来持久化Fruit数据。 然后,定义服务层接口和实现类。可以定义FruitService接口和类,其中定义了获取、添加、删除、修改等Fruit数据的方法。在实现类中,使用FruitMapper持久化和查询数据。 接下来,需要构建控制层,通过定义FruitController类来处理请求和响应。可以通过在Controller类中定义@RequestMapping注解来映射RESTful风格的接口地址。 最后,可以使用Thymeleaf模板引擎对前端页面进行渲染。Thymeleaf提供了丰富的标签和属性,可以很好地实现页面中的数据绑定、循环、条件判断等功能,并且Thymeleaf的语法很容易学习和使用。 综上所述,使用Spring Boot可以非常方便地快速构建一个水果电商系统,源代码也很简洁清晰。另外,为了更好地实现水果电商系统所需基本功能,还可以考虑配置Redis作为缓存、使用Spring Security实现认证和授权、使用JWT实现无状态认证等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挺风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值