yaml支持的三种数据的结构及其获取(代码实战)

PS:yaml与yml一样哈,这里我就用yaml,因为多个啊,哈哈哈哈

YAML 支持的三种数据的结构

  • 常见普通值:单个的、不可再分的值
  • 对象:键值对的集合
  • 数组:一组按次序排列的值

yaml的数据的结构

1.单个的,不能再分割的值

#第一种,不能再分割的值
name: rose

2.对象:键值对的集合

#第二种,对象,键值对的集合
person:
  name: zhangsan
  age: 20
  sex: male

将对象写在一行上,键与值之间也要有空格

#一行
user: {
    
    name: zhangsan, age: 20, sex: male}

3.数组:一组按次序排列的值,-与值之间也要空格

#第三种,数组或集合,一组按次序排列的值
names:
  - zhangsan
  - lisi 
  - wangwu

# 或者
names: [ 'zhangsan', 'lisi', 'wangwu' ] 

数组中存的如果是对象

users: [ {
    
     name: 'tom', age: 2 }, {
    
     name: 'jerry', age: 3 } ]

或者

users:
  - {
    
    name: tom, age: 2}
  - {
    
    name: jerry, age: 3}

name和age的左侧要对齐

users:
  - name: tom
   age: 2
  - name: jerry
   age: 3

数组如果放在一行:

lists: [zhangsan,lisi,wangwu]

yaml数据的结构获取案例

我们先在yaml中编写一个学生类型的数据结构:

#配置student  studentName--> student-name
student:
  student-name: 螺丝
  age: 18
  sex: true
  birth: 2000-07-04
  maps:
    k1: v1
    k2: v2
  lists:
    - wangliu
    - 玉如梦
  book:
    book-name: "流楚丶格念的博客"
    author: "杨小心"

在这里插入图片描述

创建实体类包
在这里插入图片描述
创建Book实体类

package com.yyl.entity;

public class Book {
    
    
    private String bookName;
    private String author;

    public String getBookName() {
    
    
        return bookName;
    }

    public void setBookName(String bookName) {
    
    
        this.bookName = bookName;
    }

    public String getAuthor() {
    
    
        return author;
    }

    public void setAuthor(String author) {
    
    
        this.author = author;
    }

    @Override
    public String toString() {
    
    
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

创建Student实体类

这里的@Component是将student对象放入容器,一会我们要从测试代码里注入这个对象,之后我们要读取主配置文件中的数据,赋值给student对象,使用@ConfigurationProperties(prefix = "student") 来获取属性,里面的prefix用来配置指定主配置文件中的数据。

PS:这里用@Value也是可以的,下一节来讲,主要思想是:@ConfigurationProperties是可以批量注入的,一次搞定,而@Value只能一个一个注入

package com.yyl.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Component// 将student对象放入容器
// 读取主配置文件中的数据,赋值给student对象
@ConfigurationProperties(prefix = "student") // 需要属性,指定主配置文件中的数据
public class Student {
    
    

    private String studentName;
    private Integer age;
    private Boolean sex;//男代表true
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Book book;

    public String getStudentName() {
    
    
        return studentName;
    }

    public void setStudentName(String studentName) {
    
    
        this.studentName = studentName;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }

    public Boolean getSex() {
    
    
        return sex;
    }

    public void setSex(Boolean sex) {
    
    
        this.sex = sex;
    }

    public LocalDate getBirth() {
    
    
        return birth;
    }

    public void setBirth(LocalDate birth) {
    
    
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
    
    
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
    
    
        this.maps = maps;
    }

    public List<Object> getLists() {
    
    
        return lists;
    }

    public void setLists(List<Object> lists) {
    
    
        this.lists = lists;
    }

    public Book getBook() {
    
    
        return book;
    }

    public void setBook(Book book) {
    
    
        this.book = book;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "studentName='" + studentName + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", book=" + book +
                '}';
    }
}

创建测试方法

package com.yyl;

import com.yyl.entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

//使用springboot环境测试
@SpringBootTest
public class QuickStartTest {
    
    

    @Autowired
    private Student student;

    @Test
    void contextLoading() {
    
    
        System.out.println(student);
    }
}

打印结果:

Student{
    
    studentName='螺丝', age=18, sex=true, birth=2000-07-04, maps={
    
    k1=v1, k2=v2}, lists=[wangliu, 玉如梦], book=Book{
    
    bookName='流楚丶格念的博客', author='杨小心'}}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45525272/article/details/126794950