购物车 -- 结算、提交订单接口开发

在购物车列表中选择对应的商品之后,点击提交生成订单的过程

流程图:

 

接口实现:

收货地址列表接口:

此操作的数据库实现可以通过tkMapper通过方法完成

service接口:

package com.qfedu.fmmall.service;

import com.qfedu.fmmall.vo.R;

/**
 * @Description:
 * @Author : Jerry
 * @create : 2022-06-30 14:59
 */
public interface UserAddrService {

    public R listAddrByUid(int userId);

}

service实现类:

package com.qfedu.fmmall.service.impl;

import com.qfedu.fmmall.dao.UserAddrMapper;
import com.qfedu.fmmall.entity.UserAddr;
import com.qfedu.fmmall.service.UserAddrService;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description:
 * @Author : Jerry
 * @create : 2022-06-30 15:01
 */
public class UserAddrServiceImpl implements UserAddrService {

    @Autowired
    private UserAddrMapper userAddrMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    public R listAddrByUid(int userId) {
        Example example = new Example(UserAddrMapper.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("status",1);
        List<UserAddr> userAddrs = userAddrMapper.selectByExample(example);
        R r = new R(ResStatus.OK, "success", userAddrs);
        return r;
    }
}

controller:

package com.qfedu.controller;

import com.qfedu.fmmall.service.UserAddrService;
import com.qfedu.fmmall.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @Author : Jerry
 * @create : 2022-06-30 15:11
 */
@RestController
@CrossOrigin
@RequestMapping("/userAddr")
public class UserAddrController {

    @Autowired
    private UserAddrService userAddrService;

    @GetMapping("/login")
    public R listAddr(Integer userId, @RequestHeader("token") String token){
        R r = userAddrService.listAddrByUid(userId);
        return r;
    }

}

购物车记录列表接口:

根据一个id的集合,查询购物车记录:

Mapper接口定义查询方法:

 映射配置:

<?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="selectShopCartByCids" 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 i.is_main = 1 and c.cart_id in
  <foreach collection="list" item="cid" separator="," open="(" close=")">
    #{cid}
  </foreach>
</select>
</mapper>

service接口:

 

service实现类:

使String变成集合

 

@Override
public R listShoppingCartsByCids(String cids) {
    String[] arr = cids.split(",");
    List<Integer> cardIds = new ArrayList<>();
    for(int i=0;i< arr.length;i++){
        cardIds.add(Integer.parseInt(arr[i]));
    }

    List<ShoppingCartVO> list = shoppingCartMapper.selectShopCartByCids(cardIds);
    R r = new R(ResStatus.OK, "success", list);
    return r;
}

controller:

@GetMapping("/listByCids")
public R listByCids(String cids, @RequestHeader("token")String token){
    R r = shoppingCartService.listShoppingCartsByCids(cids);//需用逗号隔开
    return r;
}

下一章订单提交及支付.............

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值