package com.peng.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.peng.pojo.Cart;
import com.peng.service.CartService;
import com.peng.vo.SysResult;
@Controller("cartController")
@RequestMapping("/cart")
public class CartController {
@Autowired
@Qualifier("cartService")
private CartService cartService;
// 查询我的所有的购物车信息
@RequestMapping("/query/{userId}")
@ResponseBody
public SysResult queryMyCart(@PathVariable Long userId) {
List cartList = cartService.queryMyCart(userId);
return SysResult.oK(cartList);
}
// 保存我的购物车
@RequestMapping("/save")
@ResponseBody
public SysResult saveCart(Cart cart) {
try {
cartService.saveCart(cart);
return SysResult.oK();
} catch (Exception e) {
return SysResult.build(201, "保存失败");
}
}
// 更新商品的数量--购买时点击数量进行的操作
@RequestMapping("/update/num/{userId}/{itemId}/{num}")
@ResponseBody
public SysResult updateNum(@PathVariable Long userId, @PathVariable Long itemId, @PathVariable Long num) {
try {
// 封装一个对象
Cart cart = new Cart();
cart.setUserId(userId);
cart.setItemId(itemId);
cart.setNum(Integer.parseInt(Long.toString(num)));
cartService.updateNum(cart);
return SysResult.oK();
} catch (Exception e) {
return SysResult.build(201, "更新失败");
}
}
// 删除购物车
@RequestMapping("/delete/{userId}/{itemId}")
@ResponseBody
public SysResult deleteCart(@PathVariable Long userId, @PathVariable Long itemId) {
try {
// 构造中间参数
Cart cart = new Cart();
cart.setUserId(userId);
cart.setItemId(itemId);
cartService.deleteCart(cart);
return SysResult.oK();
} catch (Exception e) {
return SysResult.build(201, "删除失败");
}
}
}
Service接口
package com.peng.service;
import java.util.List;
import com.peng.pojo.Cart;
public interface CartService {
/**
* 通过userId查询所有的购物车信息
*
* @param userId
* @return
*/
List queryMyCart(Long userId);
/**
* 保存购物车的信息
*
* @param cart
*/
void saveCart(Cart cart);
/**
* 更新数量
*
* @param cart
*/
void updateNum(Cart cart);
/**
* 删除商品
*
* @param cart
*/
void deleteCart(Cart cart);
}
Service实现类
package com.peng.service.impl;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.peng.mapper.CartMapper;
import com.peng.pojo.Cart;
import com.peng.service.BaseService;
import com.peng.service.CartService;
@Service("cartService")
public class CartServiceImpl extends BaseService implements CartService {
@Autowired
@Qualifier("cartMapper")
private CartMapper cartMapper;
@Override
public List queryMyCart(Long userId) {
return cartMapper.queryMyCart(userId);
}
@Override
public void saveCart(Cart cart) {
/* 判断商品是否存在--不存在,新增;存在,更新 */
// 不能直接用传进来的cart来查询--不准确,改造一下
Cart cart_new = new Cart();
cart_new.setUserId(cart.getUserId());
cart_new.setItemId(cart.getItemId());
// 查询
Cart isExitQueryCart = super.queryByWhere(cart_new);
// 判断
if (null == isExitQueryCart) {// 不存在--进行新增
cart.setCreated(new Date());
cart.setUpdated(cart.getCreated());
cartMapper.insertSelective(cart);
} else {// 存在--进行更新
isExitQueryCart.setNum(isExitQueryCart.getNum() + cart.getNum());
isExitQueryCart.setUpdated(new Date());
cartMapper.updateByPrimaryKeySelective(isExitQueryCart);
}
}
@Override
public void updateNum(Cart cart) {
cartMapper.updateNum(cart);
}
@Override
public void deleteCart(Cart cart) {
cartMapper.delete(cart);
}
}
#
package com.peng.mapper;
import java.util.List;
import com.peng.pojo.Cart;
public interface CartMapper extends SysMapper {
/**
* 通过userId查询所有的购物车信息
*
* @param userId
* @return
*/
List queryMyCart(Long userId);
/**
* 更新商品的数量
*
* @param cart
*/
void updateNum(Cart cart);
}
#
update tb_cart set num=#{num} where user_id=#{userId} and item_id=#{itemId}
Cookie是和域名相绑定的,nginx路径转换的时候带不过去
解决:在nginx下的配置文件中配置
proxy_set_header X-Forwarded-Host $host;
server {
listen 80;
server_name www.jt.com;
#charset koi8-r;
#access_log logs/host.access.log main;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
location / {
proxy_pass http://127.0.0.1:8082;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}