從零開始學(xué)SpringBoot如何開始使用圖文詳解
目的:
《從零開始學(xué)SpringBoot》,是小編打算通過(guò)寫一系列的文章,讓大家能夠認(rèn)識(shí)SpringBoot,通過(guò)對(duì)SpringBoot的入門學(xué)習(xí)后,小編會(huì)在通過(guò)一個(gè)示例Demo來(lái)讓大家能夠真正上手SpringBoot。
適合人群:
1、有一定Java基礎(chǔ)的朋友
2、適合初中級(jí)的朋友。
如果文章編寫中存在問(wèn)題或者對(duì)文章有疑問(wèn),都可以留言小編,和小編一起探討,小編會(huì)虛心接受大家的建議并更正。
1.什么是Spring Boot
來(lái)源官方文檔:
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.
上面的意思是說(shuō):
Spring Boot可以很簡(jiǎn)單的就創(chuàng)建一個(gè)你可以運(yùn)行的獨(dú)立的、生產(chǎn)級(jí)別的應(yīng)用系統(tǒng)。
我們可以使用Spring平臺(tái)和第三方庫(kù)快速的開始,很多的Spring Boot應(yīng)用需要很少的配置。
2.如何搭建一個(gè)Spring Boot的環(huán)境
對(duì)Spring Boot的定義,大家可能知道,但是還是有點(diǎn)暈頭,現(xiàn)在我們直接上手,來(lái)搭建一個(gè)Spring Boot的項(xiàng)目,然后一步步的來(lái)講解和實(shí)現(xiàn),讓大家更深一步來(lái)理解如何開始使用Spring Boot。
大家可以打開https://start.spring.io/,默認(rèn)的選項(xiàng),點(diǎn)擊“Generate Project”按鈕生成一個(gè)Maven的項(xiàng)目。
將生成的Maven項(xiàng)目進(jìn)行解壓,導(dǎo)入到Eclipse中。
導(dǎo)入的步驟:
Import -> Existing Maven Projects -> Next -> 選擇解壓的Demo -> Finish
導(dǎo)入后如下圖:
3.理解官網(wǎng)下載后的文件目錄
如第2步中的上圖所示:
src/main/java
--DemoApplication.java 主程序的入口,從這里執(zhí)行運(yùn)行,類似Java的Main函數(shù)入口
src/main/resources
-- application.properties 配置文件,可以設(shè)置一些參數(shù)變量值,例如MyBatis數(shù)據(jù)庫(kù)連接參數(shù)等等
src/test/java 測(cè)試程序,可以在這里寫測(cè)試用例
4.實(shí)現(xiàn)Hello World
學(xué)習(xí)任何一門語(yǔ)句或者框架,第一個(gè)打印都是Hello World,因此,我們使用SpringBoot官網(wǎng)下載的Demo實(shí)現(xiàn)一下,如何顯示Hello World
在pom.xml中添加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
個(gè)人建立一個(gè)com.example.demo.controller,實(shí)現(xiàn)一個(gè)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.通過(guò)Maven從無(wú)到有搭建Spring Boot環(huán)境 有了上面的步驟,對(duì)SpringBoot有了一個(gè)基本的認(rèn)識(shí),現(xiàn)在我們通過(guò)Maven,從無(wú)到有的搭建一個(gè)和Demo一致的SpringBoot環(huán)境,也實(shí)現(xiàn)Hello World功能。如何創(chuàng)建一個(gè)Maven項(xiàng)目,小編在這里不做多的描述和截圖,大家如果有問(wèn)題可以留言給小編,我們一起來(lái)探討學(xué)習(xí),現(xiàn)在直接在一個(gè)Maven的項(xiàng)目基礎(chǔ)上,實(shí)現(xiàn)SpringBoot的基本框架。
如圖為創(chuàng)建的一個(gè)Maven項(xiàng)目:
現(xiàn)在編寫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的目錄下,創(chuàng)建com.chyanwu.demo包,創(chuàng)建一個(gè)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); } }
直接可以運(yùn)行,現(xiàn)在實(shí)現(xiàn)Hello World
創(chuàng)建com.chyanwu.demo.controller,在創(chuàng)建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; } }
運(yùn)行后,輸入http://localhost:8080/hello?name=World
總結(jié)
到此這篇關(guān)于從零開始學(xué)SpringBoot如何開始使用圖文詳解的文章就介紹到這了,更多相關(guān)SpringBoot如何開始使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Redisson實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
- Springboot中@Value的使用詳解
- 解析SpringBoot @EnableAutoConfiguration的使用
- Springboot RestTemplate 簡(jiǎn)單使用解析
- springboot 使用上下文獲取bean
- 使用springboot結(jié)合vue實(shí)現(xiàn)sso單點(diǎn)登錄
- 使用SpringBoot-JPA進(jìn)行自定義保存及批量保存功能
- springboot使用filter獲取自定義請(qǐng)求頭的實(shí)現(xiàn)代碼
相關(guān)文章
java Aop實(shí)現(xiàn)自動(dòng)填充字段值示例
這篇文章主要為大家介紹了Aop實(shí)現(xiàn)自動(dòng)填充字段值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08maven模塊化開發(fā)部署實(shí)現(xiàn)方案
有些用戶有定制化需求,需要添加新的模塊功能,因此需要平臺(tái)主體功能迭代的同時(shí),非主體功能和定制化功能插件化,本文給大家介紹maven模塊化開發(fā)部署實(shí)現(xiàn)方案,感興趣的朋友一起看看吧2024-01-01Java基礎(chǔ)知識(shí)之Java語(yǔ)言概述
這篇文章主要介紹了Java基礎(chǔ)知識(shí)之Java語(yǔ)言概述,本文介紹了Java語(yǔ)言相關(guān)的基礎(chǔ)知識(shí)、歷史介紹、主要應(yīng)用方向等內(nèi)容,需要的朋友可以參考下2015-03-03spring.mvc.servlet.load-on-startup屬性方法源碼解讀
這篇文章主要介紹了spring.mvc.servlet.load-on-startup的屬性方法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Java實(shí)現(xiàn)JS中的escape和UNescape代碼分享
在PHP和Python中都有類似JS中的escape和UNescape函數(shù)的功能,那么Java語(yǔ)言中到底有沒(méi)有類似的方法呢?本文就來(lái)介紹一下Java實(shí)現(xiàn)JS中的escape和UNescape轉(zhuǎn)碼方法,需要的朋友可以參考下2017-09-09SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例
這篇文章主要介紹了SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng)
這篇文章主要介紹了基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06