文章目录
- 二. 购物车功能
- 添加购物车
- ShoppingCartController
- ShoppingCartService
- ShoppingCartServiceImpl
- ShoppingCartMapper
- ShoppingCartMapper
- application
- 查看购物车
- ShoppingCartController
- ShoppingCartService
- ShoppingCartServiceImpl
- 清空购物车
- ShoppingCartController
- ShoppingCartService
- ShoppingCartServiceImpl
- 删除购物车中一个商品
- ShoppingCartController
- ShoppingCartService
- ShoppingCartServiceImpl
二. 购物车功能
添加购物车
ShoppingCartController
package com.sky.controller.user;import com.sky.dto.ShoppingCartDTO;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车")
@Slf4j
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;@PostMapping("add")@ApiOperation(value = "添加购物车")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("添加购物车:{}", shoppingCartDTO);shoppingCartService.addShoppingCart(shoppingCartDTO);return Result.success();}
}
ShoppingCartService
package com.sky.service;import com.sky.dto.ShoppingCartDTO;
public interface ShoppingCartService {void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
}
ShoppingCartServiceImpl
package com.sky.service.impl;import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {@Autowiredprivate ShoppingCartMapper shoppingCartMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;@Overridepublic void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);Long currentId = BaseContext.getCurrentId();shoppingCart.setUserId(currentId);List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);if (list != null && !list.isEmpty()) {ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber() + 1);shoppingCartMapper.updateById(cart);}else {Long dishId = shoppingCartDTO.getDishId();if (dishId != null) {Dish dish = dishMapper.selectById(dishId);shoppingCart.setName(dish.getName());shoppingCart.setImage(dish.getImage());shoppingCart.setAmount(dish.getPrice());} else {Long setmealId = shoppingCartDTO.getSetmealId();Setmeal setmeal = setmealMapper.selectById(setmealId);shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());}shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}
}
ShoppingCartMapper
package com.sky.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {List<ShoppingCart> list(ShoppingCart shoppingCart);
}
ShoppingCartMapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.mapper.ShoppingCartMapper"><select id="list" resultType="com.sky.entity.ShoppingCart">SELECT * FROM shopping_cart<where><if test="userId != null">AND user_id = #{userId}</if><if test="setmealId != null">AND setmeal_id = #{setmealId}</if><if test="dishId != null">AND dish_id = #{dishId}</if><if test="dishFlavor != null">AND dish_flavor = #{dishFlavor}</if></where></select>
</mapper>
application
server:port: 8080spring:profiles:active: devmain:allow-circular-references: truedatasource:druid:driver-class-name: ${sky.datasource.driver-class-name}url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=trueusername: ${sky.datasource.username}password: ${sky.datasource.password}redis:host: ${sky.redis.host}post: ${sky.redis.post}database: ${sky.redis.database}
mybatis-plus:configuration:map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: automapper-locations: classpath:mapper/*.xml
logging:level:com:sky:mapper: debugservice: infocontroller: infosky:jwt:admin-secret-key: itcastadmin-ttl: 720000000admin-token-name: tokenuser-secret-key: itheimauser-ttl: 720000000user-token-name: authenticationalioss:endpoint: ${sky.alioss.endpoint}access-key-id: ${sky.alioss.access-key-id}access-key-secret: ${sky.alioss.access-key-secret}bucket-name: ${sky.alioss.bucket-name}wechat:appid: ${sky.wechat.appid}secret: ${sky.wechat.secret}
查看购物车
ShoppingCartController
@GetMapping("list")@ApiOperation(value = "查看购物车")public Result<List<ShoppingCart>> list() {List<ShoppingCart> list = shoppingCartService.showShoppingCart();return Result.success(list);}
ShoppingCartService
List<ShoppingCart> showShoppingCart();
ShoppingCartServiceImpl
@Overridepublic List<ShoppingCart> showShoppingCart() {Long currentId = BaseContext.getCurrentId();ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);return list;}
清空购物车
ShoppingCartController
@DeleteMapping("clean")@ApiOperation(value = "清空购物车")public Result clean() {shoppingCartService.cleanShoppingCart();return Result.success();}
ShoppingCartService
void cleanShoppingCart();
ShoppingCartServiceImpl
@Overridepublic void cleanShoppingCart() {Long currentId = BaseContext.getCurrentId();LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();wrapper.eq(ShoppingCart::getUserId, currentId);shoppingCartMapper.delete(wrapper);}
删除购物车中一个商品
ShoppingCartController
@PostMapping("/sub")public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("删除购物车中一个商品:{}", shoppingCartDTO);shoppingCartService.subShoppingCart(shoppingCartDTO);return Result.success();}
ShoppingCartService
void subShoppingCart(ShoppingCartDTO shoppingCartDTO);
ShoppingCartServiceImpl
@Overridepublic void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);Long currentId = BaseContext.getCurrentId();shoppingCart.setUserId(currentId);List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);if (list != null && !list.isEmpty()) {ShoppingCart cart = list.get(0);if (cart.getNumber() > 1) {cart.setNumber(cart.getNumber() - 1);shoppingCartMapper.updateById(cart);} else {shoppingCartMapper.deleteById(cart.getId());}}}