1. 購物車業務實作
1.1 購物車串列展現
1.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";
}
}
1.1.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);
}
}
1.1.3 頁面效果展現

1.2 購物車數量的修改
1.2.1 頁面URL分析
1.頁面URL分析

2.商品的引數 在url地址中 RESTFul風格.
3.頁面JS分析

1.2.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);
}
1.2.2 編輯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.3 購物車洗掉
1.3.1 頁面分析
業務邏輯: 當洗掉購物車時,應該洗掉資料庫記錄,之后將頁面重定向到購物車串列頁面.

1.3.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";
}
1.3.3 編輯CartService
@Override
public void deleteCart(Cart cart) { //userId/itemId
cartMapper.delete(new QueryWrapper<>(cart));
//根據物件中不為null的屬性當做where條件.
}
1.4 購物車新增
1.4.1 業務說明
業務說明: 當購物車點擊新增時,需要重定向到購物車串列頁面. 完成購物車"新增""
注意事項: 如果用戶重復添加購物車.則只做購物車數量的更新,如果購物車沒有記錄,則新增資料.

2).引數接收

1.4.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";
}
1.4.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.5 權限控制
需求: 如果用戶不登錄,則不允許訪問購物車串列頁面,如果沒有登錄則應該重定向到用戶登錄頁面.
1.5.1 攔截器作業原理

1.5.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/**");
}
}
1.5.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;
}
}
未完待續 后邊補吧!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/225896.html
標籤:其他
下一篇:極客日報第 13 期:因未發專案獎金,一名程式員決定刪代碼泄憤;2020年最常用密碼TOP200出爐,你都用過嗎?
