package com.example.demo.controller;
import com.example.demo.bean.Goods;
import com.example.demo.bean.Orders;
import com.example.demo.bean.Result;
import com.example.demo.service.IGoodsService;
import com.example.demo.service.IOrdersService;
import com.example.demo.util.CreateId;
import com.example.demo.util.GetSystemTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.List;
@RestController
@RequestMapping("/order")
public class CartController {
@Autowired
private IGoodsService goodsService;
@Autowired
private IOrdersService ordersService;
/**
* 查询所有购物车的列表信息
* @return
*/
@GetMapping("/getAllorders")
public Result<List<Orders>> getAllOrder(){
Result<List<Orders>> result = new Result<List<Orders>>();
List<Orders> allOrderList = ordersService.getAllOrders();
if(allOrderList.size()>0){
result.setCode(200);
result.setMsg("查询购物车信息列表成功");
result.setData(allOrderList);
return result;
}else{
result.setCode(400);
result.setMsg("查询失败,请稍后再试");
return result;
}
}
/**
* 根据产品的id,新增一条购物车信息
* @param id
* @return
*/
@GetMapping("/addOneOrder")
public Result<List<Orders>> addOneOrder(Integer id){
//创建一个唯一的订单id。正整数。
Integer orderNo = CreateId.createId2();
//创建回传对象。
Result<List<Orders>> result = new Result<List<Orders>>();
//根据id查询产品信息。
List<Goods> goods = goodsService.getOneGoodsById(Collections.singleton(id));
String goodsName = goods.get(0).getName();
Float goodsPrice = goods.get(0).getPrice();
//封装order对象。
Float total = Float.valueOf(goodsPrice);
Orders orders = new Orders();
orders.setOrderNo(orderNo);
orders.setCreateUserId(1);
orders.setGoodsName(goodsName);
orders.setNumber(1);//默认数量是1.
orders.setStatus(0);//默认状态是0。0:待支付,1:已支付
orders.setTotal(total);//默认就是一个产品的单价。
orders.setCreateTime(GetSystemTime.getDateTime());
//调用业务层接口,新增一个订单信息。
ordersService.addOneOrders(orders);
result.setCode(200);
result.setMsg("新增订单成功");
return result;
}
}
springboot3+vue3支付宝在线支付案例,后端接口-购物车!我定义了2个接口,一个是获取购物车列表信息,一个是新增一条购物车信息。
package com.example.demo.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.aspectj.lang.annotation.AfterReturning;
import javax.persistence.*;
import java.sql.Timestamp;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "tb_orders")
public class Orders {
@javax.persistence.Id
@org.springframework.data.annotation.Id
@GeneratedValue(strategy= GenerationType.IDENTITY)//主键生成策略
@Column(name = "id", nullable = false)
private Integer id;//主键
@Column(name = "order_no",nullable = true)
private Integer orderNo;//订单编号
@Column(name = "goods_name",nullable = true)
private String goodsName;//订单内商品名字
private Integer number;//订单总数量
private Float total;//订单金额
@Column(name = "create_time")
private Timestamp createTime;
@Column(name = "modify_time")
private Timestamp modifyTime;
@Column(name = "create_user_id")
private Integer createUserId;
@Column(name = "modify_user_id")
private Integer modifyUserId;
private Integer payNo;//支付结算的编码
@Column(name = "pay_time")
private Timestamp payTime;//支付结算的时间戳
private Integer status;//订单的状态,0-未结算,1-已结算。
}
这个就是购物车的实体类。
package com.example.demo.service;
import com.example.demo.bean.Orders;
import java.util.List;
/**
* 订单的业务层接口
*/
public interface IOrdersService {
//获取所有的订单
public List<Orders> getAllOrders();
//根据id查询某一个订单信息
public Orders getOneOrdersById(Integer id);
//删除指定id的订单
public void deleteOneOrdersById(Integer id);
//更新指定id的订单
public void updateOneOrdersById(Orders orders);
//新增一个订单
public void addOneOrders(Orders orders);
}
购物车的业务层接口代码
package com.example.demo.service.impl;
import com.example.demo.bean.Orders;
import com.example.demo.mapper.OrdersMapper;
import com.example.demo.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrdersService implements IOrdersService {
@Autowired
private OrdersMapper ordersMapper;
@Override
public List<Orders> getAllOrders() {
return ordersMapper.findAll();
}
@Override
public Orders getOneOrdersById(Integer id) {
return ordersMapper.getReferenceById(id);
}
@Override
public void deleteOneOrdersById(Integer id) {
ordersMapper.deleteById(id);
}
@Override
public void updateOneOrdersById(Orders orders) {
ordersMapper.save(orders);
}
@Override
public void addOneOrders(Orders orders) {
ordersMapper.save(orders);
}
}
购物车的业务接口实现类
package com.example.demo.mapper;
import com.example.demo.bean.Orders;
import org.springframework.data.jpa.repository.JpaRepository;
public interface OrdersMapper extends JpaRepository<Orders,Integer> {
}
业务逻辑是,在产品列表的页面,有一个加入购物车的按钮,绑定了一个点击事件。会触发这个业务接口。在数据库内新增一条购物消息。
如图所示,前端页面。购买按钮就是绑定了加入购物车的接口地址。
import { ref } from 'vue'
import { defineStore } from 'pinia'
import axios from 'axios'
const baseUrl = 'http://erxmns.natappfree.cc'
export const useGoodsStore = defineStore('goods', () => {
const goodsList = ref([])
/**
* 获取产品列表
*/
const getGoodsList = async ()=> {
const res = await axios.get(baseUrl+'/goods/alllist')
goodsList.value = res.data.data
}
/**
* 加入购物车
*/
const addOneOrders= async (id)=>{
const res = await axios.get(baseUrl+'/order/addOneOrder?id='+id)
if(res.data.code===200){
alert('新增订单成功')
}else{
alert('新增订单失败')
}
}
return {goodsList,getGoodsList,addOneOrders }
})
如图代码,在产品的业务接口goods.js内,我们定义好了一个方法。新增一条购物消息。
它调用了接口地址,就是后端的购物车接口之一。
/**
* 加入购物车
*/
const addOneOrders= async (id)=>{
const res = await axios.get(baseUrl+'/order/addOneOrder?id='+id)
if(res.data.code===200){
alert('新增订单成功')
}else{
alert('新增订单失败')
}
}
如图,后台数据库购物车的数据表各个字段情况。