Use redis hash to implement a simple shopping cart function

step1 We know the data structure stored in redis lInsert picture description here

step2 Let's first know the commonly used api of redis for hash
Insert picture description here
step 3
Insert picture description here

// 跟着上面实现一个简单的购物车
package com.shopCar.controller;

import com.shopCar.search.ShopCarSearch;
import com.shopCar.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class ShopCarController {
    
    
    @Autowired
    ShopCarService shopCarService;

    // 购物车新增物品
    @PostMapping("/add")
    public boolean add(@RequestBody ShopCarSearch search) {
    
    
        return shopCarService.add(search.getUsername(), search.getGoodsId(), search.getNums());
    }

    // 购物车 添加商品的个数
    @PostMapping("/addCount")
    public boolean addCount(@RequestBody ShopCarSearch search) {
    
    
        return shopCarService.addCount(search.getUsername(), search.getGoodsId(), search.getNums());
    }

    // 查看用户购物车有多少商品
    @PostMapping("/selectTotalCountByUser")
    public int selectTotalCountByUser(@RequestBody ShopCarSearch search) {
    
    

        return shopCarService.selectTotalCountByUser(search.getUsername());
    }

    ;

    // 删除商品
    @PostMapping("/deleteGoodsByGoodsId")
    public boolean deleteGoodsByGoodsId(@RequestBody ShopCarSearch search) {
    
    

        return shopCarService.deleteGoodsByGoodsId(search.getUsername(), search.getGoodsId());
    }

    // 查看所有的商品
    @PostMapping("/selectTotalGoods")
    public Map selectTotalGoods(@RequestBody ShopCarSearch search) {
    
    
        ;
        return shopCarService.selectTotalGoods(search.getUsername());
    }
}

package com.shopCar.service;

import java.util.Map;

public interface ShopCarService {
    
    
    //  向购物车中添加商品
    boolean add(String username, String goodsId, String nums);
   //  向购物车中增加该商品的数量
    boolean addCount(String username, String goodsId, String nums);
    // 减-对应商品进行-1 操作
    int decrGoodsCount(String username, String goodsId);
    //  查看商品的总数
    int selectTotalCountByUser(String username);
    // 购物车中删除商品
    boolean deleteGoodsByGoodsId(String username, String goodsId);
    // 获取所有商品
   Map selectTotalGoods(String username);


}


@Service
@Slf4j
public class ShopCarServiceImpl implements ShopCarService {
    
    

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean add(String username, String goodsId, String nums) {
    
    
        redisTemplate.opsForHash().put(username, goodsId, nums);
        return true;
    }

    @Override
    public boolean addCount(String username, String goodsId, String nums) {
    
    

        Long count = redisTemplate.opsForHash().lengthOfValue(username, goodsId);
        if (count <1 ) {
    
    
            log.info("您还没有存入该商品,请先添加");
            return false;
        }
        redisTemplate.opsForHash().increment(username, goodsId, Long.parseLong(nums));
        return true;
    }

    @Override
    public int decrGoodsCount(String username, String goodsId) {
    
    
        Long count = redisTemplate.opsForHash().lengthOfValue(username, goodsId);
        //  判断是否为0 
        if (count < 1) {
    
    
            log.info("您还没有存入该商品,请先添加");
            return 0;
        }
        redisTemplate.opsForHash().increment(username, goodsId, -1);
        return count.intValue() - 1;
    }

    @Override
    public int selectTotalCountByUser(String username) {
    
    
        Long size = redisTemplate.opsForHash().size(username);

        return size.intValue();
    }

    @Override
    public boolean deleteGoodsByGoodsId(String username, String goodsId) {
    
    
        redisTemplate.opsForHash().delete(username, goodsId);
        return true;
    }

    @Override
    public Map selectTotalGoods(String username) {
    
    
        Map entries = redisTemplate.opsForHash().entries(username);


        return entries;
    }
}

`  他 所有的操作都是基于用户层面的 也就是说一个用户分配一个key``

Guess you like

Origin blog.csdn.net/weixin_43689953/article/details/112425774