博客
关于我
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/

你可能感兴趣的文章
Nodejs+Express+Mysql实现简单用户管理增删改查
查看>>
nodejs+nginx获取真实ip
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs与javascript中的aes加密
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
nodejs中express的使用
查看>>
Nodejs中的fs模块的使用
查看>>
NodeJS使用淘宝npm镜像站的各种姿势
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs在Liunx上的部署生产方式-PM2
查看>>
nodejs字符与字节之间的转换
查看>>
NodeJs学习笔记001--npm换源
查看>>
NodeJs学习笔记002--npm常用命令详解
查看>>
nodejs学习笔记一——nodejs安装
查看>>
NodeJS实现跨域的方法( 4种 )
查看>>