作者主页:夜未央5788
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
基于SpringBoot+Vue的网上书城系统(前后端分离项目)
本项目分为前后台,前台为普通用户登录,后台为管理员登录;
角色:管理员、用户
功能:首页推荐(默认根据用户买过的商品进行推荐,如果没买过则根据商品销量推荐)、轮播图管理、商品、订单、购物车、商品评论、商品评论点赞、商品收藏、商品点赞、订单支付、公告、权限控制等。
使用人群:
正在做毕设的学生,或者需要项目实战练习的Java学习者
由于本程序规模不大,可供课程设计,毕业设计学习演示之用
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
4.数据库:MySql 5.7/8.0版本均可;
5.是否Maven项目:是;
6.是否为前后端分离项目:是
后台: SpringBoot + Mybatis-plus + Mybatis + lombok插件
前台:Vue + Vue Router + ELementUI + Axios
**先启动后端再启动前端**
后端:
1. 使用Navicat或者其它工具,在mysql中创建对应sql文件名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入后端项目end文件夹,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.yml配置文件中的数据库配置改为自己的配置;
4. 运行项目,后端运行成功后再运行前端项目;
前端:
1、确保安装好node及npm,命令行切换到front所在路径
2、执行npm run serve启动项目
后台地址:http://localhost:8001/login
用户: tom/123456
管理员: admin/admin
CollectController
package com.example.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.example.common.Result;
import com.example.exception.CustomException;
import com.example.service.UserService;
import com.example.entity.Collect;
import com.example.service.CollectService;
import com.example.entity.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@RequestMapping("/api/collect")
public class CollectController {
@Resource
private CollectService collectService;
@Resource
private HttpServletRequest request;
@Resource
private UserService userService;
public User getUser() {
String token = request.getHeader("token");
String username = JWT.decode(token).getAudience().get(0);
return userService.getOne(Wrappers.lambdaQuery().eq(User::getUsername, username));
}
@PostMapping
public Result> save(@RequestBody Collect collect) {
List list = collectService.list(Wrappers.lambdaQuery().eq(Collect::getGoodsId, collect.getGoodsId())
.eq(Collect::getUserId, getUser().getId()));
if (CollUtil.isNotEmpty(list)) {
throw new CustomException("-1", "您已收藏该商品");
}
collect.setCreateTime(DateUtil.now());
collectService.save(collect);
return Result.success();
}
@PutMapping
public Result> update(@RequestBody Collect collect) {
collectService.updateById(collect);
return Result.success();
}
@DeleteMapping("/{id}")
public Result> delete(@PathVariable Long id) {
collectService.removeById(id);
return Result.success();
}
@GetMapping("/{id}")
public Result> findById(@PathVariable Long id) {
return Result.success(collectService.getById(id));
}
@GetMapping
public Result> findAll() {
List list = collectService.list();
return Result.success(list);
}
@GetMapping("/page")
public Result> findPage(@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
LambdaQueryWrapper query = Wrappers.lambdaQuery().orderByDesc(Collect::getId);
IPage page = collectService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
@GetMapping("/page/front")
public Result> findPageFront(@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
User user = getUser();
if (user == null) {
return Result.success(new Page<>());
}
LambdaQueryWrapper query = Wrappers.lambdaQuery().eq(Collect::getUserId, getUser().getId()).orderByDesc(Collect::getId);
IPage page = collectService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
}
GoodsController
package com.example.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.example.common.Result;
import com.example.service.UserService;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.example.entity.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
@RestController
@RequestMapping("/api/goods")
public class GoodsController {
@Resource
private GoodsService goodsService;
@Resource
private HttpServletRequest request;
@Resource
private UserService userService;
public User getUser() {
String token = request.getHeader("token");
String username = JWT.decode(token).getAudience().get(0);
return userService.getOne(Wrappers.lambdaQuery().eq(User::getUsername, username));
}
@PostMapping
public Result> save(@RequestBody Goods goods) {
goods.setCreateTime(DateUtil.now());
goodsService.save(goods);
return Result.success();
}
@PutMapping
public Result> update(@RequestBody Goods goods) {
goodsService.updateById(goods);
return Result.success();
}
@DeleteMapping("/{id}")
public Result> delete(@PathVariable Long id) {
goodsService.removeById(id);
return Result.success();
}
@GetMapping("/{id}")
public Result> findById(@PathVariable Long id) {
Goods goods = goodsService.getById(id);
goods.setRealPrice(goods.getPrice().multiply(BigDecimal.valueOf(goods.getDiscount())));
return Result.success(goods);
}
@GetMapping
public Result> findAll() {
List list = goodsService.findAll();
return Result.success(list);
}
/**
* 推荐商品
* @return
*/
@GetMapping("/recommend")
public Result> recommend() {
List list = goodsService.recommend();
return Result.success(list);
}
/**
* 推热销商品
* @return
*/
@GetMapping("/sales")
public Result> sales() {
List list = goodsService.sales();
return Result.success(list);
}
/**
* 根据分类id查询商品
* @param id
* @param pageNum
* @param pageSize
* @return
*/
@GetMapping("/byCategory/{id}")
public Result> findByCategory(@PathVariable Long id,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
IPage page = goodsService.pageByCategory(new Page<>(pageNum, pageSize), id);
return Result.success(page);
}
@GetMapping("/page")
public Result> findPage(@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
IPage page = goodsService.findPage(new Page<>(pageNum, pageSize), name);
return Result.success(page);
}
}
OrderController
package com.example.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.dto.PreOrderQo;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.example.service.*;
import org.json.JSONException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/order")
public class OrderController {
@Resource
private OrderService orderService;
@Resource
private HttpServletRequest request;
@Resource
private UserService userService;
@Resource
private CartService cartService;
@Resource
private OrderGoodsService orderGoodsService;
@Resource
private GoodsService goodsService;
public User getUser() {
String token = request.getHeader("token");
String username = JWT.decode(token).getAudience().get(0);
return userService.getOne(Wrappers.lambdaQuery().eq(User::getUsername, username));
}
@Transactional
@PostMapping
public Result> save(@RequestBody Order order) {
order.setUserId(getUser().getId());
order.setOrderNo(DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(6));
order.setCreateTime(DateUtil.now());
String cartsStr = order.getCarts();
List carts = JSONUtil.toBean(cartsStr, new TypeReference>() {
}, true);
orderService.save(order);
for (Cart cart : carts) {
Integer count = cart.getCount();
Long goodsId = cart.getGoodsId();
// 扣库存
Goods goods = goodsService.getById(goodsId);
if(goods.getStore() - cart.getCount() < 0) {
throw new CustomException("-1", "库存不足");
}
goods.setStore(goods.getStore() - cart.getCount());
goods.setSales(goods.getSales() + cart.getCount());
goodsService.updateById(goods);
OrderGoods orderGoods = new OrderGoods();
orderGoods.setOrderId(order.getId());
orderGoods.setGoodsId(goodsId);
orderGoods.setCount(count);
orderGoodsService.save(orderGoods);
}
if (order.getType() == 1) { // 1表示购物车,0表示直接购买
// 提交订单时清空个人的购物车商品
cartService.remove(Wrappers.lambdaUpdate().eq(Cart::getUserId, getUser().getId()));
}
return Result.success(order);
}
@PutMapping
public Result> update(@RequestBody Order order) {
orderService.updateById(order);
return Result.success();
}
/**
* 付款
* @param id
* @return
*/
@Transactional
@PutMapping("/pay/{id}")
public Result> pay(@PathVariable Long id) {
Order order = orderService.getById(id);
BigDecimal totalPrice = order.getTotalPrice();
Long userId = getUser().getId();
User user = userService.findById(userId);
if (user.getAccount().compareTo(totalPrice) <= 0) {
throw new CustomException("-1", "余额不足");
}
user.setAccount(user.getAccount().subtract(totalPrice)); // 设置用户余额
userService.updateById(user);
order.setState("待发货");
orderService.updateById(order);
return Result.success();
}
@DeleteMapping("/{id}")
public Result> delete(@PathVariable Long id) {
orderService.removeById(id);
return Result.success();
}
/**
* 获取订单的确认信息
*
* @return
*/
@PostMapping("/pre")
public Result> pre(@RequestBody PreOrderQo preOrderQo) throws JSONException {
String cartsStr = preOrderQo.getCarts();
// 讲前台传来的json字符串转换成 list对象
List carts = JSONUtil.toBean(cartsStr, new TypeReference>() {
}, true);
Map all = cartService.findAll(carts);
return Result.success(all);
}
@GetMapping("/{id}")
public Result> findById(@PathVariable Long id) {
return Result.success(orderService.getById(id));
}
@GetMapping
public Result> findAll() {
List list = orderService.list();
return Result.success(list);
}
@GetMapping("/page")
public Result> findPage(@RequestParam(required = false, defaultValue = "") String name,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
LambdaQueryWrapper query = Wrappers.lambdaQuery().orderByDesc(Order::getId);
if (StrUtil.isNotBlank(name)) {
query.like(Order::getOrderNo, name);
}
IPage page = orderService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
/**
* 前台查询订单列表
* @param state
* @param pageNum
* @param pageSize
* @return
*/
@GetMapping("/page/front")
public Result> findPageFront(@RequestParam(required = false, defaultValue = "") String state,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
LambdaQueryWrapper query = Wrappers.lambdaQuery().orderByDesc(Order::getId);
query.eq(Order::getUserId, getUser().getId());
// 根据状态查询
if (StrUtil.isNotBlank(state)) {
query.eq(Order::getState, state);
}
IPage page = orderService.page(new Page<>(pageNum, pageSize), query);
for (Order order : page.getRecords()) {
Long orderId = order.getId();
List carts = orderGoodsService.findByOrderId(orderId);
order.setCarts(JSONUtil.toJsonStr(carts));
}
return Result.success(page);
}
}
如果也想学习本系统,下面领取。关注并回复:163springboot