二、SpringBoot入门
2.1 系统工具列表
- JDK 1.8 或 更高版本;
- maven3.x:maven 3.3以上版本;
- IntelliJ IDEA、STS;
- Spring Boot 正式版为 2.6.9;
2.2 maven的配置
设置maven编译器为1.8版本,给maven 的settings.xml配置文件的profiles标签添加:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
2.3 项目实现功能
浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串。
主要通过两种形式来创建spring boot项目:
- 创建maven项目,添加相应的pom依赖。
- 使用 spring boot 提供的 Initializer 来创建项目。
2.3.1 创建maven工程
2.3.2 引入依赖
<?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>
<!--引入spirngboot的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.9</version>
</parent>
<groupId>com.newcapec</groupId>
<artifactId>springboot01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<!--当前项目到打包方式为jar-->
<packaging>jar</packaging>
<!--引入核心依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.3.3 编写主程序
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//通过SpringApplication启动程序
SpringApplication.run(MainApplication.class, args);
}
}
2.3.4 编写Controller
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String hello(){
System.out.println("hello方法执行了...");
return "hello springboot我来了...";
}
}
2.3.5 运行测试
回到MainApplicaiton 类中,运行main()方法,启动springboot应用程序:
在浏览器中输入localhost:8080来测试:
在浏览器中输入localhost:8080/hello来测试:
2.3.6 简化部署
Springboot打包插件,可以将应用打包成一个可执行的jar包。
<!--Springboot打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
调用idea 的maven插件,执行package
来执行打包命令
进入 cmd
命令行窗口,定位到打包后打jar文件所在目录。
将这个应用打成jar包,直接使用 java -jar springboot01-helloworld-1.0-SNAPSHOT.jar
的命令进行执行。
程序执行成功后,可再次通过浏览器来验证hello服务是否正确返回。
2.4 Spring Initializr
构建一个Sping Boot的Maven项目,强烈推荐Spring Initializr(https://start.spring.io),它从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构。
通过IntelliJ IDEA使用,创建Spring Boot操作步骤如下:
- 在File菜单里面选择 New > Project;
- 然后选择Spring Initializr;
- 填写项目信息;
- 选项Springboot版本和依赖。
默认生成的Spring Boot项目:
- 主程序已经生成好了,只需要添加自己所需的业务逻辑控制代码(controller,service,dao等)。
- src,源码目录。
- resources文件夹中目录结构。
- static:存放所有的静态资源(js,css,images);
- templates:保存所有的模板页面。Spring Boot默认jar包使用嵌入式的Tomcat,不支持JSP页面,可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot应用的配置文件。
- .gitignore–分布式版本控制系统git的配置文件,意为忽略提交。
- mvnw–全名是maven wrapper,它的原理是在maven-wrapper.properties文件中记录你要使用的maven版本,当用户执行mvnw clean 命令时,发现当前用户的maven版本和期望的版本不一致,那么就下载期望的版本,然后用期望的版本来执行mvn命令,比如刚才的mvn clean。
- .mvn文件夹–存放maven-wrapper.properties和相关jar包。
- mvn.cmd–执行mvnw命令的cmd入口。
- .Idea存放项目的配置信息,包括历史记录,版本控制信息等。其实可以不用理它,也可以在IDEA里面设置隐藏它。