在实际 Java Web 项目开发中尤其是计算机专业的毕业设计环节选择一个既能体现技术栈整合能力又具备实际应用场景的课题至关重要。基于 SpringBoot 的手作产品交易平台微信小程序正是这样一个集后端服务、前端交互、移动应用和数据库设计于一体的综合性项目。它不仅要求开发者掌握 SpringBoot 的核心开发流程还需要理解微信小程序的开发规范、前后端数据交互协议以及电商平台的基础业务逻辑。对于即将毕业的学生而言完成这样一个项目能够系统性地串联起 Java 编程、SpringBoot 框架、MySQL 数据库、RESTful API 设计以及微信小程序开发等多方面知识为简历增添一个分量十足的实战案例。本文将围绕如何从零开始构建一个“手作产品交易平台”微信小程序的后台管理系统及 API 服务展开。我们将以 SpringBoot 作为后端框架详细讲解环境搭建、数据库设计、核心业务模块开发、API 接口实现并最终与微信小程序前端进行联调。文章会重点剖析开发过程中容易遇到的配置问题、业务逻辑难点和联调陷阱并提供可运行的源码片段和排查思路。无论你是正在寻找毕设选题的学生还是希望学习 SpringBoot 整合微信小程序开发的开发者都能通过本文获得一个清晰、可复现的实现路径。1. 理解项目架构与技术选型在动手编码之前必须先厘清整个项目的技术架构和各组件职责。一个典型的手作产品交易平台会分为微信小程序前端和 SpringBoot 后端两部分它们通过 HTTP API 进行通信。1.1 核心业务模块分析手作产品交易平台的核心业务围绕“商品”和“交易”展开。我们需要设计以下主要功能模块用户模块包括微信用户登录、注册绑定手机号、个人信息管理收货地址等。商品模块手工艺品的分类、列表展示、详情查看、搜索、收藏。订单模块购物车、下单、支付模拟或集成微信支付、订单状态管理待付款、待发货、待收货、已完成、评价。后台管理模块商品上架/下架、订单处理、用户管理、数据统计通常是一个独立的 Web 管理系统或集成在 API 中供特定角色调用。对于毕业设计实现用户、商品、订单这三个核心模块的增删改查CRUD及基本业务流程已经能够完整展示开发能力。1.2 技术栈说明与选型理由后端框架SpringBoot 2.x它极大地简化了 Spring 应用的初始搭建和开发过程内嵌 Tomcat无需打包成 WAR 部署非常适合快速构建 RESTful API。选择它而非传统的 SSMSpringSpringMVCMyBatis组合能让你更专注于业务逻辑而非配置。数据持久层MyBatis-Plus作为 MyBatis 的增强工具它提供了通用的 Mapper 和 Service 封装可以少写很多基础的 SQL 语句极大提高开发效率。对于毕业设计这类数据模型相对固定的项目非常友好。数据库MySQL 8.0关系型数据库是存储用户、商品、订单等结构化数据的标准选择。8.0 版本在性能和功能上都有提升但使用 5.7 版本也完全可行需注意驱动兼容性。缓存可选Redis用于缓存热点数据如商品信息、存储用户会话Session或购物车数据。在毕设中引入 Redis 可以体现对性能优化的考虑但非必须。API 文档Swagger2/Knife4j自动生成和测试 API 文档的工具。集成后前端开发和 API 调试会非常方便也能让答辩老师清晰地看到你的接口设计。微信小程序前端使用微信开发者工具基于 JavaScript/WXML/WXSS 开发。后端需要提供符合微信小程序调用规范的 HTTPS API。注意微信小程序要求后端 API 必须通过 HTTPS 访问。在开发阶段可以使用内网穿透工具如 ngrok、natapp将本地服务暴露到公网并配置免费的 SSL 证书进行测试。生产环境则需要购买域名和正规的 SSL 证书。2. 开发环境准备与项目初始化一个稳定且一致的开发环境是项目顺利推进的基础。以下清单列出了必须准备的组件及其推荐版本。环境/工具推荐版本说明验证命令JDK1.8 或 11SpringBoot 2.x 对 JDK 8 兼容性最好java -versionMaven3.6用于项目构建和依赖管理mvn -vIDEIntelliJ IDEA社区版即可对 SpringBoot 支持好-MySQL5.7 或 8.0提前安装并启动服务mysql -VRedis (可选)5.0如需缓存或微信会话管理redis-cli ping微信开发者工具最新稳定版用于小程序前端开发和调试-内网穿透工具ngrok/natapp开发阶段使小程序能访问本地后端-2.1 使用 Spring Initializr 初始化项目最快捷的方式是使用 IntelliJ IDEA 内置的 Spring Initializr 或访问 start.spring.io 网站生成项目骨架。选择依赖在生成器中选择以下依赖Spring Web用于构建 Web 应用和 RESTful API。MyBatis Framework或直接搜索MyBatis-Plus如果网站支持首选 MyBatis-Plus。MySQL DriverMySQL 数据库驱动。Lombok通过注解简化 Java Bean 的编写如自动生成 getter/setter。Spring Boot DevTools开发工具支持热部署。生成项目填写 Group如com.handcraft、Artifact如market-api选择 Java 版本8 或 11然后生成并下载项目压缩包。导入 IDE解压后用 IntelliJ IDEA 打开pom.xml文件等待 Maven 自动下载依赖。如果 Initializr 没有 MyBatis-Plus需手动在pom.xml中添加依赖。2.2 手动添加 MyBatis-Plus 依赖打开项目中的pom.xml文件在dependencies节点内添加以下内容!-- MyBatis-Plus 启动器 -- dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.5.2/version !-- 请使用当时最新稳定版 -- /dependency !-- MySQL 驱动 (如果Initializr未勾选) -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId scoperuntime/scope /dependency !-- Lombok (如果Initializr未勾选) -- dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency添加后点击 IDEA 右上角的 Maven 刷新按钮下载新依赖。2.3 基础配置与数据库连接在src/main/resources目录下找到或创建application.yml文件YAML 格式更清晰。进行基础配置server: port: 8080 # 服务启动端口 servlet: context-path: /api # API统一前缀可选 spring: application: name: handcraft-market datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/handcraft_db?useUnicodetruecharacterEncodingUTF-8serverTimezoneAsia/Shanghai username: root password: your_password # 替换为你的数据库密码 redis: # 如果引入了Redis host: localhost port: 6379 password: # 如果没有密码则留空 database: 0 # MyBatis-Plus 配置 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制台打印SQL开发时有用 global-config: db-config: id-type: auto # 主键策略AUTO代表数据库自增 logic-delete-field: deleted # 全局逻辑删除字段名如果要用 logic-delete-value: 1 # 逻辑已删除值 logic-not-delete-value: 0 # 逻辑未删除值 mapper-locations: classpath*:/mapper/**/*.xml # XML映射文件位置 # 日志级别方便调试 logging: level: com.handcraft.market.mapper: debug # 将你的mapper包路径改为debug级别关键点解释serverTimezoneAsia/Shanghai是解决中文环境下 MySQL 驱动时区问题的常见配置。mybatis-plus.configuration.log-impl配置可以在控制台看到 MyBatis-Plus 执行的 SQL是调试排错的神器生产环境应关闭。配置完成后在 MySQL 中创建数据库handcraft_dbCREATE DATABASE IF NOT EXISTS handcraft_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;3. 数据库设计与实体类映射良好的数据库设计是项目的基石。我们围绕核心业务设计几张基础表。3.1 核心表结构设计以下是简化版的核心表设计用于支撑基本功能用户表 (user)CREATE TABLE user ( id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 主键ID, openid varchar(100) DEFAULT NULL COMMENT 微信用户唯一标识, nickname varchar(100) DEFAULT NULL COMMENT 微信昵称, avatar_url varchar(500) DEFAULT NULL COMMENT 微信头像, phone varchar(20) DEFAULT NULL COMMENT 手机号, create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT 创建时间, update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 更新时间, PRIMARY KEY (id), UNIQUE KEY uk_openid (openid) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COMMENT用户表;商品表 (product)CREATE TABLE product ( id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 主键ID, category_id int(11) DEFAULT NULL COMMENT 分类ID, name varchar(200) NOT NULL COMMENT 商品名称, description text COMMENT 商品描述, price decimal(10,2) NOT NULL COMMENT 价格, stock int(11) NOT NULL DEFAULT 0 COMMENT 库存, main_image varchar(500) DEFAULT NULL COMMENT 主图, detail_images text COMMENT 详情图(JSON数组), status tinyint(4) NOT NULL DEFAULT 1 COMMENT 状态1-上架0-下架, create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT 创建时间, update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 更新时间, PRIMARY KEY (id) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COMMENT商品表;订单表 (order)CREATE TABLE order ( id varchar(32) NOT NULL COMMENT 订单号自定义规则如时间戳随机数, user_id bigint(20) NOT NULL COMMENT 用户ID, total_amount decimal(10,2) NOT NULL COMMENT 订单总金额, status tinyint(4) NOT NULL DEFAULT 0 COMMENT 状态0-待付款1-待发货2-待收货3-已完成4-已取消, address varchar(500) NOT NULL COMMENT 收货地址, receiver varchar(50) NOT NULL COMMENT 收货人, phone varchar(20) NOT NULL COMMENT 收货电话, create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT 创建时间, update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 更新时间, PRIMARY KEY (id), KEY idx_user_id (user_id) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COMMENT订单表;注意order是 MySQL 关键字在实际创建时建议使用反引号包裹order或者换一个表名如order_info。3.2 使用 MyBatis-Plus 生成实体类与 MapperMyBatis-Plus 提供了强大的代码生成器但为了理解过程我们先手动创建。创建实体类在src/main/java/com/handcraft/market/entity包下创建User.java。package com.handcraft.market.entity; import com.baomidou.mybatisplus.annotation.*; import lombok.Data; import java.util.Date; Data TableName(user) // 指定表名 public class User { TableId(type IdType.AUTO) // 主键自增 private Long id; private String openid; private String nickname; private String avatarUrl; // 驼峰命名MyBatis-Plus默认会映射为 avatar_url private String phone; TableField(fill FieldFill.INSERT) // 插入时自动填充 private Date createTime; TableField(fill FieldFill.INSERT_UPDATE) // 插入和更新时自动填充 private Date updateTime; }同理创建Product和Order实体类。注意字段名与数据库列名的映射规则驼峰转下划线。创建 Mapper 接口在com.handcraft.market.mapper包下创建UserMapper.java。package com.handcraft.market.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.handcraft.market.entity.User; import org.apache.ibatis.annotations.Mapper; Mapper // 重要让Spring扫描到 public interface UserMapper extends BaseMapperUser { // 继承了BaseMapper就拥有了基本的CRUD方法无需编写XML }配置自动填充处理器为了自动处理create_time和update_time需要创建一个元对象处理器。package com.handcraft.market.config; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.util.Date; Component public class MyMetaObjectHandler implements MetaObjectHandler { Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, createTime, Date.class, new Date()); this.strictInsertFill(metaObject, updateTime, Date.class, new Date()); } Override public void updateFill(MetaObject metaObject) { this.strictUpdateFill(metaObject, updateTime, Date.class, new Date()); } }4. 实现核心业务逻辑与 RESTful API我们将以实现用户登录和商品查询这两个最基础的 API 为例展示如何组织 Service 层和 Controller 层。4.1 微信用户登录流程剖析微信小程序登录流程是前后端分离项目的典型场景。其核心是后端通过微信服务器验证code换取openid和session_key。小程序端调用wx.login()获取临时登录凭证code。小程序端将code发送到你的后端服务器。后端服务器使用appid、secret和code调用微信接口https://api.weixin.qq.com/sns/jscode2session换取openid和session_key。后端服务器根据openid判断用户是否存在。若不存在则在数据库中创建新用户记录。后端服务器生成一个自定义的登录态令牌如 JWT 或一个随机 Token将其与openid、session_key的关联关系存储在 Redis或数据库中。后端服务器将自定义令牌返回给小程序端。小程序端后续请求在 Header 中携带此令牌后端通过令牌识别用户身份。4.2 实现用户登录 Service 与 Controller首先在application.yml中配置微信小程序的 AppID 和 AppSecret。wechat: mp: app-id: your_appid secret: your_secret创建一个配置类来读取这些属性package com.handcraft.market.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; Data Component ConfigurationProperties(prefix wechat.mp) public class WechatMpProperties { private String appId; private String secret; }创建 Service 接口和实现类package com.handcraft.market.service; import com.handcraft.market.entity.User; public interface UserService { /** * 微信登录 * param code 小程序端传来的code * return 用户信息或包含token的对象 */ User wechatLogin(String code); }package com.handcraft.market.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.handcraft.market.config.WechatMpProperties; import com.handcraft.market.entity.User; import com.handcraft.market.mapper.UserMapper; import com.handcraft.market.service.UserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; Slf4j Service public class UserServiceImpl implements UserService { Autowired private UserMapper userMapper; Autowired private WechatMpProperties wechatMpProperties; Autowired private RestTemplate restTemplate; // 需要配置RestTemplate Bean Override public User wechatLogin(String code) { // 1. 构建请求URL String url https://api.weixin.qq.com/sns/jscode2session?appid{appid}secret{secret}js_code{code}grant_typeauthorization_code; MapString, String params new HashMap(); params.put(appid, wechatMpProperties.getAppId()); params.put(secret, wechatMpProperties.getSecret()); params.put(code, code); // 2. 调用微信接口 ResponseEntityMap response restTemplate.getForEntity(url, Map.class, params); MapString, Object result response.getBody(); log.info(微信登录响应: {}, result); // 3. 处理响应 String openid (String) result.get(openid); if (openid null) { String errcode String.valueOf(result.get(errcode)); String errmsg (String) result.get(errmsg); throw new RuntimeException(微信登录失败: errcode - errmsg); } // 4. 根据openid查询或创建用户 LambdaQueryWrapperUser queryWrapper new LambdaQueryWrapper(); queryWrapper.eq(User::getOpenid, openid); User user userMapper.selectOne(queryWrapper); if (user null) { // 新用户创建记录 user new User(); user.setOpenid(openid); // 注意此时还没有昵称和头像可能需要用户后续授权获取 userMapper.insert(user); } // 5. 生成自定义Token此处简化直接返回用户对象。实际应生成JWT或UUID存Redis // String token generateToken(openid); // user.setToken(token); return user; } }创建 Controller 暴露 APIpackage com.handcraft.market.controller; import com.handcraft.market.common.Result; import com.handcraft.market.entity.User; import com.handcraft.market.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; RestController RequestMapping(/user) public class UserController { Autowired private UserService userService; PostMapping(/login) public ResultUser login(RequestParam String code) { try { User user userService.wechatLogin(code); return Result.success(user); } catch (Exception e) { return Result.error(e.getMessage()); } } }其中Result是一个通用的响应封装类package com.handcraft.market.common; import lombok.Data; Data public class ResultT { private Integer code; private String msg; private T data; public static T ResultT success(T data) { ResultT result new Result(); result.setCode(200); result.setMsg(success); result.setData(data); return result; } public static T ResultT error(String message) { ResultT result new Result(); result.setCode(500); result.setMsg(message); return result; } }4.3 实现商品分页查询 API商品列表查询是电商平台的高频操作通常需要支持分页、按分类筛选、按关键词搜索。创建 ProductServicepackage com.handcraft.market.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.handcraft.market.entity.Product; public interface ProductService { PageProduct listProducts(Integer pageNum, Integer pageSize, Integer categoryId, String keyword); }实现 ProductServiceImplpackage com.handcraft.market.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.handcraft.market.entity.Product; import com.handcraft.market.mapper.ProductMapper; import com.handcraft.market.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; Service public class ProductServiceImpl implements ProductService { Autowired private ProductMapper productMapper; Override public PageProduct listProducts(Integer pageNum, Integer pageSize, Integer categoryId, String keyword) { PageProduct page new Page(pageNum, pageSize); LambdaQueryWrapperProduct queryWrapper new LambdaQueryWrapper(); // 状态为上架的商品 queryWrapper.eq(Product::getStatus, 1); // 按分类筛选 if (categoryId ! null categoryId 0) { queryWrapper.eq(Product::getCategoryId, categoryId); } // 按关键词搜索商品名称或描述 if (StringUtils.hasText(keyword)) { queryWrapper.like(Product::getName, keyword) .or() .like(Product::getDescription, keyword); } // 按创建时间倒序排列 queryWrapper.orderByDesc(Product::getCreateTime); return productMapper.selectPage(page, queryWrapper); } }创建 ProductControllerpackage com.handcraft.market.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.handcraft.market.common.Result; import com.handcraft.market.entity.Product; import com.handcraft.market.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; RestController RequestMapping(/product) public class ProductController { Autowired private ProductService productService; GetMapping(/list) public ResultPageProduct list( RequestParam(defaultValue 1) Integer pageNum, RequestParam(defaultValue 10) Integer pageSize, RequestParam(required false) Integer categoryId, RequestParam(required false) String keyword) { PageProduct page productService.listProducts(pageNum, pageSize, categoryId, keyword); return Result.success(page); } }5. 运行验证与 API 测试完成核心代码后需要启动服务并进行测试。5.1 启动 SpringBoot 应用在 IDEA 中找到主启动类通常命名为MarketApplication或Application运行其main方法。观察控制台日志如果没有报错并且看到Tomcat started on port(s): 8080类似的日志说明后端服务启动成功。5.2 配置并测试微信登录 API由于微信登录 API 需要真实的 AppID 和 AppSecret在开发测试阶段我们可以先模拟这个流程或者使用微信提供的测试号。方案一使用微信测试号访问微信公众平台测试账号系统获取测试号的appID和appsecret。将这两个值配置到application.yml中。修改小程序端的appid为测试号appID。此时调用/user/login接口就能正常换取openid。方案二模拟登录用于快速验证后端逻辑在开发初期可以暂时绕过微信验证直接模拟一个用户登录。// 在UserServiceImpl中临时添加一个模拟登录方法 public User mockLogin(String mockOpenid) { LambdaQueryWrapperUser queryWrapper new LambdaQueryWrapper(); queryWrapper.eq(User::getOpenid, mockOpenid); User user userMapper.selectOne(queryWrapper); if (user null) { user new User(); user.setOpenid(mockOpenid); user.setNickname(测试用户); userMapper.insert(user); } return user; }并在 Controller 中增加一个测试接口。切记在正式上线前移除此模拟接口。5.3 使用 Postman 或 Swagger 测试商品 API集成 Swagger2 可以方便地在线测试 API。添加依赖dependency groupIdio.springfox/groupId artifactIdspringfox-swagger2/artifactId version2.9.2/version /dependency dependency groupIdio.springfox/groupId artifactIdspringfox-swagger-ui/artifactId version2.9.2/version /dependency配置 Swagger2package com.handcraft.market.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; Configuration EnableSwagger2 public class SwaggerConfig { Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage(com.handcraft.market.controller)) // 扫描的包路径 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(手作产品交易平台 API 文档) .description(毕业设计项目后端接口文档) .version(1.0) .build(); } }访问与测试启动应用后访问http://localhost:8080/swagger-ui.html。你可以看到所有定义的 Controller 和接口。直接在页面上尝试调用/product/list接口查看返回的分页数据格式是否正确。5.4 小程序端调用测试在小程序端的app.js或对应页面的.js文件中编写调用登录 API 的代码。// 小程序端登录示例 wx.login({ success(res) { if (res.code) { wx.request({ url: https://your-ngrok-domain.com/api/user/login, // 替换为你的后端公网地址 method: POST, data: { code: res.code }, success(res) { console.log(登录成功, res.data); if (res.data.code 200) { const userInfo res.data.data; // 将用户信息或token存储到本地缓存 wx.setStorageSync(userInfo, userInfo); wx.setStorageSync(token, userInfo.token); // 如果后端返回了token } else { wx.showToast({ title: res.data.msg, icon: none }); } }, fail(err) { console.error(登录请求失败, err); } }); } else { console.error(登录失败, res.errMsg); } } });6. 常见问题排查与解决方案在开发联调过程中你几乎一定会遇到下面这些问题。6.1 数据库连接失败现象应用启动时抛出Communications link failure或Access denied for user异常。排查检查application.yml中的数据库 URL、用户名、密码是否正确。确认 MySQL 服务是否已启动 (sudo systemctl status mysql或查看服务列表)。检查防火墙是否阻止了 3306 端口。确认 MySQL 用户是否有从本地或指定 IP 连接的权限。解决修正配置或执行GRANT ALL PRIVILEGES ON handcraft_db.* TO root% IDENTIFIED BY password; FLUSH PRIVILEGES;生产环境慎用。6.2 MyBatis-Plus 扫描不到 Mapper现象启动报错Field xxxMapper in xxxService required a bean of type xxxMapper that could not be found.排查检查 Mapper 接口是否加了Mapper注解。检查启动类是否加了MapperScan(com.handcraft.market.mapper)注解如果没加Mapper则必须加这个。检查包路径是否在SpringBootApplication主类所在的包或其子包下。解决确保注解和包路径正确。最稳妥的方式是两者都做接口加Mapper主类加MapperScan。6.3 微信登录 API 返回40029错误码现象调用jscode2session接口返回{“errcode”:40029,”errmsg”:”invalid code”}。排查code已被使用过或过期有效期5分钟。确保小程序端每次调用wx.login()获取新的code再传给后端。appid和secret配置错误。仔细核对微信公众平台或测试号的配置。网络问题导致请求未到达微信服务器。解决在小程序端确保每次登录都获取新code核对配置检查网络代理设置。6.4 小程序端请求后端 API 报404或500现象小程序开发者工具 Network 面板显示请求失败。排查404URL 拼写错误、后端服务未启动、context-path配置未包含在 URL 中。500后端代码抛出未捕获的异常。查看 SpringBoot 控制台日志通常有详细的堆栈信息。跨域问题 (CORS)如果后端已响应但小程序端提示失败可能是跨域问题。SpringBoot 需要配置 CORS。解决对于 CORS添加一个全局配置类Configuration public class CorsConfig implements WebMvcConfigurer { Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(/**) .allowedOriginPatterns(*) // 允许所有域名生产环境应指定具体域名 .allowedMethods(GET, POST, PUT, DELETE, OPTIONS) .allowCredentials(true) .maxAge(3600); } }仔细阅读控制台日志根据错误信息修改代码。6.5 分页查询返回所有数据不分页现象调用/product/list接口返回了数据库中所有商品而不是指定的每页10条。排查MyBatis-Plus 的分页插件没有配置。解决在 SpringBoot 主配置类或单独的配置类中添加分页插件 Bean。Configuration public class MybatisPlusConfig { Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }7. 项目优化与扩展方向完成基础功能后可以从以下几个方面深化项目提升其完整性和技术深度。7.1 安全性增强接口鉴权使用 JWT (JSON Web Token) 替代简单的自定义 Token。用户登录成功后后端生成一个签名的 JWT 返回给小程序小程序后续请求在AuthorizationHeader 中携带Bearer token。后端通过过滤器或拦截器验证 JWT 的有效性。敏感信息脱敏用户手机号、地址等在返回前端时进行部分隐藏。SQL 注入防护坚持使用 MyBatis-Plus 的 Wrapper 或#{}预编译方式避免手动拼接 SQL 字符串。XSS 防护对用户输入的内容如商品描述、评价进行转义或使用安全的富文本编辑器。7.2 性能与体验优化引入 Redis 缓存将热点数据如首页商品列表、分类信息缓存到 Redis设置合理的过期时间减轻数据库压力。数据库索引优化为order表的user_id、create_timeproduct表的category_id、status等查询条件字段添加索引。图片上传与存储商品图片不应直接存到服务器本地。应集成对象存储服务如阿里云 OSS、腾讯云 COS后端提供一个获取上传凭证的接口由小程序直传到对象存储存储成功后返回 URL 给后端保存。异步处理对于耗时的操作如发送订单成功通知短信、生成报表可以引入消息队列如 RabbitMQ进行异步处理提高主流程响应速度。7.3 业务功能扩展集成微信支付这是电商项目的核心。需仔细阅读微信支付文档实现统一下单、支付回调、订单状态更新等完整流程。注意处理回调验证和幂等性。实现完整的购物车购物车数据可以存储在 Redis 中以用户ID为Key包含商品ID、数量、选中状态等。后台管理系统可以新建一个 SpringBoot 子模块或使用现成的 Admin 模板如 VueElement UI通过 RESTful API 管理商品、订单、用户。需要实现管理员登录和权限控制。搜索功能强化当商品量变大时使用数据库LIKE查询效率低下。可以引入 Elasticsearch 或 MeiliSearch 实现全文检索。添加评价与评分系统允许用户对已完成订单的商品进行评价和打分并计算商品的平均分。7.4 部署与监控多环境配置使用application-dev.yml,application-prod.yml区分开发和生产环境配置。日志收集使用 Logback 或 Log4j2 配置日志文件滚动策略并将错误日志发送到监控平台。健康检查Spring Boot Actuator 可以提供应用健康状态、指标等信息便于监控。容器化部署编写 Dockerfile将应用打包成 Docker 镜像使用 Docker Compose 或 Kubernetes 进行部署这能极大简化环境依赖和部署流程。这个基于 SpringBoot 的手作产品交易平台项目从技术选型到环境搭建从数据库设计到核心 API 实现再到联调测试和问题排查覆盖了一个 Java Web 后端工程师需要掌握的核心工作流。作为毕业设计在实现上述基础功能后选择一两个扩展方向进行深入并撰写清晰的设计文档和部署手册足以展示出扎实的工程实践能力。在开发过程中养成查看官方文档、阅读日志、使用调试工具的习惯比单纯复制代码更为重要。