目录
1、项目说明
2、项目配置
3、项目代码分享
4、项目演示
5、项目获取(免费)
基于SpringBoot+Vue的玩具商城系统的设计与实现
开发技术环境: Idea + Vscode + Mysql + Springboot + vue3.0
购物商城网站分为前台功能和后台管理功能
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.3
com.toy
toymall
0.0.1-SNAPSHOT
toymall
toymall
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatis-plus-boot-starter
3.4.3.1
com.baomidou
mybatis-plus-generator
3.3.2
org.apache.velocity
velocity
1.7
org.hibernate
hibernate-validator
5.2.4.Final
javax.validation
validation-api
1.1.0.Final
org.jboss.logging
jboss-logging
3.4.1.Final
javax
javaee-api
7.0
com.alipay.sdk
alipay-sdk-java
4.16.2.ALL
org.springframework.boot
spring-boot-maven-plugin
2.7.3
org.projectlombok
lombok
src/main/java
**/*.xml
package com.toy.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.toy.entity.Cart;
import com.toy.entity.Orders;
import com.toy.entity.User;
import com.toy.entity.UserAddress;
import com.toy.exception.ToyMallException;
import com.toy.result.ResponseEnum;
import com.toy.service.CartService;
import com.toy.service.UserAddressService;
import com.toy.service.UserService;
import com.toy.vo.CartVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
*
* 前端控制器
*
*
* @author admin
* @since
*/
@Controller
@RequestMapping("/cart")
@Slf4j
public class CartController {
@Autowired
private CartService cartService;
@Autowired
private UserAddressService userAddressService;
/**
*添加购物车
* @return
*/
@GetMapping("/add/{productId}/{price}/{quantity}")
public String add(
@PathVariable("productId") Integer productId,
@PathVariable("price") Float price,
@PathVariable("quantity") Integer quantity,
HttpSession session){
if (productId == null || price == null || quantity ==null){
log.info("【添加购物车】参数为空");
throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
}
User user = (User) session.getAttribute("user");
if (user == null){
log.info("【添加购物车】当前为未登录状态");
throw new ToyMallException(ResponseEnum.NOT_LOGIN);
}else {
//登录用户
//购物车存在,加数量
List cartVOList = this.cartService.findCartVOListByUserId(user.getId());
for (CartVO cartVO : cartVOList) {
if (cartVO.getProductId().equals(productId)){
Boolean update = cartService.update(cartVO.getId(),
cartVO.getQuantity() + quantity,
cartVO.getCost() + price * quantity);
if (!update){
log.info("【添加购物车】修改失败");
throw new ToyMallException(ResponseEnum.CART_ADD_ERROR);
}
return "redirect:/cart/get";
}
}
//查询该用户的购物车记录
Cart cart = new Cart();
cart.setUserId(user.getId());
cart.setProductId(productId);
cart.setQuantity(quantity);
cart.setCost(price * quantity);
Boolean add = this.cartService.add(cart);
if (!add){
log.info("【添加购物车】添加失败");
throw new ToyMallException(ResponseEnum.CART_ADD_ERROR);
}
return "redirect:/cart/get";
}
}
/**
* 查看购物车
* @param session
* @return
*/
@GetMapping("/get")
public ModelAndView get(HttpSession session){
User user = (User) session.getAttribute("user");
if (user == null){
log.info("【查看购物车】当前为未登录状态");
throw new ToyMallException(ResponseEnum.NOT_LOGIN);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("settlement1");
modelAndView.addObject("cartList",
this.cartService.findCartVOListByUserId(user.getId()));
return modelAndView;
}
/**
* 更新购物车
* @return
*/
@PostMapping("update/{id}/{quantity}/{cost}")
@ResponseBody
public String update(
@PathVariable("id") Integer id,
@PathVariable("quantity") Integer quantity,
@PathVariable("cost") Float cost,
HttpSession session){
if (id == null || quantity == null || cost == null){
log.info("【更新购物车】参数为空");
throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
}
User user = (User) session.getAttribute("user");
if (user == null){
log.info("【更新购物车】当前为未登录状态");
throw new ToyMallException(ResponseEnum.NOT_LOGIN);
}
if (this.cartService.update(id, quantity, cost)){
return "success";
}
return "fail";
}
/**
* 删除购物车
* @param id
* @param session
* @return
*/
@GetMapping("/delete/{id}")
public String delete(@PathVariable Integer id,HttpSession session){
if (id == null){
log.info("【删除购物车】参数为空");
throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
}
User user = (User) session.getAttribute("user");
if (user == null){
log.info("【删除购物车】当前为未登录状态");
throw new ToyMallException(ResponseEnum.NOT_LOGIN);
}
Boolean delete = this.cartService.delete(id);
if (delete){
return "redirect:/cart/get";
}
return null;
}
/**
* 确认结算
* @param session
* @return
*/
@GetMapping("/confirm")
public ModelAndView confirm(HttpSession session){
//判断是否为登录用户
User user = (User) session.getAttribute("user");
if (user == null){
log.info("【确认订单】当前为未登录状态");
throw new ToyMallException(ResponseEnum.NOT_LOGIN);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("settlement2");
modelAndView.addObject("cartList",
this.cartService.findCartVOListByUserId(user.getId()));
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", user.getId());
modelAndView.addObject("addressList",
this.userAddressService.list(queryWrapper));
return modelAndView;
}
/**
* 确认订单信息
* @param userAddress
* @param session
* @return
*/
@PostMapping("/commit")
public ModelAndView commit(String userAddress,
String address,
String remark,
HttpSession session){
if (userAddress == null || address == null || remark == null){
log.info("【提交订单】参数为空");
throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
}
User user = (User) session.getAttribute("user");
if (user == null){
log.info("【提交订单】当前为未登录状态");
throw new ToyMallException(ResponseEnum.NOT_LOGIN);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("settlement3");
Orders orders = this.cartService.commit(userAddress, address, remark, user);
if (orders != null){
modelAndView.addObject("orders", orders);
modelAndView.addObject("cartList",
this.cartService.findCartVOListByUserId(user.getId()));
return modelAndView;
}
return null;
}
}
链接:https://pan.baidu.com/s/1wdxvqMr9YKRCe1n7Axtywg
提取码:1s7m