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文件中加入配置處理器依賴
不加配置處理器依賴會提示
在這裏插入圖片描述

        <!--配置處理器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

3.user要和yml文件中的user相同,user實體類要加入對應的get(),set()方法
4.點擊實體類中的圖標會跳轉到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[] home;
    private String time;
    private String ip;
    private String password;
    private String test;

    @Override
    public String toString() {
        return "User{" +
                "city='" + city + '\'' +
                ", country='" + country + '\'' +
                ", home=" + Arrays.toString(home) +
                ", 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[] getHome() {
        return home;
    }

    public void setHome(String[] home) {
        this.home = home;
    }

    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;
    }
}

在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;

    @Override
    public 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;
    }
}

執行後的效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章