文章目录
- 一、项目演示
- 二、项目介绍
- 三、运行截图
- 四、主要代码
一、项目演示
项目演示地址: 视频地址
二、项目介绍
项目描述:这是一个基于SpringBoot框架开发的百货超市系统。首先,这是一个很适合SpringBoot初学者学习的项目,代码简洁规范,注释说明详细,易于理解和学习。其次,这项目功能完备,具有一个百货超市商城系统的所有基础功能。
项目功能:此项目分为两个角色:普通用户和管理员。普通用户有登录注册、浏览商品信息、添加购物车、提交订单、管理个人信息、管理个人订单等等功能。管理员有管理所有商品信息、管理所有订单信息、管理所有用户信息、查看收益数据图表等等功能。
应用技术:SpringBoot + MyBatis + Thymeleaf + Bootstrap + MySQL
运行环境:Eclipse/IntelliJ IDEA(项目压缩包中自带) + MySQL5.7(项目压缩包中自带) + Maven3.6.3(项目压缩包中自带) + JDK1.8(项目压缩包中自带)
三、运行截图
四、主要代码
1.提交订单代码:
/*** 结算订单操作* @param orderDTO* @param request* @return*/@Overridepublic ResponseDTO<Boolean> submitOrder(OrderDTO orderDTO, HttpServletRequest request) {// 进行统一表单验证CodeMsg validate = ValidateEntityUtil.validate(orderDTO);if (!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {return ResponseDTO.errorByMsg(validate);}User user = (User) request.getSession().getAttribute(SessionConstant.HOME_SESSION_LOGIN_KEY);CartExample cartExample = new CartExample();cartExample.createCriteria().andUserIdEqualTo(user.getId());List<Cart> cartList = cartMapper.selectByExample(cartExample);BigDecimal totalPrice = new BigDecimal(0);Order order = CopyUtil.copy(orderDTO, Order.class);order.setId(UuidUtil.getShortUuid());order.setUserId(user.getId());order.setDeleted(OrderDeleteEnum.NO.getCode());order.setCreateTime(new Date());List<OrderItem> orderItemList = new ArrayList<>();// 验证库存和计算总价for(Cart cart : cartList) {Product product = productMapper.selectByPrimaryKey(cart.getProductId());if(cart.getQuantity() > product.getStock()) {CodeMsg codeMsg = CodeMsg.PRODUCT_STOCK_OVER;codeMsg.setMsg("商品【" + product.getName() + "】库存不足!");return ResponseDTO.errorByMsg(codeMsg);}totalPrice = totalPrice.add(product.getPrice().multiply(new BigDecimal(cart.getQuantity())));OrderItem orderItem = new OrderItem();orderItem.setId(UuidUtil.getShortUuid());orderItem.setProductId(cart.getProductId());orderItem.setOrderId(order.getId());orderItem.setProductName(product.getName());orderItem.setProductPhoto(product.getPhoto());orderItem.setProductPrice(product.getPrice());orderItem.setQuantity(cart.getQuantity());orderItem.setSumPrice(new BigDecimal(cart.getQuantity()).multiply(product.getPrice()));orderItemList.add(orderItem);}// 订单详情落库for(OrderItem orderItem : orderItemList) {if(orderItemMapper.insertSelective(orderItem) == 0) {throw new RuntimeException(CodeMsg.ORDER_ADD_ERROR.getMsg());}}// 订单数据落地order.setState(OrderStateEnum.PAYED.getCode());order.setTotalPrice(totalPrice);if(orderMapper.insertSelective(order) == 0) {throw new RuntimeException(CodeMsg.ORDER_ADD_ERROR.getMsg());}// 清空购物车数据cartMapper.deleteByExample(cartExample);return ResponseDTO.successByMsg(true,"下单成功");}
2.前台用户登录代码:
/*** 前台用户登录操作* @param userDTO* @param request* @return*/@Overridepublic ResponseDTO<Boolean> homeLogin(UserDTO userDTO, HttpServletRequest request) {if(userDTO == null) {return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);}if(CommonUtil.isEmpty(userDTO.getUsername())) {return ResponseDTO.errorByMsg(CodeMsg.USERNAME_EMPTY);}if(CommonUtil.isEmpty(userDTO.getPassword())) {return ResponseDTO.errorByMsg(CodeMsg.PASSWORD_EMPTY);}UserExample userExample = new UserExample();userExample.createCriteria().andUsernameEqualTo(userDTO.getUsername()).andPasswordEqualTo(userDTO.getPassword());List<User> userList = userMapper.selectByExample(userExample);if(userList.size() == 0) {return ResponseDTO.errorByMsg(CodeMsg.USERNAME_PASSWORD_ERROR);}request.getSession().setAttribute(SessionConstant.HOME_SESSION_LOGIN_KEY, userList.get(0));return ResponseDTO.successByMsg(true, "登录成功!");}
3.保存商品信息代码:
/*** 保存商品信息* @param productDTO* @return*/@Overridepublic ResponseDTO<Boolean> save(ProductDTO productDTO) {if(productDTO == null) {return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);}// 进行统一表单验证CodeMsg validate = ValidateEntityUtil.validate(productDTO);if (!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {return ResponseDTO.errorByMsg(validate);}Product product = CopyUtil.copy(productDTO, Product.class);if(CommonUtil.isEmpty(product.getId())){// 添加操作product.setId(UuidUtil.getShortUuid());product.setCreateTime(new Date());if(productMapper.insertSelective(product) == 0){return ResponseDTO.errorByMsg(CodeMsg.PRODUCT_ADD_ERROR);}}else {// 修改操作if(productMapper.updateByPrimaryKeySelective(product) == 0){return ResponseDTO.errorByMsg(CodeMsg.PRODUCT_EDIT_ERROR);}}return ResponseDTO.success(true);}