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

你可能感兴趣的文章
Netty工作笔记0020---Selectionkey在NIO体系
查看>>
Vue踩坑笔记 - 关于vue静态资源引入的问题
查看>>
Netty工作笔记0025---SocketChannel API
查看>>
Netty工作笔记0027---NIO 网络编程应用--群聊系统2--服务器编写2
查看>>
Netty工作笔记0050---Netty核心模块1
查看>>
Netty工作笔记0060---Tcp长连接和短连接_Http长连接和短连接_UDP长连接和短连接
查看>>
Netty工作笔记0077---handler链调用机制实例4
查看>>
Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
查看>>
Netty常见组件二
查看>>
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty核心模块组件
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>