SpringBoot-yaml语法规则和读取数据

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: SpringBoot-yaml语法规则和读取数据

在这里插入图片描述
✨博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞🎀收藏⭐留言✒
✨系列专栏:👉SpringBoot专栏(每日更新)
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
✨欢迎大佬指正,一起学习!一起加油!
在这里插入图片描述


@TOC


前言

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。


👉yaml

  • YAML是一种数据序列化格式。
  • yaml扩展名

    • .yaml
    • .yml(主流)

👉yaml语法规则

  • 大小写敏感
  • 属性层次关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • 表示注释

⭐字面值表示方式:

boolean: true
float: 3.14
int: 15
#表示空
null: ~
string: xiaofeixia
date: 2022-7-9
#日期与时间用T连接
datetime: 2022-7-9T12:00:30+02:00

⭐数组表示方式:


likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

⭐对象数组格式:

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

⭐对象数组缩略格式:

user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]

👉读取yaml数据

  • 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名}

⭐编写yaml文件

server:
  port: 81
country: china
province: henan
city: zhengzhou
area: shangqiu

party: true
birthday: 2022-11-11

user8:
  name: xiaofeixia
  age: 22
user1:
  name: xiaofeixia
  age: 22

a:
  B:
    C:
      d:
        e: abc

likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

user4: [{name:xiaofeixia,age:21},{name:xiaofeixia,age:22}]

⭐读取单一数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    //读取yaml数据中的单一数据
    @Value("${country}")
    public String country1;

    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");
        System.out.println("country=="+country1);  //country==china      
        return "springboot is running...";
    }
}

⭐读取二级数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Value("${user8.name}")
    public String username;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("username=="+username);         //username==xiaofeixia            
        return "springboot is running...";
    }
}

⭐读取数组数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
 @Value("${likes[0]}")
    public String likes1;
     @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("likes1=="+likes1);  //likes1==music      
        return "springboot is running...";
    }
   
}

⭐读取服务器端口号

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
   @Value("${server.port}")
    public String port;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
        System.out.println("port=="+port);  //port==81   
        return "springboot is running...";
    }
}

⭐读取对象属性

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Value("${user2[0].age}")
    public String age2;
    @GetMapping
    public String ById(){
        System.out.println("springboot is running...");       
         System.out.println("age2=="+age2);  //age2==22         
        return "springboot is running...";
    }
}

⭐封装全部数据到Environment对象

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Autowired
    private Environment env;
    @GetMapping
    public String ById(){        
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("user8.name"));        
        return "springboot is running...";
    }
}

⭐读取yaml引用类型属性数据

⭐⭐application.yml

server:
  port: 81
#创建类用于封装下面的数据
#由spring去加载数据到对象中,一定要告诉spring加载这组信息
#使用的时候直接从spring中获取信息
datasource:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/springboot
  username: root
  password: root

⭐⭐MyDataSource

自定义对象封装指定数据
1.定义数据模型封装yaml文件中对应的数据

package com.jkj;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//2.定义spring的管控Bean
@Component
//3.指定加载数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
    private String driver;
    private String url;
    private String username;
    private String password;

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

⭐⭐读取数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
    @Autowired
    private MyDataSource myDataSource;
    @GetMapping
    public String ById(){     
        System.out.println(myDataSource);
        //MyDataSource{driver='com.mysql.jdbc.Driver', url='jdbc:mysql://localhost/springboot', username='root', password='root'}
        return "springboot is running...";
    }
}

⭐变量的引用

⭐⭐application.yml

server:
  port: 81
baseDir: E:\window
tempDir: ${baseDir}\temp

⭐⭐读取数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController {
     @Value("${tempDir}")
     public String temp;   
    @GetMapping
    public String ById(){     
         System.out.println("temp=="+temp);  //temp==E:\window\temp
        return "springboot is running...";
    }
}

⭐ context-path

只写:

server:
  port: 81

控制台输出:path为空
在这里插入图片描述
加上context-path后:

server:
  port: 81
  servlet:
    context-path: /test

控制台输出页面:
在这里插入图片描述
注意:在浏览器输入:http://localhost:81/test/yamlBooks 进行测试。

⭐@Autowired报错解决方案

  1. file
  2. setting
  3. 搜索Spring Core
  4. 如下图所示将Severity修改为Warning

在这里插入图片描述

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
Java
Springboot 导出word,动态填充表格数据
Springboot 导出word,动态填充表格数据
|
2月前
|
JSON JavaScript 前端开发
解决js中Long类型数据在请求与响应过程精度丢失问题(springboot项目中)
解决js中Long类型数据在请求与响应过程精度丢失问题(springboot项目中)
45 0
|
2月前
|
存储 搜索推荐 Java
|
3月前
|
数据采集 存储 缓存
SpringBoot与布隆过滤器的完美邂逅:高效防护大规模数据的奇妙结合【实战】
SpringBoot与布隆过滤器的完美邂逅:高效防护大规模数据的奇妙结合【实战】
120 0
|
1天前
|
SQL Java 数据库
springboot用户创建的业务数据只能是同一组织能看的见
springboot用户创建的业务数据只能是同一组织能看的见
|
10天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
22 0
|
11天前
|
SQL Java 数据库连接
Springboot框架整合Spring JDBC操作数据
JDBC是Java数据库连接API,用于执行SQL并访问多种关系数据库。它包括一系列Java类和接口,用于建立数据库连接、创建数据库操作对象、定义SQL语句、执行操作并处理结果集。直接使用JDBC涉及七个步骤,包括加载驱动、建立连接、创建对象、定义SQL、执行操作、处理结果和关闭资源。Spring Boot的`spring-boot-starter-jdbc`简化了这些步骤,提供了一个在Spring生态中更便捷使用JDBC的封装。集成Spring JDBC需要添加相关依赖,配置数据库连接信息,并通过JdbcTemplate进行数据库操作,如插入、更新、删除和查询。
|
11天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。
|
17天前
|
Java 数据库连接 数据库
【SpringBoot系列】微服务数据持久化方案
【4月更文挑战第8天】微服务数据持久化方案Spring Boot JPA + Hiberate
|
1月前
|
SQL Java 数据库连接
一文带你快速学会SpringBoot工程下MaBatis对数据的增删改查功能!
在SpringBoot项目中,已配置好Mybatis和Lombok,数据库tb_user有四条初始数据。需求是按ID删除用户。首先在UserMapper接口添加@Delete注解的删除方法,然后在单元测试类中测试此方法,成功删除ID为4的用户。删除方法可选返回影响的记录数,此处用void。参数名在#{...}内可自定义。通过配置mybatis日志在控制台显示SQL操作。

热门文章

最新文章