天天看点

项目总结day21之购物车1 购物车

1 购物车

1.1 业务分析

当用户点击购物车按钮时,应该跳转到购物车列表页面

项目总结day21之购物车1 购物车

页面名称: cart.jsp

页面数据: ${cartList}

1.2 创建购物Cart POJO

package com.jt.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_cart")
@Data
@Accessors(chain = true)
public class Cart extends BasePojo{
   @TableId(type = IdType.AUTO)     //主键自增
   private Long id; //购物车Id号
   private Long userId; //用户Id号
   private Long itemId; //商品id号
   private String itemTitle;    //商品标题
   private String itemImage;    //商品图片信息
   private Long itemPrice;
   private Integer num;
}
           

1.3 创建jt-cart项目

1.3.1 创建项目

项目总结day21之购物车1 购物车

1.3.2 添加继承依赖插件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>jt-cart</artifactId>


    <parent>
        <artifactId>jt2007</artifactId>
        <groupId>com.jt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!--3.依赖工具API-->
    <dependencies>
        <dependency>
            <groupId>com.jt</groupId>
            <artifactId>jt-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <!--4.添加maven插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

1.3.3 购物车项目结构

项目总结day21之购物车1 购物车

1.4 购物车业务实现

1.4.1 购物车列表展现

(1)编辑CartController

package com.jt.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.service.DubboCartService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Reference(check = false)
    private DubboCartService cartService;
    /**
     *  业务描述: 展现购物车列表页面,同时查询购物车数据
     *  url: http://www.jt.com/cart/show.html
     *  参数: userId=7L
     *  返回值:  页面逻辑名称  cart.jsp
     *  页面取值:  ${cartList}
     */
    @RequestMapping("/show")
    public String show(Model model){
        Long userId = 7L;   //暂时写死
        List<Cart> cartList = cartService.findCartListByUserId(userId);
        model.addAttribute("cartList",cartList);
        return "cart";
    }

}
           

(2)编辑CartService

package com.jt.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.CartMapper;
import com.jt.pojo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@Service(timeout = 3000)
public class DubboCartServiceImpl implements DubboCartService{

    @Autowired
    private CartMapper cartMapper;


    @Override
    public List<Cart> findCartListByUserId(Long userId) {
    QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("user_id", userId);
        return cartMapper.selectList(queryWrapper);
    }
}
           

(3)页面效果展示

项目总结day21之购物车1 购物车

1.4.2 购物车数量的修改

(1)页面url分析

项目总结day21之购物车1 购物车

页面JS分析

项目总结day21之购物车1 购物车

(2)编辑CartController

/**
     * 业务描述:
     *  完成购物车数量的修改操作
     *  url地址:  http://www.jt.com/cart/update/num/1474392004/4
     *  参数:     restFul风格
     *  返回值:   void
     */
     @RequestMapping("/update/num/{itemId}/{num}")
     @ResponseBody  //让ajax程序结束
     public void updateNum(Cart cart){//springmvc 针对restFul提供的功能 名称和属性一致

         Long userId = 7L;
         cart.setUserId(userId);
         cartService.updateCartNum(cart);
     }
           

(3)编辑CartService

/**
     * Sql: update tb_cart set num=#{num},updated=#{updated}
     *      where user_id=#{userId} and item_id = #{itemId}
     * @param cart
     */
    @Override
    public void updateCartNum(Cart cart) {
        Cart cartTemp = new Cart();
        cartTemp.setNum(cart.getNum());
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cart.getUserId())
                    .eq("item_id", cart.getItemId());
        cartMapper.update(cartTemp,queryWrapper);

    }
           

1.4.3 购物车删除

(1)页面分析

业务逻辑: 当删除购物车时,应该删除数据库记录,之后将页面重定向到购物车列表页面。

项目总结day21之购物车1 购物车

(2)编辑CartController

/**
     * 实现购物车删除操作
     * 1.url地址: http://www.jt.com/cart/delete/1474392004.html
     * 2.参数:    1474392004 itemId
     * 3.返回值:  String   重定向到列表页面
     */
     @RequestMapping("/delete/{itemId}")
     public String deleteCart(Cart cart){
         Long userId = 7L;
         cart.setUserId(userId);
         cartService.deleteCart(cart);
         return "redirect:/cart/show.html";
     }
           

(3)编辑CartService

@Override
    public void deleteCart(Cart cart) { //userId/itemId

        cartMapper.delete(new QueryWrapper<>(cart));
        //根据对象中不为null的属性当做where条件.
    }
           

1.4.4 购物车新增

(1)业务说明

业务说明: 当购物车点击新增时,需要重定向到购物车列表页面. 完成购物车"新增""

注意事项: 如果用户重复添加购物车.则只做购物车数量的更新,如果购物车没有记录,则新增数据。

项目总结day21之购物车1 购物车

参数接收:

项目总结day21之购物车1 购物车

(2)编辑CartController

/**
     * 购物车新增操作
     * url地址:http://www.jt.com/cart/add/1474391990.html
     * url参数: 购物车属性数据
     * 返回值:  重定向到购物车列表页面
     */
    @RequestMapping("/add/{itemId}")
    public String addCart(Cart cart){
        Long userId = 7L;
        cart.setUserId(userId);
        cartService.addCart(cart);
        return "redirect:/cart/show.html";
    }
           

(3)编辑CartService

/**
     * 如果购物车已存在,则更新数量,否则新增.
     * 如何判断购物车数据是否存在   userId itemId
     *
     * @param cart
     */
    @Override
    public void addCart(Cart cart) {
        //1.查询购物车信息 userId,itemId
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cart.getUserId());
        queryWrapper.eq("item_id",cart.getItemId());
        Cart cartDB = cartMapper.selectOne(queryWrapper);
        if(cartDB == null){
            //第一次新增购物车
            cartMapper.insert(cart);
        }else{
            //用户已经加购,更新数量
            int num = cartDB.getNum() + cart.getNum();
            Cart cartTemp = new Cart();
            cartTemp.setNum(num).setId(cartDB.getId());
            cartMapper.updateById(cartTemp);
        }
    }
           

1.4.5 权限控制

需求: 如果用户不登录,则不允许访问购物车列表页面,如果没有登录则应该重定向到用户登录页面。

(1)拦截器工作原理

项目总结day21之购物车1 购物车

(2)编辑拦截器配置

package com.jt.config;

import com.jt.interceptor.UserInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfigurer implements WebMvcConfigurer{
	
	//开启匹配后缀型配置
	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		
		configurer.setUseSuffixPatternMatch(true);
	}

	//配置拦截器策略
	@Autowired
	private UserInterceptor userInterceptor;
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(userInterceptor)
				.addPathPatterns("/cart/**","/order/**");
	}
}
           

(3)编辑拦截器

说明:通过拦截器动态获取userId

package com.jt.interceptor;
import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import redis.clients.jedis.JedisCluster;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class UserInterceptor implements HandlerInterceptor {

    @Autowired
    private JedisCluster jedisCluster;
    /**
     * 参数介绍:
     * @param1  request  用户请求对象
     * @param2  response 服务器响应对象
     * @param3  handler  当前处理器本身
     * @return  Boolean  true 请求放行    false 请求拦截  一般配合重定向使用
     * @throws Exception
     * 如果用户不登录则重定向到登录页面
     *
     * 需求:   如何判断用户是否登录?
     * 依据:   1.根据cookie    2.判断redis
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ticket = null;
        //1.判断cookie中是否有记录
        Cookie[] cookies = request.getCookies();
        if(cookies !=null && cookies.length>0){
            for (Cookie cookie : cookies){
                if("JT_TICKET".equals(cookie.getName())){
                    ticket = cookie.getValue();
                    break;
                }
            }
        }

        //2.判断cookie数据是否有效
        if(!StringUtils.isEmpty(ticket)){
            if(jedisCluster.exists(ticket)){
                String userJSON = jedisCluster.get(ticket);
                User user = ObjectMapperUtil.toObject(userJSON, User.class);
                //3.利用request对象进行数据的传递    request对象是最为常用的传递参数的媒介.
                request.setAttribute("JT_USER", user);
                return true;    //表示用户已经登录.
            }
        }

        //重定向到用户登录页面
        response.sendRedirect("/user/login.html");
        return false;
    }
}
           

(4)动态获取UserID

项目总结day21之购物车1 购物车

继续阅读