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 頁面效果展現

2.3 完成購物車數量修改操作
2.3.1 頁面分析

2.3.2 頁面JS分析
$(".increment").click(function(){//+
var _thisInput = $(this).siblings("input");
_thisInput.val(eval(_thisInput.val()) + 1);
$.post("/cart/update/num/"+_thisInput.attr("itemId")+"/"+_thisInput.val(),function(data){
TTCart.refreshTotalPrice();
});
});
2.3.3 編輯CartController
/**
* 業務說明: 完成購物車數量的更新操作
* url地址: http://www.jt.com/cart/update/num/562379/9
* 引數: 暫時沒有
* 回傳值: void
*/
@RequestMapping("/update/num/{itemId}/{num}")
@ResponseBody //ajax結束的識別符號.
public void updateCartNum(Cart cart){ //如果{name} 與屬性的名稱一致,則可以自動的賦值.
Long userId = 7L;
cart.setUserId(userId);
cartService.updateCartNum(cart);
}
2.3.4 編輯CartService
/**
* 更新購物車的數量 num
* @param cart
* Sql: update tb_cart set num = #{num},updated = now()
* where user_id = #{user} and item_id = #{itemId}
*/
@Override
public void updateCartNum(Cart cart) { //itemId,userId
Cart cartTemp = new Cart();
cartTemp.setNum(cart.getNum());
UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("user_id", cart.getUserId())
.eq("item_id", cart.getItemId());
cartMapper.update(cartTemp,updateWrapper);
}
2.4 購物車洗掉操作
2.4.1 頁面分析
說明: 根據itemId和userId洗掉購物車資料.重定向到購物車串列頁面即可.

2.4.2 編輯CartController
/**
* 洗掉購物車資料
* url地址:http://www.jt.com/cart/delete/562379.html
* 引數: 562379
* 回傳值: 購物車串列頁面
*/
@RequestMapping("/delete/{itemId}")
public String deleteCart(Cart cart){
Long userId = 7L;
cart.setUserId(userId);
cartService.deleteCart(cart);
return "redirect:/cart/show.html";
}
2.4.3 編輯CartService
@Override
public void deleteCart(Cart cart) { //itemId/userId
//根據物件中不為null的元素充當where條件
cartMapper.delete(new QueryWrapper<>(cart));
}
2.5 購物車新增操作
2.5.1 需求說明
說明: 如果用戶點擊購入購物車時,應該將用戶的提交資料插入到tb_cart表中,并且重定向到購物車串列頁面.
注意事項: 如果用戶重復提交資料,則應該數量更新.

2).引數說明
<form id="cartForm" method="post">
<input class="text" id="buy-num" name="num" value="1" onkeyup="setAmount.modify('#buy-num');"/>
<input type="hidden" class="text" name="itemTitle" value="${item.title }"/>
<input type="hidden" class="text" name="itemImage" value="${item.images[0]}"/>
<input type="hidden" class="text" name="itemPrice" value="${item.price}"/>
</form>
2.5.2 編輯CartController
/**
* 業務需求: 完成購物車入庫操作
* url地址: http://www.jt.com/cart/add/562379.html
* 引數: form表單提交的資料/itemId
* 回傳值: 重定向到購物車串列頁面
*/
@RequestMapping("/add/{itemId}")
public String addCart(Cart cart){
Long userId = 7L;
cart.setUserId(userId);
cartService.addCart(cart);
return "redirect:/cart/show.html";
}
2.5.3 編輯CartService
/**
* 思路:
* 1.先查詢資料庫 user_id/item_id
* 2.判斷回傳值是否有結果
* true: 只做數量的更新
* false: 直接入庫即可
*/
@Override
public void addCart(Cart cart) {
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{
//更新數量 MP方式
int num = cart.getNum() + cartDB.getNum(); //數量求和
cartDB.setNum(num).setUpdated(new Date());
cartMapper.updateCartNum(cartDB);
}
}
2.5.4 編輯CartMapper
public interface CartMapper extends BaseMapper<Cart> {
@Update("update tb_cart set num=#{num},updated=#{updated} where user_id=#{userId} and item_id =#{itemId}")
void updateCartNum(Cart cartDB);
}
3 商品詳情展現
3.1 將jt-manage改造為Dubbo專案
3.1.1 編輯DubboItemServiceImpl

3.1.2 編輯YML組態檔
說明: 添加dubbo的配置即可

3.2 實作商品詳情展現
3.2.1 頁面url分析
說明:當用戶點擊某個商品時,需要跳轉到商品的詳情頁面中.根據itemId展現商品資訊.并且在item.jsp中展現資料.

3.2.2 頁面取值問題

3.2.3 編輯ItemController
@Controller
public class ItemController {
@Reference(check = false,timeout = 3000)
private DubboItemService itemService;
/**
* 業務: 根據itemId查詢商品資訊(item/itemDesc)
* url地址: http://www.jt.com/items/562379.html
* 引數: 注意restFul風格即可
* 回傳值: 頁面邏輯名稱 item.jsp
* 頁面取值: ${item.title }/${itemDesc.itemDesc }
* 5分鐘完成.
* */
@RequestMapping("/items/{itemId}")
public String findItemById(@PathVariable Long itemId, Model model){
Item item = itemService.findItemById(itemId);
ItemDesc itemDesc = itemService.findItemDescById(itemId);
model.addAttribute("item", item);
model.addAttribute("itemDesc", itemDesc);
return "item";
}
}
3.2.4 編輯ItemService
package com.jt.web.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.service.DubboItemService;
import org.springframework.beans.factory.annotation.Autowired;
@Service //dubbo的注解
public class DubboItemServiceImpl implements DubboItemService {
@Autowired
private ItemMapper itemMapper; //商品資訊
@Autowired
private ItemDescMapper itemDescMapper; //商品詳情資訊.
@Override
public Item findItemById(Long itemId) {
return itemMapper.selectById(itemId);
}
@Override
public ItemDesc findItemDescById(Long itemId) {
return itemDescMapper.selectById(itemId);
}
}
3.2.5 頁面展現

作業
用戶沒有登錄的條件下不允許訪問購物車/訂單等敏感業務.跳轉到登錄頁面 -----攔截器實作 HandlerInterceptor
利用攔截器的機制 要求動態的獲取userId,之后將引數傳遞給Controller中. cookie—>ticket---->userJSON---->userId
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/83856.html
標籤:其他
