自学实前后端践项目4 MMall商城 1

article/2025/10/31 5:15:34

一.开发环境

1.JDK8以上+Spring Boot 2.3.0+Thymeleaf+MyBatis Plus3.3.1+MySQL8.0+

2.部署:Linux,,(阿里云 腾讯云)JDK8+,MySQL8.0

3.部署方式:jar包部署,不需要Tomcat

二.新建工程

1)创建工程

 

 

 

 创建完成

 2)添加依赖pom.xml中加上三个依赖

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.1.tmp</version>
</dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.3.1.tmp</version>
</dependency><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity</artifactId><version>1.7</version>
</dependency>

3)静态页面准备删除只留下static文件将静态页面相关代码复制进去

 链接:https://pan.baidu.com/s/1uk6l69F0mBehzguRV6JYzA 
提取码:sqsd

先写配置检测静态页面完整性

spring:thymeleaf:prefix: classpath:/static/suffix: .html

主页的映射必须加后台访问

 

package com.redhat.mmall002.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@Controller
public class RedirectHandler {@GetMapping("/{url}")public String redirect(@PathVariable("url") String url){return url;}
}

[1]启动项如果报错可以参考

原因:spring boot有对数据库的默认配置,spring boot会先去根据自己的配置获取数据库连接,下面这个注解是让spring不使用DataSourceAutoConfiguration,用我们自己的配置

解决方法:

配置

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
启动服务测试

 

 

 

 

 

 3)数据连接

链接:https://pan.baidu.com/s/1Tp6UTtTlj9b3hROKMxsG-w 
提取码:7lty

 4.编写代码自动生成器main类文件

创建如下Main类运行:

package com.redhat.mmall002;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import java.util.ArrayList;
import java.util.List;public class Main {public static void main(String[] args) {AutoGenerator autoGenerator = new AutoGenerator();DataSourceConfig dataSourceConfig = new DataSourceConfig();dataSourceConfig.setDbType(DbType.MYSQL);dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");dataSourceConfig.setUsername("root");dataSourceConfig.setPassword("123456");dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mmall?useUnicode=true&characterEncoding=UTF-8");autoGenerator.setDataSource(dataSourceConfig);GlobalConfig globalConfig = new GlobalConfig();globalConfig.setOpen(true);globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");globalConfig.setAuthor("红帽");globalConfig.setServiceName("%sService");autoGenerator.setGlobalConfig(globalConfig);PackageConfig packageConfig = new PackageConfig();packageConfig.setParent("com.redhat.mmall002");packageConfig.setEntity("entity");packageConfig.setMapper("mapper");packageConfig.setController("controller");packageConfig.setService("service");packageConfig.setServiceImpl("service.impl");autoGenerator.setPackageInfo(packageConfig);StrategyConfig strategyConfig = new StrategyConfig();strategyConfig.setEntityLombokModel(true);strategyConfig.setNaming(NamingStrategy.underline_to_camel);strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);List<TableFill> list = new ArrayList<>();TableFill tableFill1 = new TableFill("create_time",FieldFill.INSERT);TableFill tableFill2 = new TableFill("update_time",FieldFill.INSERT_UPDATE);list.add(tableFill1);list.add(tableFill2);strategyConfig.setTableFillList(list);autoGenerator.setStrategy(strategyConfig);autoGenerator.execute();}
}

得出如下文件成功

 5.创建测试类进行测试运行

1)将启动类还原需要用到访问数据库了

package com.redhat.mmall002;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication
@MapperScan("com.redhat.mmall002.mapper")
public class Mmall002Application {public static void main(String[] args) {SpringApplication.run(Mmall002Application.class, args);}
}
2)数据库配置文件application.yml
spring:thymeleaf:prefix: classpath:/static/suffix: .htmldatasource:url: jdbc:mysql://localhost:3306/mmall?useUnicode=true&characterEncoding=UTF-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3)创建测试类go-to方法ProductCategoryMapperTest

package com.redhat.mmall002.mapper;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ProductCategoryMapperTest {@Autowiredprivate ProductCategoryMapper mapper;@Testvoid test(){mapper.selectList(null).forEach(System.out::println);}
}

运行结果测试

 二。系统环境搭建

1)创建封装VO

package com.redhat.mmall002.vo;import lombok.Data;import java.util.List;@Data
public class ProductCategoryVO {private Integer id;private String name;private List<ProductCategoryVO> children;
}

2)ProductCategoryService中声明方法

public interface ProductCategoryService extends IService<ProductCategory> {public List<ProductCategoryVO> getAllProductCategoryVO();}

3)在ProductCategoryServiceImpl中重写这个没有的方法

@Autowired
private ProductCategoryMapper productCategoryMapper;@Override
public List<ProductCategoryVO> getAllProductCategoryVO() {//实体类转VOList<ProductCategory> productCategoryList = productCategoryMapper.selectList(null);//VOList<ProductCategoryVO> productCategoryVOList = new ArrayList<>();for (ProductCategory productCategory : productCategoryList){ProductCategoryVO productCategoryVO = new ProductCategoryVO();productCategoryVO.setId(productCategory.getId());productCategoryVO.setName(productCategory.getName());productCategoryVOList.add(productCategoryVO);}return null;
}

4.go-to创建新的ProductCategoryServiceImplTest 测试类

package com.redhat.mmall002.service.impl;import com.redhat.mmall002.service.ProductCategoryService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class ProductCategoryServiceImplTest {@Autowiredprivate ProductCategoryService service;@Testvoid getAllProductCategoryVO(){service.getAllProductCategoryVO();}}

测试结果

 

 

 

 

 5.实现三级分类

1)ProductCategoryVO类中增加ProductCategoryVO

package com.redhat.mmall002.vo;import lombok.Data;import java.util.List;@Data
public class ProductCategoryVO {private Integer id;private String name;private List<ProductCategoryVO> children;public ProductCategoryVO(Integer id, String name) {this.id = id;this.name = name;}
}

2)ProductCategoryServiceImpl中创建三级分类逻辑关系

package com.redhat.mmall002.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.redhat.mmall002.entity.ProductCategory;
import com.redhat.mmall002.mapper.ProductCategoryMapper;
import com.redhat.mmall002.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redhat.mmall002.vo.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;/*** <p>*  服务实现类* </p>** @author 红帽* @since 2021-11-24*/
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {@Autowiredprivate ProductCategoryMapper productCategoryMapper;@Overridepublic List<ProductCategoryVO> getAllProductCategoryVO() {
//        //实体类转VO
//        List<ProductCategory> productCategoryList = productCategoryMapper.selectList(null);
//        //VO
//        List<ProductCategoryVO> productCategoryVOList = new ArrayList<>();
//        for (ProductCategory productCategory : productCategoryList){
//            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
//            BeanUtils.copyProperties(productCategory,productCategoryVO);//优化操作productCategoryVO.setId(productCategory.getId());productCategoryVO.setName(productCategory.getName());
//            productCategoryVOList.add(productCategoryVO);
//        }//一级分类QueryWrapper wrapper = new QueryWrapper();wrapper.eq("type",1);List<ProductCategory> levelOne = productCategoryMapper.selectList(wrapper);List<ProductCategoryVO> levelOneVO = levelOne.stream().map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());for (ProductCategoryVO levelOneProductCategoryVO : levelOneVO){wrapper = new QueryWrapper();wrapper.eq("type",2);wrapper.eq("parent_id",levelOneProductCategoryVO.getId());List<ProductCategory> levelTwo = productCategoryMapper.selectList(wrapper);List<ProductCategoryVO> levelTwoVO = levelTwo.stream().map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());levelOneProductCategoryVO.setChildren(levelTwoVO);for (ProductCategoryVO levelTwoProductCategoryVO : levelTwoVO){wrapper = new QueryWrapper();wrapper.eq("type",3);wrapper.eq("parent_id",levelTwoProductCategoryVO.getId());List<ProductCategory> levelThree = productCategoryMapper.selectList(wrapper);List<ProductCategoryVO> levelThreeVO = levelThree.stream().map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());levelTwoProductCategoryVO.setChildren(levelThreeVO);}}
//        //方法一
//        List<ProductCategoryVO> levelOneVO = new  ArrayList<>();
//         for (ProductCategory productCategory : levelOne){
//             ProductCategoryVO productCategoryVO = new ProductCategoryVO();
//             BeanUtils.copyProperties(productCategory,productCategoryVO);
//             levelOneVO.add(productCategoryVO);
//         }return levelOneVO;}
}

3)运行测试类测试结果分三级分类

 4)实现前端页面调用找到公共类映射对应数据库数据commom.html中

<li th:each="levelOne:${list}"><div class="fj"><span class="n_img"></span> <span class="fl" th:text="${levelOne.name}"></span></div><div class="zj"><div class="zj_l"><div class="zj_l_c" th:each="levelTwo:${levelOne.children}"><h2><a href="/productList.html" th:text="${levelTwo.name}"></a></h2><a href="/productList.html" th:each="levelThree:${levelTwo.children}" th:text="${levelThree.name}"></a></div></div></div>
</li><li>

测试显示结果

 

 

 

 OK显示成功!

 


http://chatgpt.dhexx.cn/article/4JIfe9HR.shtml

相关文章

mmall电商项目学习笔记之mybatis三剑客

一.Mybatis plugin IDEA 2017.3版本下Mybatis plugin 3.53安装使用 插件下载地址 http://www.awei.org/download/iMybatis-3.21.jar 二.MyBatis-Generate 反向生成 【转】mybatis自动生成实体代码的插件 【method2】逆向生成 2.1 在pom.xml中做两处配置 2.1.1配置depen…

自学实践前后端项目4 MMall商城 4

一。实现商品详情展示 1.测试获取后台当个商品的信息 2.实现通过点击商品名称和商品图片进入商品详情页面 1&#xff09;查找出商品信息 2&#xff09;在前端进行映射 3&#xff09;设置库存选择限制 判断逻辑 $(function(){//给type绑定点击事件$(".type").click…

mmall 项目实战(一)项目初始化

1.创建 数据库 及 表 数据脚本&#xff1a; /* Navicat Premium Data Transfer Source Server : 182.92.82.103 Source Server Type : MySQL Source Server Version : 50173 Source Host : 182.92.82.103 Source Database : mmall Target Se…

B2C购物商城---MMALL商城概览

注意&#xff1a; 商品小图原图缺失后续上传到图片服务器。不影响使用支付宝二维码是支付宝沙箱开发环境生成&#xff0c;不会产生真实交易&#xff0c;若需体验&#xff0c;请下载支付宝沙箱版扫描体验项目仍需优化 本项目的完成参考了慕课网happymmall的设计 项目源码在&…

MMall项目完整分析总结

Linux服务器 线上环境&#xff1a; 1.jdk 2.vsftpd 3.nginx 4.mysql 5.tomcat 6.git 7.maven 8.Redis 项目采用Tomcat集群方式: 在此架构图中&#xff0c;nginx使用的是轮询的负载均衡策略。session不交给tomcat自己管理&#xff0c;已经交由左侧的redis分布式…

python flask-sqlalchemy flask-marshmallow基本使用

首先安装 pip install marshmallow-sqlalchemy pip install flask-sqlalchemy pip install flask-marshmallow 参考 sqlalchemy query 官网 app.py文件内容 from flask import Flask,jsonify import config from flask_sqlalchemy import SQLAlchemy from flask_marshmallow i…

Flask_使用flask_marshmallow序列化数据

代码如下&#xff1a; from flask import Flask from flask_marshmallow import Marshmallow from flask_sqlalchemy import SQLAlchemy from marshmallow import fieldsapp Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] "mysqlpymysql://root:12…

【Python】Marshmallow:Python中的“棉花糖”

博主&#xff1a;&#x1f44d;不许代码码上红 欢迎&#xff1a;&#x1f40b;点赞、收藏、关注、评论。 文章目录 一、Marshmallow简介1.1、基础概念 二、序列化2.1、User类2.2、UserSchema类2.3、Serializing(序列化)2.4、运行2.5、过滤输出 三、反序列化四、验证数据4.1、V…

marshmallow——简介

一、marshmallow简介 在marshmallow诞生之前,已经有很多优秀的模块来用于数据的格式化和数据校验中。 因此书写mashmallow这个库的作者受这些库的启发,例如Django REST Framework, Flask-RESTful, 和colander这些。他同样从这些库中大量借用了设计和实现序列化、反序列化以及…

flask---》Marshmallow介绍及基础使用

0. Marshmallow背景介绍 介绍 Marshmallow&#xff0c;中文译作&#xff1a;棉花糖。是一个轻量级的数据格式转换的模块&#xff0c;也叫序列化和反序列化模块&#xff0c;常用于将复杂的orm模型对象与python原生数据类型之间相互转换。一般用于flaskmarshmallow提供了丰富的…

YApi接口平台-接口挡板

YApi是一个开源的平台&#xff0c;官方平台链接&#xff0c;如下https://hellosean1025.github.io/yapi/index.html,目前很多大公司在使用&#xff0c;主要优势我认为有2个&#xff0c;第一该平台开源&#xff0c;搭建方便&#xff1b;第二该平台解决了前后端分离开发的痛点&am…

open source HTML 5移动应用 -Exlive 人员定位客户端(BlackBerry 10, Android, iPhone)

下图是exlive人员定位系统的宣传页&#xff0c;其官方主站在这里 www.exlive.cn 下图是BlackBerry OS 7.0上面的运行效果&#xff0c;更多截图见这里 http://blog.csdn.net/berryreload/article/details/8099674 Update: Remove BlackBerry OS 7.0支持 升级到PhoneGap 3.0&…

My BlackBerry

什么是黑莓 RIM公司成立于1984年。 黑莓手机&#xff08;Blackberry&#xff09;&#xff0c;是指由加拿大Reserach In Motion&#xff08;RIM&#xff09;公司推出的一种无线手持邮件解决终端设备&#xff0c;也就是我们平时称的手机。 2013年1月30日&#xff0c;RIM公司今…

解决联网下载服务端返回405问题

最近遇到了一个问题&#xff0c;在项目中有一个启动页广告图片下载的功能&#xff0c;之前能够正常下载&#xff0c;由于这个版本遇到了运营商DNS劫持的问题&#xff0c;服务端要调整图片的下载路径&#xff0c;也就是改变了图片的链接地址。修改地址之后下载就出现异常了&…

IIS 405 Method Not Allowed

今天将项目发布到IIS上后&#xff0c;发现Delete方法用不了&#xff0c;可进行如下设置&#xff1a; 打开处理程序映射 找到WebDAV点击请求限制&#xff0c;勾选全部谓词 这个方法如果无效的话 方法二&#xff1a;因为是WebDAVModule限制的请求&#xff0c;所以直接将WebDAV…

前端请求接口时报405错误

他说方法不存在 他的问题所在就是没有找到后端对应的接口 意思就是说没有找到我后端put这个对应的接口 我记得写了不知道为啥没了纳闷

IIS put请求 报HTTP Error 405 - Method Not Allowed

在新的服务器上部署了一个.net core的项目&#xff0c;部分请求地址使用了put、delete方式&#xff0c;导致无法正常请求&#xff0c;报Error 405 - Method Not Allowed。 由于配置IIS时把“WebDAV 发布”给勾选了&#xff0c;所以会导致拦截。 服务器和IIS 10配置如下图&…

405 Method Not Allowed 解决方案

背景描述 前端发送请求到后端&#xff0c;可是经过swagger测试&#xff0c;后端接口是没问题的 可是一旦调用就发生405报错 解决方案 一般这种情况发生是因为前后端请求方式不一致 写在前端的接口声明方式是post 我们来看后端 很明显了&#xff0c;后端是get&#xff0c;因…

405 Method Not Allowed(亲测,实用)

今天在发布一个网站的时候遇到 标题上的问题&#xff0c;一直不明白是为何&#xff0c;刚开始以为是我的程序写的有问题&#xff0c;随即将项目发给同事来发布试试&#xff0c;在他的IIS上发布却没出现问题&#xff0c;一切正常&#xff0c;这可就怪了&#xff0c;于是想到了应…