从零到一手把手教你用SpringBootMyBatis搭建苍穹外卖后端含Git版本控制与Nginx配置在当今快节奏的餐饮行业数字化转型浪潮中一套高效稳定的外卖系统后端架构已成为餐饮企业的核心竞争力。本文将带你从零开始使用SpringBoot和MyBatis构建一个完整的苍穹外卖后端系统涵盖多模块工程划分、Git版本控制、Nginx配置等实战环节。无论你是刚接触Java后端开发的初学者还是希望提升工程化实践能力的开发者这篇指南都将为你提供清晰的实现路径。1. 项目初始化与多模块架构设计现代Java项目往往采用多模块架构来提升代码的可维护性和复用性。对于苍穹外卖这样的商业级项目合理的模块划分能够有效隔离业务边界降低代码耦合度。1.1 创建Maven父工程首先使用Spring Initializr创建一个标准的SpringBoot项目作为父工程mvn archetype:generate -DgroupIdcom.sky -DartifactIdsky-take-out -DarchetypeArtifactIdmaven-archetype-quickstart -DinteractiveModefalse在pom.xml中定义模块继承关系和统一依赖管理modules modulesky-common/module modulesky-pojo/module modulesky-server/module /modules dependencyManagement dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-dependencies/artifactId version2.7.0/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement1.2 核心模块功能划分模块名称主要职责典型内容示例sky-common公共组件封装工具类、异常处理、常量定义sky-pojo数据模型定义Entity、DTO、VO等数据传输对象sky-server业务逻辑实现Controller、Service、Mapper层代码sky-common模块建议包含以下包结构├── constant # 系统常量定义 ├── context # 线程上下文管理 ├── exception # 自定义异常体系 ├── utils # 通用工具类集合 └── result # 统一响应格式封装2. MyBatis集成与数据层开发MyBatis作为轻量级ORM框架在复杂SQL场景下展现出明显优势。以下是整合过程中的关键步骤2.1 数据源与MyBatis配置在application.yml中配置数据源和MyBatis参数spring: datasource: url: jdbc:mysql://localhost:3306/sky_take_out?useSSLfalse username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: true2.2 动态SQL编写技巧在Mapper XML中使用MyBatis动态SQL实现灵活查询select idqueryDishes resultTypeDishVO SELECT * FROM dish where if testcategoryId ! null AND category_id #{categoryId} /if if testname ! null and name ! AND name LIKE CONCAT(%,#{name},%) /if if teststatus ! null AND status #{status} /if /where ORDER BY update_time DESC /select提示使用MapperScan注解批量扫描Mapper接口避免每个接口都添加Mapper注解3. Git版本控制实战规范的版本控制是团队协作的基础。以下是Git工作流的具体实施建议3.1 初始化本地仓库# 初始化本地仓库 git init # 创建.gitignore文件 echo /target/ /.idea/ *.iml .gitignore # 提交初始代码 git add . git commit -m init: 项目初始化3.2 分支管理策略master生产环境对应分支仅接受经过测试的合并请求develop集成开发分支功能开发完成后的合并目标feature/功能开发分支按功能模块创建hotfix/紧急修复分支用于生产环境问题快速修复# 创建并切换到新功能分支 git checkout -b feature/user-management # 开发完成后合并到develop分支 git checkout develop git merge --no-ff feature/user-management4. Nginx高级配置技巧Nginx作为反向代理服务器能显著提升系统性能和安全性。4.1 反向代理配置server { listen 80; server_name api.sky.com; location /api/ { proxy_pass http://localhost:8080/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # 静态资源缓存设置 location ~* \.(jpg|png|css|js)$ { expires 7d; add_header Cache-Control public; } } }4.2 负载均衡策略对比策略类型实现方式适用场景轮询(默认)请求按顺序分配到各服务器服务器性能均衡的环境权重(weight)性能高的服务器分配更多请求服务器配置不均的环境IP哈希(ip_hash)同一IP固定访问同一服务器需要会话保持的应用upstream backend { server 192.168.1.101:8080 weight3; server 192.168.1.102:8080; server 192.168.1.103:8080 backup; keepalive 32; # 保持长连接数量 }5. 接口文档与Swagger集成良好的API文档能极大提升前后端协作效率。Knife4j作为Swagger的增强工具提供了更友好的文档界面。5.1 Knife4j配置类示例Configuration EnableSwagger2 public class SwaggerConfig { Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage(com.sky.controller)) .paths(PathSelectors.any()) .build() .securitySchemes(securitySchemes()); } private ListApiKey securitySchemes() { return Arrays.asList( new ApiKey(Authorization, Authorization, header)); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(苍穹外卖API文档) .description(接口说明) .version(1.0) .build(); } }5.2 接口注释规范示例Api(tags 菜品管理) RestController RequestMapping(/dish) public class DishController { ApiOperation(新增菜品) PostMapping public ResultLong save(RequestBody DishDTO dishDTO) { // 方法实现... } ApiOperation(value 菜品分页查询, notes 可根据分类、名称筛选) GetMapping(/page) public ResultPageResult page(DishPageQueryDTO queryDTO) { // 方法实现... } }在实际项目中我们通常会遇到各种边界情况。比如在实现购物车功能时需要特别注意并发场景下的数据一致性问题。通过Redis分布式锁可以有效解决这类问题但也要权衡性能与一致性的平衡。