作者主页:夜未央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项目:是;
技术栈
后端:SpringBoot + Mybatis-plus + sa-token
前端:Vue +ELementUI
使用说明
项目运行:
1. 使用Navicat或者其它工具,在mysql中创建对应sql文件名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.yml配置文件中的数据库配置改为自己的配置;
运行截图
前台界面
后管界面
相关代码
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.<User>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<Cart> carts = JSONUtil.toBean(cartsStr, new TypeReference<List<Cart>>() {
}, 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.<Cart>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<Cart> carts = JSONUtil.toBean(cartsStr, new TypeReference<List<Cart>>() {
}, true);
Map<String, Object> 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<Order> 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<Order> query = Wrappers.<Order>lambdaQuery().orderByDesc(Order::getId);
if (StrUtil.isNotBlank(name)) {
query.like(Order::getOrderNo, name);
}
IPage<Order> 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<Order> query = Wrappers.<Order>lambdaQuery().orderByDesc(Order::getId);
query.eq(Order::getUserId, getUser().getId());
// 根据状态查询
if (StrUtil.isNotBlank(state)) {
query.eq(Order::getState, state);
}
IPage<Order> page = orderService.page(new Page<>(pageNum, pageSize), query);
for (Order order : page.getRecords()) {
Long orderId = order.getId();
List<Cart> carts = orderGoodsService.findByOrderId(orderId);
order.setCarts(JSONUtil.toJsonStr(carts));
}
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.<User>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<Goods> list = goodsService.findAll();
return Result.success(list);
}
/**
* 推荐商品
* @return
*/
@GetMapping("/recommend")
public Result<?> recommend() {
List<Goods> list = goodsService.recommend();
return Result.success(list);
}
/**
* 推热销商品
* @return
*/
@GetMapping("/sales")
public Result<?> sales() {
List<Goods> 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<Goods> 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<Goods> page = goodsService.findPage(new Page<>(pageNum, pageSize), name);
return Result.success(page);
}
}
CategoryController
package com.example.controller;
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.Category;
import com.example.service.CategoryService;
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/category")
public class CategoryController {
@Resource
private CategoryService categoryService;
@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.<User>lambdaQuery().eq(User::getUsername, username));
}
@PostMapping
public Result<?> save(@RequestBody Category category) {
categoryService.save(category);
return Result.success();
}
@PutMapping
public Result<?> update(@RequestBody Category category) {
categoryService.updateById(category);
return Result.success();
}
@DeleteMapping("/{id}")
public Result<?> delete(@PathVariable Long id) {
categoryService.removeById(id);
return Result.success();
}
@GetMapping("/{id}")
public Result<?> findById(@PathVariable Long id) {
return Result.success(categoryService.getById(id));
}
@GetMapping
public Result<?> findAll() {
List<Category> list = categoryService.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<Category> query = Wrappers.<Category>lambdaQuery().orderByDesc(Category::getId);
if (StrUtil.isNotBlank(name)) {
query.like(Category::getName, name);
}
IPage<Category> page = categoryService.page(new Page<>(pageNum, pageSize), query);
return Result.success(page);
}
}
如果也想学习本系统,下面领取。关注并回复:162springboot