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

你可能感兴趣的文章
NVIDIA-cuda-cudnn下载地址
查看>>
nvidia-htop 使用教程
查看>>
nvidia-smi 参数详解
查看>>
Nvidia驱动失效,采用官方的方法重装更快
查看>>
nvmw安装node-v4.0.0之后版本的临时解决办法
查看>>
nvm切换node版本
查看>>
nvm安装以后,node -v npm 等命令提示不是内部或外部命令 node多版本控制管理 node多版本随意切换
查看>>
ny540 奇怪的排序 简单题
查看>>
NYOJ 1066 CO-PRIME(数论)
查看>>
nyoj------203三国志
查看>>
nyoj58 最少步数
查看>>
OAuth2 + Gateway统一认证一步步实现(公司项目能直接使用),密码模式&授权码模式
查看>>
OAuth2 Provider 项目常见问题解决方案
查看>>
OAuth2 vs JWT,到底怎么选?
查看>>
Vue.js 学习总结(14)—— Vue3 为什么推荐使用 ref 而不是 reactive
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
查看>>
OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
查看>>
OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
查看>>