目录
1、项目说明
基于SpringBoot+Vue的玩具商城系统的设计与实现
开发技术环境: Idea + Vscode + Mysql + Springboot + vue3.0
购物商城网站分为前台功能和后台管理功能
2、项目配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.toy</groupId>
<artifactId>toymall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>toymall</name>
<description>toymall</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<!--mybatis-plus自动生成代码-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!--springMVC自动校验-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.4.1.Final</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.16.2.ALL</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.3</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
3、项目代码分享
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;
/**
* <p>
* 前端控制器
* </p>
*
* @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<CartVO> 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<UserAddress> 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;
}
}
4、项目演示
5、项目获取(免费)
链接:https://pan.baidu.com/s/1wdxvqMr9YKRCe1n7Axtywg
提取码:1s7m