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

执行后的效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值