1. 程式人生 > >springBoot極速入門

springBoot極速入門

1、去這裡下載https://start.spring.io/
a.選擇Switch to the full version 進行各種配置
b.Packaging 為了以後方便最好還是選擇 war
c.點選 Generate Project 下載
2、解壓並用eclipse開啟
我的長這個樣子
在這裡插入圖片描述
a.找到自動生成的一個類xxxxApplication 譬如DemoApplication 啟動程式直接以這個為主類像執行普通JAVA程式似的執行就可以
具體操作:右鍵類檔案->Run As->Java Application
b.此時控制檯輸出一堆,不用管他
最後出現這個就證明啟動成功了
2018-10-18 09:37:31.172 INFO 744 — [ main] com.mnn.DemoApplication : Started DemoApplication in 2.687 seconds (JVM running for 3.202)


c.瀏覽器訪問http://127.0.0.1:8080/
出現這個就證明啟動成功了
在這裡插入圖片描述
3、 編寫第一個controller
PS:需要匯入包

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

a. 新建一個類
譬如這樣

package com.mnn;

public class HelloController {

}

然後添加註解把它標識為一個Controller

package com.mnn;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	@RequestMapping("/hello")
	public String h()
	{
		return "helloWorld";
	}

}

然後重新執行程式訪問

http://127.0.0.1:8080/hello就可以看到
在這裡插入圖片描述
springBoot啟動成功,剩下的自行摸索

PS:springBoot實現熱載入即開發環境

 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

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