1.學習主要還是看官網(第一手學習資料):
SpringBoot快速入門文檔(英文)
SpringBoot快速入門文檔(中文)
springBoot的描述:
Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.
由它的描述可以得出springboot不完全算一個微服務,只能算是一個快速構建,啓動項目的一個框架,Spring Boot旨在通過最少的Spring前期配置使您儘快啓動並運行。
1.1什麼是微服務:http://www.bdata-cap.com/newsinfo/1713874.html(詳細介紹)
1.2環境準備:jdk1.8
maven下載(conf->setting.xml中配置鏡像)
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
2.入門程序
2.1創建一個springboot項目:File->New->Project
勾選SpringWeb代表它可以通過Tomcat容器啓動,然後我選擇了一個SpringBoot的版本2.2.2,next然後finish後即可。
2.2創建後的項目結構
2.3新建一個controller包,包裏新建一個helloController類
package com.study.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/show")
public String show(){
return "hello,springboot";
}
}
啓動:SpringbootStudyApplication
思考:SpringbootStudyApplication啓動器可以隨便放位置嗎?
嘗試:在我們的controller包裏新建一個test包,然後把啓動類放裏面,然後測試:
如何解決這個問題呢?
在啓動類下面添加一個註解:
2.4啓動時springboot圖標的修改:
在Resources下面創建一個banner.txt文件
banner生成地址,輸入你想生成的,然後生成之後粘貼到banner.txt文件中,重新啓動項目。
自定義的banner就生成了。
3.配置文件(application.properties、application.ymal)
關於:
一般上來說,當我們創建一個SpringBoot項目時,IDE會默認幫我們創建一個application.properties配置文件。有些朋友習慣把.properties文件改成.yml文件。那麼這兩種文件類型有什麼區別呢?
區別:
1.內容格式比較:
.properties文件,通過.來連接,通過=來賦值,結構上,沒有分層的感覺,但比較直接。
.yml文件,通過:來分層,結構上,有比較明顯的層次感,最後key賦值的:後需要留一個空格
2.執行順序
如果工程中同時存在application.properties文件和 application.yml文件,yml文件會先加載,而後加載的properties文件會覆蓋yml文件。所以建議工程中,只使用其中一種類型的文件即可。
案例:
application.properties:
server.port=8081
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.url=jdbc:mysql://aliyuncs.com:3306/database?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
application.yml:(注意每個之間有空格)
server:
port: 8082
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/database
username: root
password: ******
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver