当前位置: 首页 > wzjs >正文

网站源码可以做淘宝客深圳百度seo代理

网站源码可以做淘宝客,深圳百度seo代理,安全的营销型网站制作,网站建设与管理升学就业方向一.查看购物车 查看购物车使用get请求。我们要查看当前用户的购物车,就要获取当前用户的userId字段进行条件查询。因为在用户登录时就已经将userId封装在token中了,因此我们只需要解析token获取userId即可,不需要前端再传入参数了。 Control…

一.查看购物车

查看购物车使用get请求。我们要查看当前用户的购物车,就要获取当前用户的userId字段进行条件查询。因为在用户登录时就已经将userId封装在token中了,因此我们只需要解析token获取userId即可,不需要前端再传入参数了。

Controller层

package com.sky.controller.user;import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
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.*;import java.util.List;@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "购物车相关接口")
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;/*** 添加购物车* @param shoppingCartDTO* @return*/@ApiOperation("添加购物车")@PostMapping("/add")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("向购物车中添加菜品或套餐:{}",shoppingCartDTO);shoppingCartService.add(shoppingCartDTO);return Result.success();}/*** 查看购物车* @return*/@GetMapping("/list")@ApiOperation("查看购物车")public Result<List<ShoppingCart>> list() {log.info("查看购物车:{}", BaseContext.getCurrentId());List<ShoppingCart> list = shoppingCartService.showShoppingCart();return Result.success(list);}
}

返回一个含有当前用户所有购物车信息的列表,列表的元素为购物车对象ShoppingCart。

Service层

接口

package com.sky.service;import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import org.springframework.stereotype.Service;import java.util.List;@Service
public interface ShoppingCartService {/*** 添加购物车* @param shoppingCartDTO*/void add(ShoppingCartDTO shoppingCartDTO);/*** 查查看购物车* @return*/List<ShoppingCart> showShoppingCart();
}

实现类

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 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
public class ShoppingCartServiceImpl implements ShoppingCartService {@Autowiredprivate ShoppingCartMapper shoppingCartMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 添加购物车* @param shoppingCartDTO*/@Overridepublic void add(ShoppingCartDTO shoppingCartDTO) {// 首先判断这次添加购物车的操作加入的菜品或套餐是否已经存在,如果存在就把份数+1,如果不存在就新增ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);Long userId = BaseContext.getCurrentId();shoppingCart.setUserId(userId);// 1.首先查询该菜品或套餐在数据库中是否存在List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);    // 每次添加的要么是菜品,要么是套餐。且如果重复添加只会增加份数而不会新增一条数据,因此每次查询要么为空,要么查询出1条数据if (list != null && list.size() > 0) {      // 已存在,数量+1ShoppingCart cart = list.get(0);    // 将已存在的购物车对象取出cart.setNumber(cart.getNumber() + 1);   // 并将其菜品/套餐数量+1shoppingCartMapper.updateNumberById(cart);      // 通过id更新} else {// 2.不存在,先判断是套餐还是菜品,因为套餐和菜品在购物车中所需要的属性是不一样的Long dishId = shoppingCartDTO.getDishId();if (dishId != null) {// 3.如果是菜品,那么从菜品数据库中查找并将对应属性赋值给购物车对象Dish dish = dishMapper.getById(dishId);shoppingCart.setName(dish.getName());shoppingCart.setImage(dish.getImage());shoppingCart.setAmount(dish.getPrice());} else {// 4.如果是套餐,那么从套餐数据库中查找并将对应属性赋值给购物车对象Long setmealId = shoppingCartDTO.getSetmealId();Setmeal setmeal = setmealMapper.getById(setmealId);shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());}// 5.将新增的菜品/套餐加入数据库中shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}/*** 查看购物车* @return*/@Overridepublic List<ShoppingCart> showShoppingCart() {Long userId = BaseContext.getCurrentId();ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);return list;}
}

我们要根据userId来筛选要查看哪个用户的购物车,因此要通过token令牌获得userId,然后构建shoppingCart对象并将该属性赋值,最后通过数据库进行查找符合该用户的购物车列表。

Mapper层

接口

package com.sky.mapper;import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;import java.util.List;@Mapper
public interface ShoppingCartMapper {/*** 查询菜品/套餐是否存在* @param shoppingCart* @return*/List<ShoppingCart> list(ShoppingCart shoppingCart);/*** 更新购物车中套餐/菜品份数* @param shoppingCart*/@Update("update shopping_cart set number = #{number} where id = #{id}")void updateNumberById(ShoppingCart shoppingCart);/*** 向购物车中加入菜品/套餐* @param shoppingCart*/@Insert("insert into shopping_cart(name, image, user_id, dish_id, setmeal_id, dish_flavor, number, amount, create_time) " +"VALUES (#{name}, #{image}, #{userId}, #{dishId}, #{setmealId}, #{dishFlavor}, #{number}, #{amount},#{createTime})")void insert(ShoppingCart shoppingCart);
}

XML映射文件

只有userId,根据userId查询即可。

<?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="dishId != null">and dish_id = #{dishId}</if><if test="setmealId != null">and setmeal_id = #{setmealId}</if><if test="dishFlavor != null">and dish_flavor = #{dishFlavor}</if></where></select>
</mapper>

成功 

http://www.dtcms.com/wzjs/408777.html

相关文章:

  • wordpress企业站手机客户端怎么宣传自己的产品
  • 濮阳网吧北京网站优化实战
  • 访问网站人多的时候很慢是服务器问题还是带宽深圳网络推广市场
  • 兰州网络推广关键词网站优化要做哪些
  • 网站模板图链接买卖平台
  • 建设网站都需要哪些内容大数据精准营销系统
  • 甘肃兰州网站建设店铺运营方案策划
  • 建设银行网站如何查询开户行电商培训学校
  • 大浪网站建设注册平台
  • 安仁网站制作成都抖音seo
  • 网站建设是 口号竞价推广账户托管费用
  • 第一次做网站没头绪商务软文写作
  • html5网站建设重庆seo薪酬水平
  • 免费创建网站软件如何做个网站推广自己产品
  • 小公司建网站 优帮云今日时事新闻
  • 网站客服弹窗代码什么是互联网营销师
  • 宁波seo深度优化平台有哪些衡阳有实力seo优化
  • 网站转让 备案万网域名注册查询
  • 中铁建设集团门户网登陆宁波seo关键词优化方法
  • wordpress移动端设置方法网页seo是什么意思
  • 广州企业建设网站搭建网站多少钱
  • 网站 外包 版权西安优化seo
  • 网站策划案需要包括哪些2021年近期舆情热点话题
  • 同ip网站怎么做成都公司建站模板
  • 做企业网站的供应商成都网站seo推广
  • 网站建设应急处置方案seo及网络推广招聘
  • 营销咨询公司经营范围站内关键词排名优化软件
  • 网站必须做301重定向吗廊坊seo排名公司
  • 网络营销常用的方法包括seo顾问咨询
  • 网站详情页怎么做企业网站推广方案设计毕业设计