CGB2005-京淘19

1.用户模块

1.1 用户登出操作

1.1.1 编辑UserController

/**
     * 实现用户的登出操作  要求删除cookie 和redis中的数据(key)
     * 步骤: 通过cookie获取ticket信息.
     * url: http://www.jt.com/user/logout.html
     * 参数: 暂时没有
     * 返回值: 重定向到系统首页
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
    
    

        Cookie[] cookies = request.getCookies();
        if(cookies !=null && cookies.length >0){
    
    
            for(Cookie cookie : cookies){
    
    
                if("JT_TICKET".equals(cookie.getName())){
    
    
                    //获取value之后删除cookie
                    String ticket = cookie.getValue();
                    jedisCluster.del(ticket);   //删除redis中的数据
                    //删除cookie时 必须与原来的cookie数据保持一致
                    cookie.setDomain("jt.com");
                    cookie.setPath("/");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }
        }

        return "redirect:/";
    }

1.2 Cookie优化

package com.jt.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    
    

    /**
     * 该工具API主要的任务
     *      1.根据cookie的名称 返回cookie对象
     *      2.根据cookie的名称 返回valve的值
     *      3.新增cookie方法
     *      4.删除cookie方法
     */
    public static Cookie getCookie(String cookieName, HttpServletRequest request){
    
    
        Cookie[] cookies = request.getCookies();
        if(cookies !=null && cookies.length >0) {
    
    
            for (Cookie cookie : cookies) {
    
    
                if (cookieName.equals(cookie.getName())) {
    
    
                    return cookie;
                }
            }
        }
        return null ;
    }

    public static String getCookieValue(String cookieName,HttpServletRequest request){
    
    
        Cookie cookie = getCookie(cookieName, request);
        return cookie ==null?null:cookie.getValue();
    }

    public static void addCookie(String cookieName, String cookieValue, String path,
                                 String domain, int maxAge, HttpServletResponse response){
    
    
        Cookie cookie = new Cookie(cookieName,cookieValue);
        cookie.setPath(path);
        cookie.setDomain(domain);
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

    public static void deleteCookie(String cookieName,String path,
                                    String domain,HttpServletResponse response){
    
    

        addCookie(cookieName,"",path, domain, 0, response);
    }

}

1.3重构用户登出操作

 /**
     * 实现用户的登出操作  要求删除cookie 和redis中的数据(key)
     * 步骤: 通过cookie获取ticket信息.
     * url: http://www.jt.com/user/logout.html
     * 参数: 暂时没有
     * 返回值: 重定向到系统首页
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
    
    
        String ticket = CookieUtil.getCookieValue("JT_TICKET", request);
        if(!StringUtils.isEmpty(ticket)){
    
    
            //1.删除redis
            jedisCluster.del(ticket);
            //2.删除cookie
            CookieUtil.deleteCookie("JT_TICKET", "/", "jt.com", response);
        }
        return "redirect:/";

       /* Cookie[] cookies = request.getCookies();
        if(cookies !=null && cookies.length >0){
            for(Cookie cookie : cookies){
                if("JT_TICKET".equals(cookie.getName())){
                    //获取value之后删除cookie
                    String ticket = cookie.getValue();
                    jedisCluster.del(ticket);   //删除redis中的数据
                    //删除cookie时 必须与原来的cookie数据保持一致
                    cookie.setDomain("jt.com");
                    cookie.setPath("/");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }
        }*/
    }

2.购物车模块实现

2.1 创建jt-cart项目

2.1.1 新建项目

在这里插入图片描述

2.1.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>jt</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
    <!--添加依赖项-->
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>jt-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

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

</project>

2.1.3 编辑POJO

@TableName("tb_cart")
@Data
@Accessors(chain = true)
public class Cart extends BasePojo{
    
    

    @TableId(type = IdType.AUTO)
    private Long id;            //主键自增
    private Long userId;        //用户id
    private Long itemId;        //商品id
    private String itemTitle;
    private String itemImage;
    private Long itemPrice;
    private Integer num;
}

2.1.4 编辑Cart代码结构

在这里插入图片描述

2.1.5 编辑YML配置文件

server:
  port: 8094
  servlet:
    context-path: /
spring:
  datasource:
    #引入druid数据源
    #type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

  #提供了MVC的支持
  mvc:
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp

#mybatis-plush配置
mybatis-plus:
  type-aliases-package: com.jt.pojo
  mapper-locations: classpath:/mybatis/mappers/*.xml
  configuration:
    map-underscore-to-camel-case: true

logging:
  level: 
    com.jt.mapper: debug

#关于Dubbo配置
dubbo:
  scan:
    basePackages: com.jt    #指定dubbo的包路径
  application:              #应用名称
    name: provider-cart     #一个接口对应一个服务名称
  registry:                 #zk集群 主机中的信息与从机中的信息一致的 从zk中获取数据的时候链接的从机 主机的作用就是监控集群
    address: zookeeper://192.168.126.129:2181?backup=192.168.126.129:2182,192.168.126.129:2183
  protocol:  #指定协议
    name: dubbo  #使用dubbo协议(tcp-ip)  web-controller直接调用sso-Service
    port: 20881  #每一个服务都有自己特定的端口 不能重复.

2.2 购物车列表展现

2.2.1 页面分析

1). 页面url分析
在这里插入图片描述
2).业务说明
当用户点击购物车按钮时,应该根据userId查询购物车数据信息,之后在cart.jsp中展现列表信息.
在这里插入图片描述

2.2.2 编辑CartController

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

    @Reference(check = false,timeout = 3000)
    private DubboCartService cartService;

    /**
     * 1.购物车列表数据展现
     * url地址: http://www.jt.com/cart/show.html
     * 参数:    暂时没有
     * 返回值:  页面逻辑名称  cart.jsp
     * 页面取值: ${cartList}
     * 应该将数据添加到域对象中 Request域  model工具API操作request对象
     *
     */
    @RequestMapping("/show")
    public String show(Model model){
    
    
        //1.暂时将userId写死    7L
        Long userId = 7L;
        List<Cart> cartList = cartService.findCartListByUserId(userId);
        model.addAttribute("cartList",cartList);
        return "cart";
    }
}

2.2.2 编辑CartService

package com.jt.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.Query;
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
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);
    }
}

2.2.3 页面效果展现

在这里插入图片描述

作业:

1.完成购物车修改
2.完成购物车删除

猜你喜欢

转载自blog.csdn.net/qq_16804847/article/details/108658932