当前位置: 首页 > news >正文

SpringCloud项目搭建快速入门

SpringCloud项目搭建

注意:本项目采用JDK17,且已上传至Gitee,可自行下载

父工程创建方式一

  • Step1: 创建一个Maven聚合工程ScDemo

    在这里插入图片描述

    创建完成后删除除pom.xml文件外的所有文件及目录

    在这里插入图片描述

  • Step2: 设置Mven为本地Maven

    博主已进行过修改,具体修改可详见Maven部分内容

    在这里插入图片描述

  • Step3: 在pom.xml文件中进行相关配置:

    • Step3-1: 根据实际要求自行修改<groupId><artifactId><version>的标签体内容
    • Step3-2: 设置聚合工程(即父模块)的打包方式为pom:<packaging>pom</packaging>
    • Step3-3: 利用<modules>标签设置子模块
      • 由于此时还未创建子模块,所以该<modules>标签体内为空,后续创建子模块后,会将子模块添加到该标签体内
    • Step3-4: 利用<properties>标签自定义属性
      • jdk版本为17
      • SpringBoot、SpringCloud、SpringAlibaba版本依次为:3.0.7、2022.0.3、2022.0.0.0-RC2
    • Step3-5: 利用<dependencies>标签配置公共依赖
    • Step3-6: 利用<dependencyManagement>标签配置可选依赖
    • Step3-7: 配置插件

    初始状态的pom.xml文件如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--1 根据需求自行修改以下三个标签--><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><!--2 设置聚合工程(即父模块)的打包方式为pom--><packaging>pom</packaging><!--3 设置子模块--><modules></modules><!--4 自定义属性--><properties><java.version>17</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-boot.version>3.0.7</spring-boot.version><spring-cloud.version>2022.0.3</spring-cloud.version><spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version></properties><!--5 配置公共依赖--><dependencies></dependencies><!--6 配置可选依赖--><dependencyManagement><dependencies><!-- springboot的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- springcloud依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--spring-cloud-alibaba--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><!--7 配置插件--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
    

父工程创建方式二

  • Step1: 创建一个SpringBoot聚合工程SCDemo

    在这里插入图片描述

    在这里插入图片描述

    创建完成后删除除pom.xml文件外的所有文件及目录

    在这里插入图片描述

  • Step2: 设置Mven为本地Maven

    博主已进行过修改,具体修改可详见Maven部分内容

    在这里插入图片描述

  • 生成的原始pom.xml文件,完整代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.itheima</groupId><artifactId>SCDemo</artifactId><version>0.0.1-SNAPSHOT</version><name>SCDemo</name><description>SCDemo</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version><spring-cloud.version>2024.0.0</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
    
  • 注意

    • 利用IDE生成的SpringBoot项目默认使用的SpringBoot、SpringCloud版本分别为3.4.1、2024.0.0

      由于SpringAlibaba最新版本并未与SpringBoot最新版本兼容,所以需要更改SpringBoot和SpringCloud的版本,使三者版本对应兼容

      所以建议使用的SpringBoot、SpringCloud、SpringAlibaba版本依次为

      SpringBootSpringCloudSpringAlibaba最低支持JDK版本
      3.0.72022.0.32022.0.0.0-RC2JDK17
    • 生成的原始pom.xml文件有如下代码,可根据开发情况自行使用或删除

      • 在使用自定义的pom文件中未对该部分代码删除
      • 在使用生成的原始pom文件中对该部分代码进行了删除

      在这里插入图片描述

使用生成的原始pom文件

  • Step1: 将SpringBoot版本更改为3.0.7

    在这里插入图片描述

  • Step2: 根据实际要求自行修改<groupId><artifactId><version>的标签体内容

  • Step3: 设置聚合工程(即父模块)的打包方式为pom:<packaging>pom</packaging>

  • Step4: 利用<modules>标签设置子模块

    • 由于此时还未创建子模块,所以该<modules>标签体内为空,后续创建子模块后,会将子模块添加到该标签体内
  • Step5: 利用<properties>标签自定义属性

    • jdk版本为17
    • SpringBoot对应的SpringCloud、SpringAlibaba版本依次为:2022.0.3、2022.0.0.0-RC2
  • Step6: 利用<dependencies>标签配置公共依赖

  • Step7: 利用<dependencyManagement>标签配置可选依赖

  • Step8: 配置公共插件

  • 初始状态的pom.xml文件如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--1 将SpringBoot版本更改为3.0.7--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.7</version><relativePath/> <!-- lookup parent from repository --></parent><!--2 根据需求自行修改以下三个标签--><groupId>com.itheima</groupId><artifactId>SCDemo</artifactId><version>0.0.1-SNAPSHOT</version><name>SCDemo</name><description>SCDemo</description><!--3 设置聚合工程(即父模块)的打包方式为pom--><packaging>pom</packaging><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><!--4 设置子模块--><modules></modules><!--5 自定义属性--><properties><java.version>17</java.version><!--设置SpringCloud版本为2022.0.3--><spring-cloud.version>2022.0.3</spring-cloud.version><!--设置SpringAlibaba版本为2022.0.0.0-RC2--><spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version></properties><!--6 配置公共依赖--><dependencies><!--lombok依赖--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><!--7 配置可选依赖--><dependencyManagement><dependencies><!-- springCloud依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--springCloud-alibaba依赖--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><!--8 配置公共插件--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
    

使用自定义的pom文件

重新创建一个SpringBoot聚合工程ScDemo,重复部分不再演示,此处只进行pom文件相关配置演示

  • Step1: 删除生成的原始pom.xml文件中定义的父工程,即图示部分内容

    在这里插入图片描述

  • Step2: 根据实际要求自行修改<groupId><artifactId><version>的标签体内容

  • Step3: 设置聚合工程(即父模块)的打包方式为pom:<packaging>pom</packaging>

  • Step4: 利用<modules>标签设置子模块

    • 由于此时还未创建子模块,所以该<modules>标签体内为空,后续创建子模块后,会将子模块添加到该标签体内
  • Step5: 利用<properties>标签自定义属性

    • jdk版本为17
    • SpringBoot、SpringCloud、SpringAlibaba版本依次为:3.0.2、2022.0.3、2022.0.0.0-RC2
  • Step6: 利用<dependencies>标签配置公共依赖

  • Step7: 利用<dependencyManagement>标签配置可选依赖

  • Step8: 配置公共插件

初始状态的pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--1 根据需求自行修改以下三个标签--><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><!--2 设置聚合工程(即父模块)的打包方式为pom--><packaging>pom</packaging><!--3 设置子模块--><modules></modules><!--4 自定义属性--><properties><java.version>17</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-boot.version>3.0.7</spring-boot.version><spring-cloud.version>2022.0.3</spring-cloud.version><spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version></properties><!--5 配置公共依赖--><dependencies><!--lombok依赖--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><!--6 配置可选依赖--><dependencyManagement><dependencies><!-- springboot的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- springcloud依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--spring-cloud-alibaba--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><!--7 配置公共插件--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

子模块创建

以ScDemo工程为例进行子模块创建

子模块order-service创建

  • Step1: 在该聚合工程(即父工程)ScDemo下创建子Maven模块order-service

    在这里插入图片描述

    创建完成后初始pom文件代码如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>order-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>
    
  • Step2: 配置聚合工程(即父工程)ScDemo的pom文件

    • Step2-1: 在pom文件中定义该工程的子工程

      注意:由于在创建子模块时指定了子模块的聚合工程,所以聚合工程的pom文件中会自动设置子模块,此时跳过该步即可;若在创建子模块时未指定子模块的聚合工程,则需要该步

      <!--3 设置子模块-->
      <modules><module>order-service</module>
      </modules>
      
  • Step3: 配置子模块order-service的pom文件

    • Step3-1: 定义该工程模块的聚合工程(即父工程)

      注意:由于在创建子模块时指定了子模块的聚合工程,所以该子模块的pom文件中会自动设置聚合工程,此时跳过该步即可;若在创建子模块时未指定子模块的聚合工程,则需要该步

    • Step3-2: 引入聚合工程(即父工程)的可选依赖

    子模块order-service的pom文件代码如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--定义该工程模块的聚合工程(即父工程)--><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>order-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--配置依赖--><dependencies><!--启动类相关依赖:SpringBoot依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>
    
  • Step4: 在子模块的java包下创建at.guigu包,并在该包下创建SpringBoot的启动类OrderApplication,代码如下:

    package at.guigu;import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;@Slf4j
    @SpringBootApplication
    public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);log.info("OrderApplication Running");}
    }
    
  • 运行启动类OrderApplication测试是否能够正常运行

    在这里插入图片描述

子模块user-service创建

步骤略,可详见子模块order-service创建部分内容

SpringCloud项目实现

聚合工程环境准备

聚合工程环境准备主要是配置pom文件

  • 引入公共依赖

    • mysql坐标
    • 数据源坐标:druid
    • MyBatisPlus坐标:MyBatisPlus
    • lombok坐标:lombok
    • 分页坐标:mybatis-plus-jsqlparser
    • hutool坐标:hutool-all
    • SpringBoot测试用到的坐标:spring-boot-starter-test
    <!--lombok坐标-->
    <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
    </dependency>
    <!--druid坐标-->
    <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.18</version>
    </dependency>
    <!--mysql坐标-->
    <dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope>
    </dependency>
    <!--MyBatisPlus坐标-->
    <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>${mybatis-plus.version}</version>
    </dependency>
    <!--分页坐标mybatis-plus-jsqlparser-->
    <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>${mybatis-plus-jsqlparser.version}</version>
    </dependency>
    <!--hutool坐标-->
    <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool-all.version}</version>
    </dependency>
    <!--springbootTest坐标-->
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
    </dependency>
    
  • 相关公用插件

    • Mockito警告解决插件

      <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.2</version><configuration><argLine>-javaagent:"${settings.localRepository}/org/mockito/mockito-core/5.14.2/mockito-core-5.14.2.jar"</argLine></configuration>
      </plugin>
      
  • 完整pom文件代码如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--1 根据需求自行修改以下三个标签--><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><!--2 设置聚合工程(即父模块)的打包方式为pom--><packaging>pom</packaging><!--3 设置子模块--><modules><module>order-service</module><module>user-service</module></modules><!--4 自定义属性--><properties><java.version>17</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-boot.version>3.0.7</spring-boot.version><spring-cloud.version>2022.0.3</spring-cloud.version><spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version><mybatis-plus.version>3.5.9</mybatis-plus.version><mybatis-plus-jsqlparser.version>3.5.9</mybatis-plus-jsqlparser.version><hutool-all.version>5.8.25</hutool-all.version></properties><!--5 配置公共依赖--><dependencies><!--lombok坐标--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--druid坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.18</version></dependency><!--mysql坐标--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!--MyBatisPlus坐标--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>${mybatis-plus.version}</version></dependency><!--分页坐标mybatis-plus-jsqlparser--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>${mybatis-plus-jsqlparser.version}</version></dependency><!--hutool坐标--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool-all.version}</version></dependency><!--测试类相关依赖:spring-boot-starter-test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><!--6 配置可选依赖--><dependencyManagement><dependencies><!-- springboot的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- springcloud依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--spring-cloud-alibaba--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><!--7 配置公共插件--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.2</version><configuration><argLine>-javaagent:"${settings.localRepository}/org/mockito/mockito-core/5.14.2/mockito-core-5.14.2.jar"</argLine></configuration></plugin></plugins></build></project>
    

子模块order-service功能实现

  • Step1: 导入SQL脚本文件并使IDE与数据库建立连接

    本步骤略,sql脚本文件已存至对应resources目录下的sql目录中

  • Step2: 配置pom文件

    • 配置依赖

      • Junit单元测试相关依赖:spring-boot-starter-test
        • 该依赖已在聚合工程(即父工程)中配置,所以在子模块中就不需要再次进行配置
      • 启动类相关依赖:spring-boot-starter-web
      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--定义该工程模块的聚合工程(即父工程)--><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>order-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--配置依赖--><dependencies><!--启动类相关依赖:spring-boot-starter-web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>
      
  • Step3: 右键源代码配置文件目录(即资源文件resources)→NewFile,创建配置文件application.yml,代码如下:

    server:port: 8080spring:# 配置数据库连接信息以及数据源信息datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/cloud_order?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=trueusername: rootpassword: 123456mybatis-plus:configuration:# 配置MyBatisPlus的运行日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl# 是否开启下划线和驼峰映射, 默认为truemap-underscore-to-camel-case: true# 实体类的别名扫描包---即设置别名type-aliases-package: at.guigu.poglobal-config:db-config:# 全局id类型---即id生成策略# id-type: auto# 更新策略,默认为not_null# update-strategy: not_null# 设置数据库表名前缀table-prefix: tb_logging:level:# 配置指定包及其子包下的所有类的日志级别为debugat.guigu: debug# 配置日志输出的时间戳格式pattern:dateformat: MM-dd HH:mm:ss:SSS
    
  • Step4: 创建po包,并在po包下创建实体类UserOrder,代码如下

    package at.guigu.po;
    import lombok.Data;@Data
    public class User {private Long id;private String username;private String address;
    }
    
    package at.guigu.po;import com.baomidou.mybatisplus.annotation.TableField;
    import lombok.Data;@Data
    public class Order {private Long id;private Long userId;private String name;private Long price;private Integer num;@TableField(exist = false)private User user;
    }
    
  • Step5: 创建持久层mapper包,并在该包下创建继承BaseMapper<T>接口的OrderMapper接口,代码如下

    package at.guigu.mapper;import at.guigu.po.Order;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.apache.ibatis.annotations.Mapper;@Mapper
    public interface OrderMapper extends BaseMapper<Order> {
    }
    
  • Step6: 创建业务层service包

    • 在该包下创建继承IService<T>接口的IOrderService接口,代码如下

      package at.guigu.service;import at.guigu.po.Order;
      import com.baomidou.mybatisplus.extension.service.IService;public interface IOrderService extends IService<Order> {
      }
      
    • 在该包下创建impl包,并在该包下创建继承ServiceImpl<M extends BaseMapper<T>, T>类、实现IOrderService接口的类OrderServiceImpl,同时用@Service注解标注该类

      package at.guigu.service.impl;import at.guigu.mapper.OrderMapper;
      import at.guigu.po.Order;
      import at.guigu.service.IOrderService;
      import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
      import org.springframework.stereotype.Service;@Service
      public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
      }
      
  • Step7: 在测试包下创建业务层对应的测试类OrderServiceImplTest,代码如下

    注意:若要在test测试目录下进行测试,则pom文件中必须有SpringBootTest依赖:spring-boot-starter-test

    package at.guigu.service.impl;import at.guigu.po.Order;
    import at.guigu.service.IOrderService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
    class OrderServiceImplTest {@Autowiredprivate IOrderService orderService;@Testvoid getOrderById() {Order order = orderService.getById(101L);System.out.println(order);}
    }
    

    在这里插入图片描述

  • Step8: 创建表现层controller包,并在该包下创建OrderController类,代码如下

    package at.guigu.controller;import at.guigu.po.Order;
    import at.guigu.service.IOrderService;
    import lombok.RequiredArgsConstructor;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;@RestController
    @RequestMapping("order")
    @RequiredArgsConstructor
    public class OrderController {private final IOrderService orderService;@GetMapping("{orderId}")public Order queryOrderById(@PathVariable("orderId") Long orderId) {// 根据id查询订单并返回return orderService.getById(orderId);}
    }
    
  • Step9: 运行启动类,PostMan测试是否成功

    在这里插入图片描述

子模块user-service功能实现

  • Step1: 导入SQL脚本文件并使IDE与数据库建立连接

    本步骤略,sql脚本文件已存至对应resources目录下的sql目录中

  • Step2: 配置pom文件

    • 配置依赖

      • Junit单元测试相关依赖:spring-boot-starter-test
        • 该依赖已在聚合工程(即父工程)中配置,所以在子模块中就不需要再次进行配置
      • 启动类相关依赖:spring-boot-starter-web
      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>user-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--配置依赖--><dependencies><!--启动类相关依赖:spring-boot-starter-web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
      </project>
      
  • Step3: 右键源代码配置文件目录(即资源文件resources)→NewFile,创建配置文件application.yml,代码如下:

    server:port: 8081spring:# 配置数据库连接信息以及数据源信息datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/cloud_user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=trueusername: rootpassword: 123456mybatis-plus:configuration:# 配置MyBatisPlus的运行日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl# 是否开启下划线和驼峰映射, 默认为truemap-underscore-to-camel-case: true# 实体类的别名扫描包---即设置别名type-aliases-package: at.guigu.poglobal-config:db-config:# 全局id类型---即id生成策略# id-type: auto# 更新策略,默认为not_null# update-strategy: not_null# 设置数据库表名前缀table-prefix: tb_logging:level:# 配置指定包及其子包下的所有类的日志级别为debugat.guigu: debug# 配置日志输出的时间戳格式pattern:dateformat: MM-dd HH:mm:ss:SSS
    
  • Step4: 创建po包,并在po包下创建实体类User,代码如下

    package at.guigu.po;
    import lombok.Data;@Data
    public class User {private Long id;private String username;private String address;
    }
    
  • Step5: 创建持久层mapper包,并在该包下创建继承BaseMapper<T>接口的UserMapper接口,代码如下

    package at.guigu.mapper;import at.guigu.po.User;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.apache.ibatis.annotations.Mapper;@Mapper
    public interface UserMapper extends BaseMapper<User> {
    }
    
  • Step6: 创建业务层service包

    • 在该包下创建继承IService<T>接口的IUserService接口,代码如下

      package at.guigu.service;import at.guigu.po.User;
      import com.baomidou.mybatisplus.extension.service.IService;public interface IUserService extends IService<User> {
      }
      
    • 在该包下创建impl包,并在该包下创建继承ServiceImpl<M extends BaseMapper<T>, T>类、实现IUserService接口的类UserServiceImpl,同时用@Service注解标注该类

      package at.guigu.service.impl;import at.guigu.mapper.UserMapper;
      import at.guigu.po.User;
      import at.guigu.service.IUserService;
      import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
      import org.springframework.stereotype.Service;@Service
      public class implUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
      }
      
  • Step7: 在测试包下创建业务层对应的测试类OrderServiceImplTest,代码如下

    注意:若要在test测试目录下进行测试,则pom文件中必须有SpringBootTest依赖:spring-boot-starter-test

    package at.guigu.service.impl;import at.guigu.po.User;
    import at.guigu.service.IUserService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
    class implUserServiceImplTest {@Autowiredprivate IUserService userService;@Testvoid getOrderById() {User user = userService.getById(1L);System.out.println(user);}
    }
    

    在这里插入图片描述

  • Step8: 创建表现层controller包,并在该包下创建UserController类,代码如下

    package at.guigu.controller;import at.guigu.po.User;
    import at.guigu.service.IUserService;
    import lombok.RequiredArgsConstructor;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;@RestController
    @RequiredArgsConstructor
    @RequestMapping("/user")
    public class UserController {private final IUserService userService;@GetMapping("/{id}")public User queryById(@PathVariable("id") Long id) {return userService.getById(id);}
    }
    
  • Step9: 运行启动类,PostMan测试是否成功

    在这里插入图片描述

最终完整pom文件

聚合工程(即父工程)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--1 根据需求自行修改以下三个标签--><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><!--2 设置聚合工程(即父模块)的打包方式为pom--><packaging>pom</packaging><!--3 设置子模块--><modules><module>order-service</module><module>user-service</module></modules><!--4 自定义属性--><properties><java.version>17</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-boot.version>3.0.7</spring-boot.version><spring-cloud.version>2022.0.3</spring-cloud.version><spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version><mybatis-plus.version>3.5.9</mybatis-plus.version><mybatis-plus-jsqlparser.version>3.5.9</mybatis-plus-jsqlparser.version><hutool-all.version>5.8.25</hutool-all.version></properties><!--5 配置公共依赖--><dependencies><!--lombok坐标--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--druid坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.18</version></dependency><!--mysql坐标--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!--MyBatisPlus坐标--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>${mybatis-plus.version}</version></dependency><!--分页坐标mybatis-plus-jsqlparser--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>${mybatis-plus-jsqlparser.version}</version></dependency><!--hutool坐标--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool-all.version}</version></dependency><!--测试类相关依赖:spring-boot-starter-test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><!--6 配置可选依赖--><dependencyManagement><dependencies><!-- springboot的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- springcloud依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--spring-cloud-alibaba--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><!--7 配置公共插件--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.2</version><configuration><argLine>-javaagent:"${settings.localRepository}/org/mockito/mockito-core/5.14.2/mockito-core-5.14.2.jar"</argLine></configuration></plugin></plugins></build></project>

子模块order-service

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--定义该工程模块的聚合工程(即父工程)--><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>order-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--配置依赖--><dependencies><!--启动类相关依赖:spring-boot-starter-web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

子模块user-service

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>user-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--配置依赖--><dependencies><!--启动类相关依赖:spring-boot-starter-web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>

pom文件优化

在上述pom文件中,我们可以看到,由于两个子模块均配置了启动类相关依赖(即spring-boot-starter-web),所以我们可以将其直接配置到聚合工程(即父工程)的pom文件中来降低耦合,此时pom文件代码分别如下:

  • 聚合工程(即父工程)

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--1 根据需求自行修改以下三个标签--><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><!--2 设置聚合工程(即父模块)的打包方式为pom--><packaging>pom</packaging><!--3 设置子模块--><modules><module>order-service</module><module>user-service</module></modules><!--4 自定义属性--><properties><java.version>17</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-boot.version>3.0.7</spring-boot.version><spring-cloud.version>2022.0.3</spring-cloud.version><spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version><mybatis-plus.version>3.5.9</mybatis-plus.version><mybatis-plus-jsqlparser.version>3.5.9</mybatis-plus-jsqlparser.version><hutool-all.version>5.8.25</hutool-all.version></properties><!--5 配置公共依赖--><dependencies><!--lombok坐标--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--druid坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.18</version></dependency><!--mysql坐标--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!--MyBatisPlus坐标--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>${mybatis-plus.version}</version></dependency><!--分页坐标mybatis-plus-jsqlparser--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>${mybatis-plus-jsqlparser.version}</version></dependency><!--hutool坐标--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool-all.version}</version></dependency><!--启动类相关依赖:spring-boot-starter-web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--测试类相关依赖:spring-boot-starter-test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><!--6 配置可选依赖--><dependencyManagement><dependencies><!-- springboot的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- springcloud依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--spring-cloud-alibaba--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><!--7 配置公共插件--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.2</version><configuration><argLine>-javaagent:"${settings.localRepository}/org/mockito/mockito-core/5.14.2/mockito-core-5.14.2.jar"</argLine></configuration></plugin></plugins></build></project>
    
  • 子模块order-service

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--定义该工程模块的聚合工程(即父工程)--><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>order-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--配置依赖--><dependencies></dependencies></project>
    
  • 子模块user-service

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.itheima</groupId><artifactId>ScDemo</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>user-service</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--配置依赖--><dependencies></dependencies>
    </project>
    

http://www.mrgr.cn/news/83433.html

相关文章:

  • 【C++入门】详解(中)
  • [python3]Uvicorn库
  • vue封装axios请求
  • Git | git stash命令详解
  • 天气app的收获
  • 小米路由器IPv6 功能使用指南
  • CentOS 7 下 MySQL 5.7 的详细安装与配置
  • C语言程序环境和预处理详解
  • 模拟闯红灯的抓拍系统
  • RabbitMQ高级篇
  • LeetCode 热题 100 | 双指针
  • 【数据结构】第1天之Java中的数据结构
  • 【MySQL学习笔记】MySQL视图View
  • C语言中两个不同类型的结构体相互赋值
  • ffmpeg 常用命令 案例
  • word中电流符号i或者j,这两个字母的头上的点会消失---完美解决办法
  • AI人工智能(3):Python 入门机器学习
  • fisco bcos CRUD编写合约的注意点
  • 【应用篇】09.自主Shell命令行解释器
  • mysql中创建计算字段
  • 音视频入门基础:RTP专题(2)——使用FFmpeg命令生成RTP流
  • ollama简单上手
  • 【C语言】_使用冒泡排序模拟实现qsort函数
  • java_将数据存入elasticsearch进行高效搜索
  • Vue Router4
  • Flask----前后端不分离-登录