第一步:创建一个简单的mavne项目
创建一个简单的maven项如上图所示,这是一个最简单的maven项目
第二步:在pom.xml中添加springboot所需要的依赖
<!--引入一个父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency> <!-- 这是启动web,使用tomcat作为默认的容器-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <!-- 这是测试单元-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!--thymeleaf的依赖-->
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-thymeleaf </artifactId>
</dependency>
</dependencies>
<build> <!-- 打jar包的插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在pom.xml中引入依赖之后,项目的右边会出现sprigboot所需要的jar包
第三步:创建启动类
启动类的代码如下所示:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello") @ResponseBody
public String hello(){
return "Hello Springboot";
}
}
第四步:创建控制层,controller
HelloController的代码如下所示:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello") @ResponseBody
public String hello(){
return "Hello Springboot";
}
}
第五步:在Application中运行程序,在网页中输入localhost:8080/hello,页面如图所示,第一个springboot的程序创建成功