使用Idea创建Spring Boot项目

前言

Spring Boot 是 Spring Framework 的一个子项目,它旨在简化 Spring 应用程序的开发过程。通过提供一组开箱即用的功能和配置,Spring Boot 使得创建、部署和运行 Spring 应用变得更加简便和高效。

使用Idea创建Spring Boot项目也非常的简单。

创建项目

创建项目,新版本的Idea在创建项目的时候JDK最低是17。

image-20240802152049340

添加我们的依赖

如果单纯写接口选择Spring Web就行,我这里要调用其它接口所以又添加了OpenFeign

image-20240802152309239

如果想降低JDK或SpringBoot版本,请查看

https://www.psvmc.cn/article/2024-01-15-idea-jdk-version.html

添加Controller

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/user_info")
public Map<String, Object> userInfo() {
Map<String, Object> map = new HashMap<>();
map.put("code", 0);
map.put("msg", "成功");
return map;
}
}

运行我们就能访问我们的接口了

http://localhost:8080/user/user_info

设置Maven镜像地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<repositories>
<repository>
<id>maven-ali</id>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>

降低SpringBoot版本

Spring Boot最新的版本在整合Mybatis时报错

降低版本就可以了

https://www.psvmc.cn/article/2024-01-15-idea-jdk-version.html

连接数据库

https://www.psvmc.cn/article/2024-01-15-spring-boot-mysql.html