購物車刪除操做
1.1頁面分析
![image image](http://static.javashuo.com/static/loading.gif)
1.2編輯CartController
/**
* 購物車刪除操做
* url地址: http://www.jt.com/cart/delete/562379.html
* 參數: 獲取itemId
* 返回值: 重定向到購物車的展示頁面
*/
@RequestMapping("/delete/{itemId}")
public String deleteCarts(@PathVariable Long itemId){
Long userId = 7L;
cartService.deleteCarts(userId,itemId);
return "redirect:/cart/show.html";
}
1.3編輯CartService
@Override
public void deleteCarts(Long userId, Long itemId) {
QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
queryWrapper.eq("item_id", itemId);
cartMapper.delete(queryWrapper);
}
2.京淘權限實現
2.1業務需求
當用戶進行敏感操做時,必需要求用戶先登陸以後才能訪問後端服務器.例如京東商城.
使用技術:
1.AOP 2.攔截器:攔截用戶的請求
![image image](http://static.javashuo.com/static/loading.gif)
2.2定義京淘攔截器
2.2.1SpringMVC調用原理圖
![image image](http://static.javashuo.com/static/loading.gif)
2.2.2SpringMVC攔截器工做原理
![image image](http://static.javashuo.com/static/loading.gif)
2.2.3配置攔截器
@Component //spring容器管理對象
public class UserInterceptor implements HandlerInterceptor {
@Autowired
private JedisCluster jedisCluster;
//Spring版本升級 4 必須實現全部的方法 spring 5 只須要重寫指定的方法便可.
/**
* 需求: 攔截/cart開頭的全部的請求進行攔截.,而且校驗用戶是否登陸.....
* 攔截器選擇: preHandler
* 如何判斷用戶是否登陸: 1.檢查cookie信息 2.檢查Redis中是否有記錄.
* true : 請求應該放行
* false: 請求應該攔截 則配合重定向的語法實現頁面跳轉到登陸頁面 使得程序流轉起來
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//1.判斷用戶是否登陸 檢查cookie是否有值
String ticket = CookieUtil.getCookieValue(request,"JT_TICKET");
//2.校驗ticket
if(!StringUtils.isEmpty(ticket)){
//3.判斷redis中是否有值.
if(jedisCluster.exists(ticket)){
//4.動態獲取json信息
String userJSON = jedisCluster.get(ticket);
User user = ObjectMapperUtil.toObj(userJSON,User.class);
request.setAttribute("JT_USER",user);
return true;
}
}
response.sendRedirect("/user/login.html");
return false;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//銷燬數據
request.removeAttribute("JT_USER");
}
}
2.2.4獲取動態UserId
![image image](http://static.javashuo.com/static/loading.gif)
2.3ThreadLocal介紹
2.3.1ThreadLocal做用
名稱:本地線程變量
做用:能夠在同一線程內,實現數據共享.
![image image](http://static.javashuo.com/static/loading.gif)
2.3.2ThreadLocal入門案例
private ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
public void a(){
int a = 100;
threadLocal.set(a);
b();
}
public void b(){
int a = threadLocal.get();
int b = 100*a;
}
2.3.3編輯ThreadLocal工具API
public class UserThreadLocal {
//static不會影響影響線程 threadLocal建立時跟隨線程.
//private static ThreadLocal<Map<k,v>> threadLocal = new ThreadLocal<>();
private static ThreadLocal<User> threadLocal = new ThreadLocal<>();
public static void set(User user){
threadLocal.set(user);
}
public static User get(){
return threadLocal.get();
}
public static void remove(){
threadLocal.remove();
}
}
2.3.4重構User攔截器
![image image](http://static.javashuo.com/static/loading.gif)
2.3.5動態獲取UserId
![image image](http://static.javashuo.com/static/loading.gif)
京淘訂單模塊
3.1訂單表設計
![image image](http://static.javashuo.com/static/loading.gif)
3.2建立訂單項目
3.2.1建立項目
![image image](http://static.javashuo.com/static/loading.gif)
3.2.2添加繼承依賴
<!--2.添加依賴信息-->
<dependencies>
<!--依賴實質依賴的是jar包文件-->
<dependency>
<groupId>com.jt</groupId>
<artifactId>jt-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<!--3.添加插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.2.3添加POJO
![image image](http://static.javashuo.com/static/loading.gif)
刪除orderItem的主鍵標識
![image image](http://static.javashuo.com/static/loading.gif)
3.2.4構建jt-order項目
訂單項目代碼結構以下
![image image](http://static.javashuo.com/static/loading.gif)
3.3訂單確認頁面跳轉
3.3.1url分析
![image image](http://static.javashuo.com/static/loading.gif)
3.3.2編輯OrderController
package com.jt.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.service.DubboCartService;
import com.jt.service.DubboOrderService;
import com.jt.thread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/order")
public class OrderController {
@Reference(timeout = 3000,check = false)
private DubboOrderService orderService;
@Reference(timeout = 3000,check = false)
private DubboCartService cartService;
/**
* 訂單頁面跳轉
* url: http://www.jt.com/order/create.html
* 頁面取值: ${carts}
*/
@RequestMapping("/create")
public String create(Model model){
//1.根據userId查詢購物車信息
Long userId = UserThreadLocal.get().getId();
List<Cart> cartList = cartService.findCartListByUserId(userId);
model.addAttribute("carts",cartList);
return "order-cart";
}
}
3.3.3編輯OrderService
@Override
public List<Cart> findCartListByUserId(Long userId) {
QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
return cartMapper.selectList(queryWrapper);
}
3.3.4頁面效果展示
![image image](http://static.javashuo.com/static/loading.gif)
3.4關於SpringMVC參數提交問題說明
3.4.1簡單參數傳參問題
1.頁面url標識
2.Controller中的方法
public void xxx(String name,int age){
}
public class User{
private Integer name;
private String age;
}
3.4.3使用對象的引用爲參數賦值
難點:屬性的重名提交問題
解決思路:能夠採用對象的引用方式爲屬性賦值
<input name="name" value="二郎神" />
<input name="age" value="3000" />
<input name="dog.name" value="哮天犬" />
<input name="dog.age" value="8000" />
2.Controller中的方法
public void xxx(User user){
}
public class Dog{
private String name;
private Integer age;
}
public class User{
private String name;
private Integer age;
private Dog dog;
}
3.5關於訂單提交
3.5.1頁面URL說明
![image image](http://static.javashuo.com/static/loading.gif)
3.5.2請求參數
![image image](http://static.javashuo.com/static/loading.gif)
3.5.3頁面JS分析
jQuery.ajax( {
type : "POST",
dataType : "json",
url : "/order/submit",
data : $("#orderForm").serialize(),
// data: {"key":"value","key2":"value2".....}
// data: id=1&name="xxx"&age=18......
cache : false,
success : function(result) {
if(result.status == 200){
location.href = "/order/success.html?id="+result.data;
}else{
$("#submit_message").html("訂單提交失敗,請稍後重試...").show();
}
},
error : function(error) {
$("#submit_message").html("親愛的用戶請不要頻繁點擊, 請稍後重試...").show();
}
});
3.5.3編輯OrderController
/**
* 訂單提交
* url: http://www.jt.com/order/submit
* 參數: 整個form表單
* 返回值: SysResult對象 攜帶返回值orderId
* 業務說明:
* 當訂單入庫以後,須要返回orderId.讓用戶查詢.
*/
@RequestMapping("/submit")
@ResponseBody
public SysResult saveOrder(Order order){
Long userId = UserThreadLocal.get().getId();
order.setUserId(userId);
String orderId = orderService.saveOrder(order);
if(StringUtils.isEmpty(orderId))
return SysResult.fail();
else
return SysResult.success(orderId);
}
3.5.4編輯OrderService
@Service(timeout = 3000)
public class DubboOrderServiceImpl implements DubboOrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private OrderShippingMapper orderShippingMapper;
/**
* Order{order訂單自己/order物流信息/order商品信息}
* 難點: 操做3張表完成入庫操做
* 主鍵信息: orderId
* @param order
* @return
*/
@Override
public String saveOrder(Order order) {
//1.拼接OrderId
String orderId =
"" + order.getUserId() + System.currentTimeMillis();
//2.完成訂單入庫
order.setOrderId(orderId).setStatus(1);
orderMapper.insert(order);
//3.完成訂單物流入庫
OrderShipping orderShipping = order.getOrderShipping();
orderShipping.setOrderId(orderId);
orderShippingMapper.insert(orderShipping);
//4.完成訂單商品入庫
List<OrderItem> orderItems = order.getOrderItems();
//批量入庫 sql: insert into xxx(xxx,xx,xx)values (xx,xx,xx),(xx,xx,xx)....
for (OrderItem orderItem : orderItems){
orderItem.setOrderId(orderId);
orderItemMapper.insert(orderItem);
}
System.out.println("訂單入庫成功!!!!");
return orderId;
}
}
3.6訂單成功跳轉
3.6.1頁面url分析
![image image](http://static.javashuo.com/static/loading.gif)
3.6.2編輯OrderController
/**
* 實現商品查詢
* 1.url地址: http://www.jt.com/order/success.html?id=71603356409924
* 2.參數說明: id 訂單編號
* 3.返回值類型: success.html
* 4.頁面取值方式: ${order.orderId}
*/
@RequestMapping("/success")
public String findOrderById(String id,Model model){
Order order = orderService.findOrderById(id);
model.addAttribute("order",order);
return "success";
}
3.6.2編輯OrderService
@Override
public Order findOrderById(String id) {
//1.查詢訂單信息
Order order = orderMapper.selectById(id);
//2.查詢訂單物流信息
OrderShipping orderShipping = orderShippingMapper.selectById(id);
//3.查詢訂單商品
QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("order_id",id);
List<OrderItem> lists =orderItemMapper.selectList(queryWrapper);
return order.setOrderItems(lists).setOrderShipping(orderShipping);
}
3.6.3頁面效果展示
![image image](http://static.javashuo.com/static/loading.gif)
4.項目結構圖
![image image](http://static.javashuo.com/static/loading.gif)