1、为什么使用
使用Spring Boot开发web应用程序非常方便,只需要进行简单的配置,可以把更多的精力放在业务逻辑上。
2、什么是Spring Boot
其设计目的是用来简化 Spring 应用的初始搭建以及开发过程,能够极大的简化基于 Spring MVC 的 Web 应用和 REST 服务开发。
所以它是简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。
3、三种运行方式
通过idea直接启动,右键—run
通过maven直接启动,进入到项目的目录下,执行 mvn spring-boot:run
进入到项目目录下,执行 maven install 编辑jar包,执行 java -jar XX.jar 运行程序。
4、springboot项目入门
项目workspace/boottest
1.新建maven web项目
2.添加springboot配置
修改pom.xml文件:
添加配置依赖的时候最好一个一个加,一个一个更新,网速不好的情况下,一起添加依赖,会很慢。
记得在maven中添加镜像地址:
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<!--spring-boot-starter-parent是特殊的,提供了maven的默认配置-->
<!--springboot依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--springboot插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.编写java
@RestController
public class HelloController {
@RequestMapping("/")
String home() {
return "Hello";
}
}
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
4.启动
访问localhost:8080
补充:配置文件
Spring Boot项目中对于配置项的支持非常友好,支持两种类型的配置文件,分别是 properties 类型和 yml 类型。其中 yml 的配置文件描述的更加清晰。
配置文件中可以添加Tomcat端口,thymeleaf模板,数据库配置,邮箱配置等。
整合视图层技术thymeleaf
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在resource文件夹下新建application.properties文件,添加Thymeleaf配置参数
spring.thymeleaf.check-template-location=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.content-type=text/html(版本低用)
spring.thymeleaf.servlet.content-type=text/html(2.0.4版本添加这个)
spring.thymeleaf.suffix=.html
html文件:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span>后端传递result:</span>[[${result}]]
</body>
</html>
(未完待续)