當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
SpringBoot创建项目入门案例
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                SpringBoot创建项目入门案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                目錄結構
一、創建SpringBoot項目
1.創建骨架名稱
2.給項目命名
3.配置pom.xml文件
 
4.MySql的驅動包
5.自動生成的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.william</groupId><artifactId>keepmoving</artifactId><version>0.0.1-SNAPSHOT</version><name>keepmoving</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>二、寫入demo
package com.william.keepmoving.controller;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author :lijunxuan* @date :Created in 2020/5/27 22:42* @description :* @version: 1.0*/ @RestController public class HoldOnLife {@RequestMapping("/hello")public String hello(){return "hello";}}三、啟動項目
發起請求
 http://localhost:8080/hello
響應的頁面
 
四、Spring的自動配置
五、yml文件的使用
特殊的單詞出現的問題
 home 會輸出本地的home
 country 會輸出國家的英文簡稱
yml的兩種注入方式
1.實體類注入
創建實體類
1.加入注解
 @Component
 @ConfigurationProperties(prefix = “user”)
 2.加入以上兩個注解時需要在pom.xml文件中加入配置處理器依賴
 不加配置處理器依賴會提示
 
3.user要和yml文件中的user相同,user實體類要加入對應的get(),set()方法
 4.點擊實體類中的圖標會跳轉到yml文件對應的字段
 
在yml文件中加入特定值
 
 測試類需要注入
 
2.注解注入
在yml文件中配置
 
 只需要在測試類中加入注解@value配置就可以了
 
六、數組
yml文件
實體類
package com.william.keepmoving.domain;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;import java.util.Arrays;/*** @author :lijunxuan* @date :Created in 2020/5/28 22:26* @description :* @version: 1.0*/ @Component @ConfigurationProperties(prefix = "user") public class User {private String city;private String country;private String[] home1;private String time;private String ip;private String password;private String test;@Overridepublic String toString() {return "User{" +"city='" + city + '\'' +", country='" + country + '\'' +", home1=" + Arrays.toString(home1) +", time='" + time + '\'' +", ip='" + ip + '\'' +", password='" + password + '\'' +", test='" + test + '\'' +'}';}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String[] getHome1() {return home1;}public void setHome1(String[] home1) {this.home1 = home1;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getTest() {return test;}public void setTest(String test) {this.test = test;} }執行后的效果
總結
以上是生活随笔為你收集整理的SpringBoot创建项目入门案例的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 如何使用笔记本电脑的摄像头录制视频笔记本
- 下一篇: 给笔记本电脑清灰笔记本电脑如何清灰
