從零開始學SpringBoot如何開始使用圖文詳解
目的:
《從零開始學SpringBoot》,是小編打算通過寫一系列的文章,讓大家能夠認識SpringBoot,通過對SpringBoot的入門學習後,小編會在通過一個示例Demo來讓大家能夠真正上手SpringBoot。
適合人群:
1、有一定Java基礎的朋友
2、適合初中級的朋友。
如果文章編寫中存在問題或者對文章有疑問,都可以留言小編,和小編一起探討,小編會虛心接受大家的建議並更正。
1.什麼是Spring Boot
來源官方文件:
Spring Boot makes it easy to create stand-alone,production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
上面的意思是說:
Spring Boot可以很簡單的就建立一個你可以執行的獨立的、生產級別的應用系統。
我們可以使用Spring平臺和第三方庫快速的開始,很多的Spring Boot應用需要很少的配置。
2.如何搭建一個Spring Boot的環境
對Spring Boot的定義,大家可能知道,但是還是有點暈頭,現在我們直接上手,來搭建一個Spring Boot的專案,然後一步步的來講解和實現,讓大家更深一步來理解如何開始使用Spring Boot。
大家可以開啟https://start.spring.io/,預設的選項,點選“Generate Project”按鈕生成一個Maven的專案。
將生成的Maven專案進行解壓,匯入到Eclipse中。
匯入的步驟:
Import -> Existing Maven Projects -> Next -> 選擇解壓的Demo -> Finish
匯入後如下圖:
3.理解官網下載後的檔案目錄
如第2步中的上圖所示:
src/main/java
--DemoApplication.java 主程式的入口,從這裡執行執行,類似Java的Main函式入口
src/main/resources
--application.properties 配置檔案,可以設定一些引數變數值,例如MyBatis資料庫連線引數等等
src/test/java 測試程式,可以在這裡寫測試用例
4.實現Hello World
學習任何一門語句或者框架,第一個列印都是Hello World,因此,我們使用SpringBoot官網下載的Demo實現一下,如何顯示Hello World
在pom.xml中新增:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
個人建立一個com.example.demo.controller,實現一個HelloWorld類:
package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorld { @RequestMapping("/hello") public String Hello(String name) { return "Hello " + name; } }
在瀏覽器中輸入:http://localhost:8080/hello?name=World
5.通過Maven從無到有搭建Spring Boot環境 有了上面的步驟,對SpringBoot有了一個基本的認識,現在我們通過Maven,從無到有的搭建一個和Demo一致的SpringBoot環境,也實現Hello World功能。如何建立一個Maven專案,小編在這裡不做多的描述和截圖,大家如果有問題可以留言給小編,我們一起來探討學習,現在直接在一個Maven的專案基礎上,實現SpringBoot的基本框架。
如圖為建立的一個Maven專案:
現在編寫pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cyw</groupId> <artifactId>maven_sboot</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>maven_sboot Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在src/main/java的目錄下,建立com.chyanwu.demo包,建立一個Application.class的類
package com.chyanwu.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
直接可以執行,現在實現Hello World
建立com.chyanwu.demo.controller,在建立HelloWorld類
package com.chyanwu.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorld { @RequestMapping("/hello") public String Hello(String name) { return "Hello " + name; } }
執行後,輸入http://localhost:8080/hello?name=World
總結
到此這篇關於從零開始學SpringBoot如何開始使用圖文詳解的文章就介紹到這了,更多相關SpringBoot如何開始使用內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!