前置准备:
Redis (高性能)+MongoDB(海量数据)+Elasticsearch/Hbase(大数据数据库)
pom引入:实体类:org.springframework.boot spring-boot-starter-parent2.1.7.RELEASE com.mook shoppingmall0.0.1-SNAPSHOT shoppingmall Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest mysql mysql-connector-javaorg.mybatis.spring.boot mybatis-spring-boot-starter2.1.0 com.github.pagehelper pagehelper-spring-boot-starter1.2.13 org.projectlombok lombokcn.springboot best-pay-sdk1.3.0 org.springframework.boot spring-boot-starter-freemarkerorg.springframework.boot spring-boot-starter-data-rediscom.google.code.gson gson
表单数据:
//用于接收前端参数 @Data public class CarAddForm { @NotNull private Integer productId; private Boolean selected = true; }
pojo类:
//购物车需要参数,具体数量需要根据实际情况查询数据库,所以用不到 @Data @AllArgsConstructor @NoArgsConstructor public class Car { private Integer productId; private Integer quantity; private Boolean productSelected; }
返回Vo:
//购物车类 @Data public class CarVo { private ListcarProductVoList; private Boolean selectedAll; private BigDecimal carTotalPrice; private Integer carTotalQuantity; }
//购物车商品类 @Data public class CarProductVo { private Integer productId; private Integer quantity; private String productName; private String productSubtitle; private String productMainImage; private String detail; private String subImage; private BigDecimal productPrice; private BigDecimal productTotalPrice; private Integer productStock; private Integer productStatus; private Boolean productSelected; }Controller层:
@RestController public class CarController { @Autowired private ICarService carService; @PostMapping("/car") public ResponseVo参数异常捕获:add(@Valid @RequestBody CarAddForm carAddForm){ //注意这里参数校验异常已经在RuntimeException.notValidExceptionHandle中捕获,不用在做参数校验 return carService.add(1,carAddForm); } }
@ControllerAdvice public class RunTimeExceptionHandler { @ExceptionHandler(RuntimeException.class) @ResponseBody @ResponseStatus(HttpStatus.FORBIDDEN) //设置相应状态码 public ResponseVo handle(RuntimeException e) { return ResponseVo.error(ResponseEnum.MASHINE_ERROR, e.getMessage()); } //捕获自定义异常 @ExceptionHandler(UserLoginException.class) @ResponseBody public ResponseVo userLoginHandle(){ return ResponseVo.error(ResponseEnum.FAIL_NON_LOGIN); } //捕获参数不能为null异常 @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public ResponseVo notValidExceptionHandle(MethodArgumentNotValidException e){ String msg = e.getBindingResult().getFieldError().getField() + e.getBindingResult().getFieldError().getDefaultMessage(); return ResponseVo.error(ResponseEnum.PARAMS_ERROR,msg); } }service层:
注意:查询时数量等商品变化信息要实时从数据库中获取最新值。
@Service public class ICarService { private final static String CAR_REDIS_KEY_TEMPLATE = "car_%d"; @Autowired private StringRedisTemplate redisTemplate; @Autowired private IProductMapper productMapper; private Gson gson = new Gson(); public ResponseVoadd(Integer uid, CarAddForm carAddForm) { Integer quantity = 1; ProductDetailVo product = productMapper.selectByPrimaryKey(carAddForm.getProductId()); //商品是否存在 if (product == null) { return ResponseVo.error(ResponseEnum.PRODUCT_NON); } //商品是否正常在售 if (!product.getStatus().equals(ProductStatusEnum.ON_SALE.getI())) { return ResponseVo.error(ResponseEnum.PRODUCT_NON); } //商品库存是否充足 if (product.getStock() <= 0) { return ResponseVo.error(ResponseEnum.PRODUCT_NON); ; } //写入到Redis , 使用gson进行序列化 HashOperations opsForHash = redisTemplate.opsForHash(); //先要从redis中读取数量 String redisKey = String.format(CAR_REDIS_KEY_TEMPLATE, uid); String value = opsForHash.get(redisKey, String.valueOf(product.getId())); Car car; if (StringUtils.isEmpty(value)) { //没有该商品,新增 car = new Car(product.getId(), quantity, carAddForm.getSelected()); } else { //有商品,数量+1 car = gson.fromJson(value, Car.class); car.setQuantity(car.getQuantity() + quantity); } //list需要遍历,所以不用list,用hash进行存储 //redisTemplate.opsForValue().set(String.format(CAR_REDIS_KEY_TEMPLATE,uid),gson.toJson(new Car(product.getId(),quantity,carAddForm.getSelected()))); opsForHash.put(String.format(CAR_REDIS_KEY_TEMPLATE, uid), String.valueOf(product.getId()), gson.toJson(car)); return null; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)