SpringCloud环境搭建 --- Rest使用
作者:互联网
Rest环境搭建
环境说明:jdk1.8,idea2019,maven3.6.X,springcloud最新版2021.0.3,mybatis-plus
我们首先搭建一个父工程项目,创建一个空的普通的maven项目,我们直接把src目录删除掉,pom文件写下面的使用父工程管理依赖,这里都是与文章日期最新依赖。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.18.24</lombok.version>
<springcloud-version>2021.0.3</springcloud-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!--springcloud依赖-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--日志测试-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!--父工程的项目名称-->
<finalName>springcloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<!--以$开头结尾 符合在src/main/resources路径下面访问到 主要是yml文件或者properties文件-->
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
有了父工程项目,我们再准备一个数据库环境,写一个api类,这里我们新建一个简单的账户表,往里面插入几条简单的数据
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`account_id` int(11) NOT NULL,
`account_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`account_pwd` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
PRIMARY KEY (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES ('1', 'zhangsan', '333');
INSERT INTO `account` VALUES ('2', 'lisi', '444');
INSERT INTO `account` VALUES ('3', 'wangwu', '555');
INSERT INTO `account` VALUES ('4', 'zhangliu', '666');
INSERT INTO `account` VALUES ('5', 'qiqi', '777');
INSERT INTO `account` VALUES ('6', 'baba', '888');
新建一个maven普通项目起名叫springcloud-api,里面呢,放一个这样的实体类,与表的关系对应起来的一个简单对象,使用了lombok,这里大家可以自行去了解
1、导入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
我们可以看到这里如果正确会有一个小圆圈,如果你这里没有,那就要检查下父工程依赖了
2、编写配置,这里没有什么配置,所以就是滤过
3、编写对应代码
provider-服务端提供者
再新建一个maven普通项目模块,我们做服务的提供者
标签:SpringCloud,环境搭建,rest 来源: