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<?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>
- Step3-1: 根据实际要求自行修改
父工程创建方式二
-
Step1: 创建一个SpringBoot聚合工程SCDemo
创建完成后删除除pom.xml文件外的所有文件及目录
-
Step2: 设置Mven为本地Maven
博主已进行过修改,具体修改可详见Maven部分内容
-
生成的原始pom.xml文件,完整代码如下:
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版本依次为
SpringBoot SpringCloud SpringAlibaba 最低支持JDK版本 3.0.7 2022.0.3 2022.0.0.0-RC2 JDK17 -
生成的原始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<?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
<?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<?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文件中会自动设置子模块,此时跳过该步即可;若在创建子模块时未指定子模块的聚合工程,则需要该步
xml<!--3 设置子模块--> <modules> <module>order-service</module> </modules>
-
-
Step3: 配置子模块order-service的pom文件
-
Step3-1: 定义该工程模块的聚合工程(即父工程)
注意:由于在创建子模块时指定了子模块的聚合工程,所以该子模块的pom文件中会自动设置聚合工程,此时跳过该步即可;若在创建子模块时未指定子模块的聚合工程,则需要该步
-
Step3-2: 引入聚合工程(即父工程)的可选依赖
子模块order-service的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> <!--定义该工程模块的聚合工程(即父工程)--> <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
,代码如下:javapackage 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
xml<!--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警告解决插件
xml<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<?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> <!--springbootTest--> <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: 右键源代码配置文件目录(即资源文件
resources
)→New
→File
,创建配置文件application.yml,代码如下:yamlserver: port: 8080 spring: # 配置数据库连接信息以及数据源信息 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud_order?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true username: root password: 123456 mybatis-plus: configuration: # 配置MyBatisPlus的运行日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 是否开启下划线和驼峰映射, 默认为true map-underscore-to-camel-case: true # 实体类的别名扫描包---即设置别名 type-aliases-package: at.guigu.po global-config: db-config: # 全局id类型---即id生成策略 # id-type: auto # 更新策略,默认为not_null # update-strategy: not_null # 设置数据库表名前缀 table-prefix: tb_ logging: level: # 配置指定包及其子包下的所有类的日志级别为debug at.guigu: debug # 配置日志输出的时间戳格式 pattern: dateformat: MM-dd HH:mm:ss:SSS
-
Step3: 创建po包,并在po包下创建实体类
User
、Order
,代码如下javapackage at.guigu.po; import lombok.Data; @Data public class User { private Long id; private String username; private String address; }
javapackage 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; }
-
Step4: 创建持久层mapper包,并在该包下创建继承
BaseMapper<T>
接口的OrderMapper
接口,代码如下javapackage 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> { }
-
Step5: 创建业务层service包
-
在该包下创建继承
IService<T>
接口的IOrderService
接口,代码如下javapackage 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
注解标注该类javapackage 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 { }
-
-
Step6: 在测试包下创建业务层对应的测试类
OrderServiceImplTest
,代码如下注意:若要在test测试目录下进行测试,则pom文件中必须有SpringBootTest依赖:
spring-boot-starter-test
javapackage 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 { @Autowired private IOrderService orderService; @Test void getOrderById() { Order order = orderService.getById(101L); System.out.println(order); } }
-
Step7: 创建表现层controller包,并在该包下创建
OrderController
类,代码如下javapackage 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); } }
-
Step8: 运行启动类,PostMan测试是否成功
子模块user-service功能实现
-
Step1: 导入SQL脚本文件并使IDE与数据库建立连接
本步骤略,sql脚本文件已存至对应resources目录下的sql目录中
-
Step2: 右键源代码配置文件目录(即资源文件
resources
)→New
→File
,创建配置文件application.yml,代码如下:yamlserver: port: 8081 spring: # 配置数据库连接信息以及数据源信息 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud_user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true username: root password: 123456 mybatis-plus: configuration: # 配置MyBatisPlus的运行日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 是否开启下划线和驼峰映射, 默认为true map-underscore-to-camel-case: true # 实体类的别名扫描包---即设置别名 type-aliases-package: at.guigu.po global-config: db-config: # 全局id类型---即id生成策略 # id-type: auto # 更新策略,默认为not_null # update-strategy: not_null # 设置数据库表名前缀 table-prefix: tb_ logging: level: # 配置指定包及其子包下的所有类的日志级别为debug at.guigu: debug # 配置日志输出的时间戳格式 pattern: dateformat: MM-dd HH:mm:ss:SSS
-
Step3: 创建po包,并在po包下创建实体类
User
,代码如下javapackage at.guigu.po; import lombok.Data; @Data public class User { private Long id; private String username; private String address; }
-
Step4: 创建持久层mapper包,并在该包下创建继承
BaseMapper<T>
接口的UserMapper
接口,代码如下javapackage 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> { }
-
Step5: 创建业务层service包
-
在该包下创建继承
IService<T>
接口的IUserService
接口,代码如下javapackage 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
注解标注该类javapackage 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 { }
-
-
Step6: 在测试包下创建业务层对应的测试类
OrderServiceImplTest
,代码如下注意:若要在test测试目录下进行测试,则pom文件中必须有SpringBootTest依赖:
spring-boot-starter-test
javapackage 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 { @Autowired private IUserService userService; @Test void getOrderById() { User user = userService.getById(1L); System.out.println(user); } }
-
Step7: 创建表现层controller包,并在该包下创建
UserController
类,代码如下javapackage 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); } }
-
Step8: 运行启动类,PostMan测试是否成功