博客
关于我
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生成多层目录和生成文件的通用方法
查看>>
nodejs端口被占用原因及解决方案
查看>>
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
nodejs系列之Koa2
查看>>
Nodejs连接mysql
查看>>
nodejs连接mysql
查看>>
NodeJs连接Oracle数据库
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
Nodemon 深入解析与使用
查看>>
node不是内部命令时配置node环境变量
查看>>
node中fs模块之文件操作
查看>>
Node中同步与异步的方式读取文件
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node中自启动工具supervisor的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
Node响应中文时解决乱码问题
查看>>
node基础(二)_模块以及处理乱码问题
查看>>