• JSD-2204-创建csmall项目-Day02


    1.创建csmall项目

    我们要搭建一个项目,用于学习各种微服务知识

    搭建的过程有很多新的标准,需要我们掌握和学习

    发给大家的3个csmall的项目

    csmall-finish.zip:这个项目是当前学习过程中需要使用的项目,有些配置和固定的代码,可以从中复制

    csmall-mobile-repo.zip:酷鲨商城前台前端项目

    csmall-jsd2203.zip:酷鲨商城前台后端项目模板

    1.1业务概述

    我们通过学习电商网站添加订单的业务来学习需要使用到的微服务组件

    我们模拟用户选中了购物车中的商品后,点击"确认订单"按钮后的业务操作

    1.减少用户选中商品的库存数量(sku)

    2.删除用户购物车中勾选的商品

    3.生成订单,将订单信息保存到数据库

    我们将上面3个操作设计为3个模块完成

    • 库存模块:减少库存
    • 购物车模块:删除购物车信息
    • 订单模块:新增订单

    除此之外,我们还要创建一个项目对新增订单的业务进行触发操作

    • 触发模块:business

    1.2创建csmall父项目

    创建项目名称csmall

    我们微服务开发过程中,一般都会使用一个Idea中包含多个项目的形式

    这个形式就是先创建一个"父项目",再在这个父项目中创建多个子项目的操作

    我们首先创建一个项目:csmall

    注意细节的修改

             

    首先,将当前csmall项目修剪为父项目的格式

    • 删除csmall项目的src文件夹,因为父项目不写代码
    • 修改pom文件

    最终csmall的pom文件如下

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.4version>
    9. <relativePath/>
    10. parent>
    11. <groupId>cn.tedugroupId>
    12. <artifactId>csmallartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>csmallname>
    15. <description>Demo project for Spring Bootdescription>
    16. <packaging>pompackaging>
    17. project>

    csmall-jsd2204: 学习微服务项目的简单示例

    1.3创建子项目

    创建csmall-stock项目

             

    我们每次创建一个子项目之后

    都要进行"父子相认"

    在父项目的pom文件中,编写子项目的存在

    1. <packaging>pompackaging>
    2. <modules>
    3. <module>csmall-stockmodule>
    4. modules>

    还需要在子项目的pom文件中对父项目进行继承操作

    父项目的第11行到第13行

    复制到子项目的第6行到第8行

    子项目pom文件修改后

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>cn.tedugroupId>
    7. <artifactId>csmallartifactId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. <relativePath/>
    10. parent>
    11. <groupId>cn.tedugroupId>
    12. <artifactId>csmall-stockartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>csmall-stockname>
    15. <description>Demo project for Spring Bootdescription>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.bootgroupId>
    19. <artifactId>spring-boot-starterartifactId>
    20. dependency>
    21. dependencies>
    22. project>

    父子相认完成

    这样当前子项目就可以读取父项目中的pom文件信息了

    删除stock模块src\test文件夹

    1.4父项目管理依赖版本

    在我们现在使用maven添加依赖的认知中

    有些依赖时必须添加版本号才能执行

    有些依赖则不必添加版本号

    原因是我们继承的SpringBoot(2.5.9)父项目中,定义了一些常用依赖的版本号

    如果我们自己编写的父项目想定义我们项目中需要的依赖版本号的话,也是可以实现的

    这样做可以统一所有子项目的版本,在更新版本时,只需要修改父项目中定义的版本号即可

    父项目的pom文件添加如下内容

    1. <properties>
    2. <mybatis.version>2.2.2mybatis.version>
    3. properties>
    4. <dependencyManagement>
    5. <dependencies>
    6. <dependency>
    7. <groupId>org.mybatis.spring.bootgroupId>
    8. <artifactId>mybatis-spring-boot-starterartifactId>
    9. <version>${mybatis.version}version>
    10. dependency>
    11. dependencies>
    12. dependencyManagement>

    子项目中如果需要mybatis的依赖只需要添加如下内容即可,无需再指定版本号

    1. <dependency>
    2. <groupId>org.mybatis.spring.bootgroupId>
    3. <artifactId>mybatis-spring-boot-starterartifactId>
    4. dependency>

    上面的操作也称之为"锁版本"

    前端Vant项目git地址

    vant2204: 学习简单Vant的示例,了解Vant一般功能

    包含今天笔记的csmall项目

    csmall-jsd2204: 学习微服务项目的简单示例

    1.5加载正式项目pom文件

    因为我们学习微服务的过程中需要很多微服务相关的依赖

    这些依赖都需要在父项目中进行版本的管理的

    所以我们直接使用分享给大家的完整版项目的父项目pom文件即可

    父项目完整最终pom文件如下

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.9version>
    9. <relativePath/>
    10. parent>
    11. <groupId>cn.tedugroupId>
    12. <artifactId>csmallartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>csmallname>
    15. <description>Demo project for Spring Bootdescription>
    16. <packaging>pompackaging>