博客
关于我
springboot项目实战---Redis购物车
阅读量:368 次
发布时间:2019-03-04

本文共 6564 字,大约阅读时间需要 21 分钟。

Redis???????

????????????????????????????????????????????????????????????????????????Redis?????????Redis?????????

Redis????

???????CartPrefix???????????????????????

public class CartPrefix extends BasePrefix {    public CartPrefix(int expireSeconds, String prefix) {        super(expireSeconds, prefix);    }}
public static CartPrefix getCartList = new CartPrefix(0, "cart");

???????

????????

@Servicepublic class CartServiceImpl implements CartService {    @Autowired    private RedisService redisService;    @Autowired    private ProductInfoDao productInfoDao;    @Override    public int addCart(String userId, String productId, int num) {        // ???????        Boolean exists = redisService.existsValue(CartPrefix.getCartList, userId, productId);        if (exists) {            // ??????            String json = redisService.hget(CartPrefix.getCartList, userId, productId);            if (json != null) {                CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json), CartDto.class);                cartDto.setProductNum(cartDto.getProductNum() + num);                redisService.hset(CartPrefix.getCartList, userId, productId, JSON.toJSON(cartDto).toString());            } else {                return 0;            }            return 1;        }        // ??????        ProductInfo productInfo = productInfoDao.findProductById(productId);        if (productInfo == null) {            return 0;        }        // ???????        CartDto cartDto = new CartDto();        cartDto.setProductId(productId);        cartDto.setProductName(productInfo.getProductName());        cartDto.setProductIcon(productInfo.getProductIcon());        cartDto.setProductPrice(productInfo.getProductPrice());        cartDto.setProductStatus(productInfo.getProductStatus());        cartDto.setProductNum(num);        cartDto.setCheck("1");        redisService.hset(CartPrefix.getCartList, userId, productId, JSON.toJSON(cartDto).toString());        return 1;    }

???????

@Overridepublic List
getCartList(String userId) { List
jsonList = redisService.hvals(CartPrefix.getCartList, userId); List
cartDtoList = new LinkedList<>(); for (String json : jsonList) { CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json), CartDto.class); cartDtoList.add(cartDto); } return cartDtoList;}

??????

@Overridepublic int updateCartNum(String userId, String productId, int num) {    String json = redisService.hget(CartPrefix.getCartList, userId, productId);    if (json == null) {        return 0;    }    CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json), CartDto.class);    cartDto.setProductNum(num);    redisService.hset(CartPrefix.getCartList, userId, productId, JSON.toJSON(cartDto).toString());    return 1;}

????

@Overridepublic int checkAll(String userId, String checked) {    List
jsonList = redisService.hvals(CartPrefix.getCartList, userId); for (String json : jsonList) { CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json), CartDto.class); if ("true".equals(checked)) { cartDto.setCheck("1"); } else if ("false".equals(checked)) { cartDto.setCheck("0"); } else { return 0; } redisService.hset(CartPrefix.getCartList, userId, cartDto.getProductId(), JSON.toJSON(cartDto).toString()); } return 1;}

????

@Overridepublic int delCartProduct(String userId, String productId) {    redisService.hdel(CartPrefix.getCartList, userId, productId);    return 1;}

?????

@Overridepublic int delCart(String userId) {    redisService.delete(CartPrefix.getCartList, userId);    return 1;}

?????

?????

@RestController@RequestMapping("/cart")public class CartController {    @Autowired    private CartService cartService;    @PostMapping("/add")    @Authorization    public Object addCart(@RequestBody Map
reqMap, @CurrentUser User user) { String userId = RequestUtil.getMapString(user.getId()); String productId = RequestUtil.getMapString(reqMap.get("product_id").toString()); String numString = RequestUtil.getMapString(reqMap.get("product_num").toString()); Integer num = Integer.parseInt(numString); int effectNum = cartService.addCart(userId, productId, num); if (effectNum <= 0) { return ResultUtil.fail(ResultEnum.ADD_CART_ERROR); } return ResultUtil.ok(ResultEnum.ADD_CART_SUCCESS.getMessage()); }

???????

@GetMapping(value = "/getCartList")@Authorizationpublic Object getCartList(@CurrentUser User user) {    String userId = RequestUtil.getMapString(user.getId());    List
cartDtoList = cartService.getCartList(userId); return ResultUtil.ok(cartDtoList);}

??????

@PostMapping(value = "/updateCartNum")@Authorizationpublic Object updateCartNum(@RequestBody Map
reqMap, @CurrentUser User user) { String userId = RequestUtil.getMapString(user.getId()); String productId = RequestUtil.getMapString(reqMap.get("product_id").toString()); String numString = RequestUtil.getMapString(reqMap.get("product_num").toString()); Integer num = Integer.parseInt(numString); int effectNum = cartService.updateCartNum(userId, productId, num); if (effectNum <= 0) { return ResultUtil.fail(); } return ResultUtil.ok();}

????

@PostMapping(value = "/checkAll")@Authorizationpublic Object checkAll(@RequestBody Map
reqMap, @CurrentUser User user) { String userId = RequestUtil.getMapString(user.getId()); String check = RequestUtil.getMapString(reqMap.get("check").toString()); int effectNum = cartService.checkAll(userId, check); if (effectNum <= 0) { return ResultUtil.fail(); } return ResultUtil.ok();}

????

@PostMapping("/delCartProduct")@Authorizationpublic Object delCartProduct(@RequestBody Map
reqMap, @CurrentUser User user) { String userId = RequestUtil.getMapString(user.getId()); String productId = RequestUtil.getMapString(reqMap.get("product_id").toString()); int effectNum = cartService.delCartProduct(userId, productId); if (effectNum <= 0) { return ResultUtil.fail(); } return ResultUtil.ok();}

?????

@PostMapping("/delCart")@Authorizationpublic Object delCart(@CurrentUser User user) {    String userId = RequestUtil.getMapString(user.getId());    int effectNum = cartService.delCart(userId);    if (effectNum <= 0) {        return ResultUtil.fail();    }    return ResultUtil.ok();}

????

?????https://github.com/627886474/sneaker

?????????????Issue??????????

转载地址:http://qwve.baihongyu.com/

你可能感兴趣的文章
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>