1、购物车 — 添加购物车:
流程分析:
接口实现:
购物车数据表结构:
购物车接口实现:
package com.qfedu.fmmall.service;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.vo.R;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-28 20:35
*/
public interface ShoppingCartService {
public R addShoppingCart(ShoppingCart cart);
}
接口实现类:
package com.qfedu.fmmall.service.impl;
import com.qfedu.fmmall.dao.ShoppingCartMapper;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.service.ShoppingCartService;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-28 20:37
*/
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
@Autowired
private ShoppingCartMapper shoppingCartMapper;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@Override
public R addShoppingCart(ShoppingCart cart) {
cart.setCartTime(sdf.format(new Date()));
int i = shoppingCartMapper.insert(cart);
if(i > 0){
return new R(ResStatus.OK,"success",null);
}else{
return new R(ResStatus.NO,"fail",null);
}
}
}
2、购物车–添加购物车(非登录状态)
流程分析:
公共类:
package com.qfedu.fmmall.vo;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-22 17:26
*/
public class ResStatus {
public static final int OK=10000;
public static final int NO=10001;
public static final int LOGIN_SUCCESS=20000; //认证成功
public static final int LOGIN_FAIL_NOT=20001; //用户未登录
public static final int LOGIN_FAIL_OVERDUE=20002; //用户登录失效
}
拦截器:校验token
package com.qfedu.interceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @Description: 拦截器校验token
* @Author : Jerry
* @create : 2022-06-24 15:10
*/
@Component
public class CheckTokenInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//放行option请求;
String method = request.getMethod();
if("OPTIONS".equalsIgnoreCase(method)){
return true;
}
String token = request.getParameter("token");
if(token == null){
R r = new R(ResStatus.LOGIN_FAIL_NOT, "请先登录!", null);
//提示请先登录
doResponse(response,r);
return false;
}else{
try{
//验证token
JwtParser parser = Jwts.parser();
parser.setSigningKey("123456"); //解析token的SigningKey必须和生成的token时设置密码一样
//如果token正确(密码正确,有效期内)则正常执行,否则抛出异常
Jws<Claims> claimsJws = parser.parseClaimsJws(token);
return true;
}catch (ExpiredJwtException e){
R r = new R(ResStatus.LOGIN_FAIL_OVERDUE, "登录过期,请重新登录!", null);
doResponse(response,r);
}catch (UnsupportedJwtException e){
R r = new R(ResStatus.NO, "token不合法,请自重!", null);
doResponse(response,r);
}catch (Exception e){
R r = new R(ResStatus.LOGIN_FAIL_NOT, "请先登录!", null);
doResponse(response,r);
}
}
return false;
}
private void doResponse(HttpServletResponse response,R r) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String s = new ObjectMapper().writeValueAsString(r);
out.print(s);
out.flush();
out.close();
}
}
3、购物车–购物车列表;
流程分析:
接口实现:
sql:
实体类:productCartVO
package com.qfedu.fmmall.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Table;
import java.math.BigDecimal;
/**
* 新增productName,productImg
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShoppingCartVO {
private Integer cartId;
private String productId;
private String skuId;
private String userId;
private String cartNum;
private String cartTime;
private BigDecimal productPrice;
private String skuProps;
private String productName;
private String productImg;
private Double originalPrice;
private Double sellPrice;
private String skuName;
}
在mapper接口定义查询方法:
package com.qfedu.fmmall.dao;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.entity.ShoppingCartVO;
import com.qfedu.fmmall.general.GeneralDAO;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ShoppingCartMapper extends GeneralDAO<ShoppingCart> {
public List<ShoppingCartVO> selectShopCartByUserId(int userId);
}
映射配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qfedu.fmmall.dao.ShoppingCartMapper">
<resultMap id="ShoppingCartVOMap" type="com.qfedu.fmmall.entity.ShoppingCartVO">
<result column="cart_id" jdbcType="INTEGER" property="cartId" />
<result column="product_id" jdbcType="VARCHAR" property="productId" />
<result column="sku_id" jdbcType="VARCHAR" property="skuId" />
<result column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="cart_num" jdbcType="VARCHAR" property="cartNum" />
<result column="cart_time" jdbcType="VARCHAR" property="cartTime" />
<result column="product_price" jdbcType="DECIMAL" property="productPrice" />
<result column="sku_props" jdbcType="VARCHAR" property="skuProps" />
<result column="product_name" jdbcType="VARCHAR" property="productName" />
<result column="url" jdbcType="VARCHAR" property="productImg" />
<result column="original_price" jdbcType="VARCHAR" property="originalPrice" />
<result column="sell_price" jdbcType="VARCHAR" property="sellPrice" />
<result column="sku_name" jdbcType="VARCHAR" property="skuName" />
</resultMap>
<select id="selectShopCartByUserId" resultMap="ShoppingCartVOMap">
select c.cart_id,
c.product_id,
c.sku_id,
c.user_id,
c.cart_num,
c.cart_time,
c.product_price,
c.sku_props,
p.product_name, i.url,
s.original_price,s.sell_price,s.sku_name
from shopping_cart c
INNER JOIN product p
INNER JOIN product_img i
INNER JOIN product_sku s
ON c.product_id = p.product_id
and i.item_id = p.product_id
and c.sku_id = s.sku_id
where user_id = #{userId}
and i.is_main=1
</select>
</mapper>
service接口:
package com.qfedu.fmmall.service.impl;
import com.qfedu.fmmall.dao.ShoppingCartMapper;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.entity.ShoppingCartVO;
import com.qfedu.fmmall.service.ShoppingCartService;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-28 20:37
*/
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
@Autowired
private ShoppingCartMapper shoppingCartMapper;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@Override
public R addShoppingCart(ShoppingCart cart) {
cart.setCartTime(sdf.format(new Date()));
int i = shoppingCartMapper.insert(cart);
if(i > 0){
return new R(ResStatus.OK,"success",null);
}else{
return new R(ResStatus.NO,"fail",null);
}
}
@Transactional(propagation = Propagation.SUPPORTS)
public R listShoppingCartByUserId(int userId) {
List<ShoppingCartVO> list = shoppingCartMapper.selectShopCartByUserId(userId);
R r = new R(ResStatus.OK, "success", list);
return r;
}
}
4、购物车–修改购物车数量
流程分析:
在mapper接口定义修改方法:
package com.qfedu.fmmall.dao;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.entity.ShoppingCartVO;
import com.qfedu.fmmall.general.GeneralDAO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ShoppingCartMapper extends GeneralDAO<ShoppingCart> {
public List<ShoppingCartVO> selectShopCartByUserId(int userId);
public int updateCartNumByCartId(@Param("cartId") int cartId,
@Param("cartNum") int cartNum);
}
映射文件:
<update id="updateCartNumByCartId">
update shopping_cart set cart_num=#{cartNum} where cart_id=#{cartId}
</update>
service接口:
package com.qfedu.fmmall.service;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.vo.R;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-28 20:35
*/
public interface ShoppingCartService {
public R addShoppingCart(ShoppingCart cart);
public R listShoppingCartByUserId(int userId);
public R updateCartNum(int cartId,int cartNum);
}
service实现类:
package com.qfedu.fmmall.service.impl;
import com.qfedu.fmmall.dao.ShoppingCartMapper;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.entity.ShoppingCartVO;
import com.qfedu.fmmall.service.ShoppingCartService;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-28 20:37
*/
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
@Autowired
private ShoppingCartMapper shoppingCartMapper;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@Override
public R updateCartNum(int cartId, int cartNum) {
int i = shoppingCartMapper.updateCartNumByCartId(cartId, cartNum);
if(i>0){
return new R(ResStatus.OK,"update success",null);
}else{
return new R(ResStatus.NO,"update fail",null);
}
}
}
controller:
package com.qfedu.controller;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.service.ShoppingCartService;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @Description:
* @Author : Jerry
* @create : 2022-06-23 16:32
*/
@RestController
@RequestMapping("/shopcart")
@CrossOrigin
public class ShopcartController {
@Autowired
private ShoppingCartService shoppingCartService;
@PutMapping("/update/{cid}/{cnum}")
public R updateNum(@PathVariable("cid") Integer cartId,
@PathVariable("cnum") Integer cartNum,
@RequestHeader("token")String token){
R r = shoppingCartService.updateCartNum(cartId, cartNum);
return r;
}
}
版权声明:本文为Flying_Heaven原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。