學習B站尚硅谷SpringBoot頂尖教程(springboot之idea版spring boot)有感
目錄
- 0 什么是SpringBoot
- 1 jdk 與jre安裝
- 2 maven安裝
- 3 Idea安裝
- 4 SpringBoot安裝
- 5 修改Maven的setting配置
- 6 修改Idea中的Maven配置
- 7 開始SpringBoot操作
- 7.1 創建一個maven工程
- 7.2 匯入SpringBoot相關依賴
- 7.3 撰寫Main主程式
- 7.3.1 新建Main類
- 7.3.2 加入注解
- 7.3.3 寫Main class內容
- 7.4 撰寫Controller和Service
- 7.4.1 新建Controller類
- 7.4.2 加入注解和import
- 7.4.3 寫controller代碼
- 7.5 效果測驗
- 7.6 將專案打jar包
- 7.6.1 加入用于打包的插件
- 7.6.2 開始打包
- 7.6.3 運行jar包
- 8 附錄:代碼匯總
0 什么是SpringBoot
SpringBoot通過整合Spring技術堆疊,簡化了Spring開發,
SpingBoot特點——微服務:
- 是一種架構風格
- 服務應該是一組小型服務的組合(功能元素動態組合,比如在一個服務器多放點A,少放點B服務)
- 每個服務都可替換可升級
- 每個服務可以通過http方式溝通
微服務與單體應用 (all in one)相對,單體應用具有以下優點:
- 開發測驗簡單
- 部署簡單(整個打包成war包即可)
- 拓展簡單(提高并發只要相同應用復制到多個服務器即可)
但缺點在于每次修改都要重新部署,
接下來了解如何從0開始用SpringBoot框架搭建一個HelloWorld程式
目標功能:
瀏覽器發送hello請求,服務器接收請求并處理,給瀏覽器回應一個hello world字串
1 jdk 與jre安裝
JDK1.8下載、安裝和環境配置教程
該文章同時內含jre安裝
2 maven安裝
Window系統下的Maven3.3.9安裝
3 Idea安裝
軟體安裝管家-idea2019安裝教程
4 SpringBoot安裝
SpringBoot不需要手動下載,只需要后續在檔案中配置即可自動下載
5 修改Maven的setting配置
打開maven的安裝目錄下conf\setting.xml,如D:\Maven\apache-maven-3.3.9\conf\setting.xml檔案,在<profiles>標簽內增加下面這段并保存,
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
6 修改Idea中的Maven配置
打開Idea的首頁-右下角的Configure-左邊找到Maven項
- 將Maven home directionory設定為Maven安裝目錄
- 將User setting file設定為第5步修改過的
setting.xml檔案,勾選Override - 將Local repository設定為
maven安裝路徑\repository(本來maven安裝路徑里是沒有repository檔案夾的,是自己寫的路徑,表示到時候倉庫就放這)

7 開始SpringBoot操作
7.1 創建一個maven工程
-
打開idea,new project,選擇左側maven
-
修改上方的project sdk為自己安裝jdk的目錄,next

-
設定專案名稱和存盤地址,finish

-
右下角 啟動自動匯入,這樣在pom檔案里面每寫一個依賴就會自動加入相關依賴

7.2 匯入SpringBoot相關依賴
去Spring官網,SpringBoot的Quick Start,復制依賴(即下面這段),粘貼到一開始就打開的pom檔案里面(<project>里面,<version>下面即可),由于是自動匯入,所以會開始下載
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
當外部庫出現新的maven庫就成功了,
但是我用的時候這段沒有自動開始下載,改用下面的才成功了,其實理論上匯入spring-boot-starter-web應該就行了,不清楚原因,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
最終效果如下:
7.3 撰寫Main主程式
用于啟動springboot應用
7.3.1 新建Main類
在左側Src-Main-java檔案夾,右鍵-新建Java class-起名HelloWorldMainApplication或者com.xxx.HelloWorldMainApplication(這樣更好,是放到了包下面,這樣起名idea會自動創建對應的包),最終效果如下:

7.3.2 加入注解
在package(如果上一步寫的是com.xxx的話)和pulic class之間寫上import和@SpringBootApplication用于說明是springboot應用,
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldMainApplication{
}
7.3.3 寫Main class內容
在class里寫
@SpringBootApplication
public class HelloWorldMainApplication{
public static void main(String[]args){
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
這里遇到 cannot resolve method 'run'的錯誤,經查詢可能是maven包安裝問題,嘗試以下2個方法后成功,
-
清空快取
-
在cmd下進入專案的根目錄下,執行以下代碼,用于清理所有的依賴并重新安裝
mvn dependency:purge-local-repository
7.4 撰寫Controller和Service
7.4.1 新建Controller類
在com.xxx的包下新建Java類,命名為controller.HelloController,則會創建一個controller包,下面有HelloController類
7.4.2 加入注解和import
同Main類一樣,在Controller類里面加入以下代碼:
import org.springframework.stereotype.Controller;
@Controller
但是由于剛剛更換了maven包,導致dependency沒有及時更新,所以出現了import時springframework后面沒有stereotype包的問題,
解決:將pom檔案重新匯入reimport即可,

7.4.3 寫controller代碼
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
@RequestMapping("/hello")意為接收瀏覽器的hello請求
return "Hello World"意為Controller向瀏覽器回傳一個"Hello World"字串
7.5 效果測驗
測驗方法:
- 運行主程式的main方法
即點main函式(而非class)旁邊的綠色按鈕-選擇第一個選項
控制臺會列印資訊,如下:

說明tomcat在8080埠已經啟動
-
打開瀏覽器,輸入
localhost:8080,默認出現白色界面如下,不用管

-
瀏覽器輸入改成
localhost:8080/hello表示瀏覽器發出hello請求 -
瀏覽器出現“Hello World”字樣,則成功

-
點擊這里可停止該應用

7.6 將專案打jar包
7.6.1 加入用于打包的插件
在pom中增加以下代碼(用于增加插件):
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
這里遇到spring-boot-maven-plugin出現 not found 的錯誤
解決方法:
- 據說該包不是maven內部的,所以找不到,因此嘗試在pom和maven\setting里面增加配置,仍失敗
- 參考此方法寫plugin時加上
<version>標簽再reimport一下pom(注意,每次修改完要把pom reimport一下 ,不然不會修改),成功,<version>2.2.2.RELEASE</version>
7.6.2 開始打包
雙擊運行package方法

從控制臺可以看到,jar包的位置

從檔案夾也可看到

7.6.3 運行jar包
現在可以直接在命令列用jar命令啟動
- cd到jar的目錄
- 用Java -jar命令運行

瀏覽器顯示如下,運行成功

8 附錄:代碼匯總
似乎增加@會自動import,而且中途pom檔案增加了好幾個依賴,因此將完整代碼附到下面,

HelloWorldMainApplication.java:
package com.atguigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.*;
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
HelloController.java:
package com.atguigu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-boot-01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<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>
<version>2.2.2.RELEASE</version>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>alimaven spring plugin</id>
<name>alimaven spring plugin</name>
<url>https://maven.aliyun.com/repository/spring-plugin</url>
</pluginRepository>
</pluginRepositories>
</project>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/160242.html
標籤:其他
下一篇:Eclipse 教程
