【Spring boot笔记】01 入门搭建

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文件:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>
spring-boot-starter-parent是特殊的,提供了maven的默认配置
<!--springboot依赖-->
<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>
<!--springboot插件-->
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

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

添加依赖

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.suffix=.html

(未完待续)

发布了23 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41022866/article/details/100944891
今日推荐