创建Provider模块
1. 新建模块
在项目名上右键,依次点击 新建 > 新模块
在左侧选择Spring Initializr
在右侧输入模块名称provider
,语言选择Java
,构建系统选Maven
,打包选择Jar
,然后点击“下一步”
然后按照下图所示,从左侧选择依赖:Spring Web
、JDBC API
、MyBatis Framework
、MySQL Driver
、Eureka Discovery Client
新建完provider
模块后,在pom.xml
上右键,选择添加为 Maven 项目
修改pom.xml
,添加对common
模块的依赖,修改完之后重载Maven项目。完整配置如下文所示
<?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.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider</name>
<description>provider</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<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>
2. 修改配置
打开application.properties
,按照下图修改
3. 修改代码
(1) 新建Mapper
在com.example.provider
包下,新建mapper
包。并在com.example.provider.mapper
包下,新建StudentMapper
接口。代码如下图所示
(2) 新建Mapper.xml
在resources
目录下,新建mapper
目录。并在mapper
目录下,新建StudentMapper.xml
文件。代码如下图所示
(3) 新建Service
在com.example.provider
包下,新建service
包。并在com.example.provider.service
包下,新建StudentService
类。代码如下图所示
(4) 新建Controller
在com.example.provider
包下,新建controller
包。并在com.example.provider.controller
包下,新建StudentController
类。代码如下图所示
4. 构建模块
先点击provider
模块下的maven clean
,完成后再点击maven compile
,构建模块