文章目录
需求分析
数据模型
代码开发
搭建框架
实体类ShoppingCart.java
package com.taotao.reggie.entity;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 购物车
*/
@Data
public class ShoppingCart implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//名称
private String name;
//用户id
private Long userId;
//菜品id
private Long dishId;
//套餐id
private Long setmealId;
//口味
private String dishFlavor;
//数量
private Integer number;
//金额
private BigDecimal amount;
//图片
private String image;
private LocalDateTime createTime;
}
Mapper接口ShoppingCartMapper.java
package com.taotao.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.reggie.entity.ShoppingCart;
import org.apache.ibatis.annotations.Mapper;
/**
* create by 刘鸿涛
* 2022/9/29 11:19
*/
@SuppressWarnings({"all"})
@Mapper
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {
}
业务层接口ShoppingCartService.java
package com.taotao.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.reggie.entity.ShoppingCart;
/**
* create by 刘鸿涛
* 2022/9/29 11:21
*/
@SuppressWarnings({"all"})
public interface ShoppingCartService extends IService<ShoppingCart> {
}
业务层实现类ShoppingCartServiceImpl.java
package com.taotao.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.entity.ShoppingCart;
import com.taotao.reggie.mapper.ShoppingCartMapper;
import com.taotao.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* create by 刘鸿涛
* 2022/9/29 11:23
*/
@SuppressWarnings({"all"})
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
}
控制层ShoppingCartController.java
package com.taotao.reggie.controller;
import com.taotao.reggie.entity.ShoppingCart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/9/29 11:25
*/
@SuppressWarnings({"all"})
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
@Autowired
private ShoppingCartController shoppingCartController;
}
编写Controoler方法
package com.taotao.reggie.controller;
import com.taotao.reggie.common.R;
import com.taotao.reggie.entity.ShoppingCart;
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;
/**
* create by 刘鸿涛
* 2022/9/29 11:25
*/
@SuppressWarnings({"all"})
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
@Autowired
private ShoppingCartController shoppingCartController;
/**
* 添加购物车
* @param shoppingCart
* @return
*/
@PostMapping("/add")
public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
log.info("购物车数据:{}",shoppingCart);
return null;
}
}
验证页面
继续分析
查看数据库发现“number”属性,此属性代表dish_id相同的物品的数量,所谓点了“几份”
原子类
老师因为这个“购物车添加物品时,相同数量+1功能,扩展了一个原子类”,此类不受线程影响,对处理此功能更加方便。
课程中使用了number属性,如果此物品id被添加过,那么就让number + 1,否则此物品第一次添加默认为1,但是实际开发并不这样用
——乐观锁——在修改数据前会判断此数据有没有被修改过
完善ShoppingCartController.java方法
package com.taotao.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.taotao.reggie.common.BaseContext;
import com.taotao.reggie.common.R;
import com.taotao.reggie.entity.ShoppingCart;
import com.taotao.reggie.service.ShoppingCartService;
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;
import java.time.LocalDateTime;
/**
* create by 刘鸿涛
* 2022/9/29 11:25
*/
@SuppressWarnings({"all"})
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
@Autowired
private ShoppingCartService shoppingCartService;
/**
* 添加购物车
* @param shoppingCart
* @return
*/
@PostMapping("/add")
public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
log.info("购物车数据:{}",shoppingCart);
//设置用户id,指定当前是哪个用户的购物车数据
Long currentId = BaseContext.getCurrentId();
shoppingCart.setUserId(currentId);
//查询当前菜品或者套餐是否在购物车中
Long dishId = shoppingCart.getDishId();
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//where ShoppingCart.userId = currentId
//eq方法目的是动态查询
queryWrapper.eq(ShoppingCart::getUserId,currentId);
if(dishId != null){
//添加到购物车的是菜品
//where art.getUserId = currentId
queryWrapper.eq(ShoppingCart::getDishId,dishId);
}else{
//添加到购物车的是套餐
queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
}
//SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
if(cartServiceOne != null){
//如果已经存在,就在原来的数量基础上加一
Integer number = cartServiceOne.getNumber();
cartServiceOne.setNumber(number + 1);
shoppingCartService.updateById(cartServiceOne);
}else{
//如果不存在,则添加到购物车,数量默认就是一
shoppingCart.setNumber(1);
shoppingCart.setCreateTime(LocalDateTime.now());
shoppingCartService.save(shoppingCart);
cartServiceOne = shoppingCart;
}
return R.success(cartServiceOne);
}
}
功能测试
尝试添加“菜品”数据,数据库更新成功
尝试添加"套餐数据"