您的位置:首页 > 编程语言 > Java开发

spring学习笔记(四)springboot 入门

2019-07-31 07:32 330 查看

1.8 使用向导快速创建SpringBoot项目

IDE都支持使用Spring的项目创建向导快速创建按建springBoot应用(联网)

1.步骤:

1.1 打开New Project 里有个Spring Initializr

1.2 选择jdk的版本(1.8)后,点击下一步,并填写项目信息,如下

1.3 选择模块启动器--只选spring web starter模块启动器

 1.4 点击next,再次点击finish完成快速创建,联网从springboot官网生成需要的启动项

1.5 查看生成的pom文件

[code]    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu</groupId>
<artifactId>springboot-01-helloworld-quick</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-01-helloworld-quick</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- springboot 单元测试功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

1.6 主程序

[code]package com.atguigu.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot01HelloworldQuickApplication {

public static void main(String[] args) {
SpringApplication.run(Springboot01HelloworldQuickApplication.class, args);
}

}

1.7 controller

[code]package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//这个类的所有方法返回的数据直接写给浏览器,如果是对象转为json数据
//@ResponseBody
//@Controller
//restController是@ResponseBody和@Controller的合体
@RestController
public class HelloController {

@RequestMapping("/hello")
public String hello() {
return "Hello World";
}

//RESTAPI方式

}

1.8 启动并访问hello请求

2.简单介绍默认生成的springboot项目:

2.1 主程序已经生成了,我们只需要编写我们自己的逻辑

2.2 resources文件夹中的目录结构

      statics:保存所有的静态资源;js,css,images等

      templates:保存所有的模板页面:SpringBoot默认jar包使用嵌入式的tomcat,默认不支持jsp页面,可以使用模板引擎                                    (freemarker,thymeleaf);

      application.properties:默认的springBoot的配置文件,所有默认的配置文件都可以在这个配置文件中修改(如tomcat端口                                                  号  server.port = 8081)

2.SpringBoot配置

2.1 配置文件application

1.SpringBoot使用一个全局的配置文件,配置文件名固定(resources下的二选一)

       application.properties文件

       application.yml文件

2.配置文件的作用:修改springBoot自动配置的默认值,springBoot在底层都给我们配置好了

3.YAML(YAML Ain't Markup Language):是一个标记语言??

4.标记语言:

    以前的配置文件:大多都是使用xxx.xml的形式

    YAML:以数据为中心,比json,xml更适合做配置文件

    示例:--yaml

[code]server:
port: 8081

   xml:

[code]<server>
<port>8081</port>
</server>

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: