yaml基本用法


簡介

YAML 是 "YAML Ain't Markup Language"(YAML 不是一種標記語言)的遞歸縮寫。在開發的這種語言時,YAML 的意思其實是:"Yet Another Markup Language"(仍是一種標記語言)。

非常適合用來做以數據為中心的配置文件

基本用法

  • key: value

    key和value之間有空格

  • 大小寫敏感

  • 使用縮進表示層級關系

  • 縮進不允許使用tab,只允許空格,但在idea上使用tab也可以

  • 縮進的空格數不重要,只要相同層級的元素左對齊即可

  • #表示注釋

  • 字符串無需加引號,如果要加,單引號會被轉義,雙引號不會不轉義

數據類型

字面量

單個的、不可再分的值。date、boolean、string、number、null

k: v

對象

鍵值對的集合。map、hash、set、object

#行內寫法:
k: {k1:v1,k2:v2,k3:v3}
#或
k: 
  k1: v1
  k2: v2
  k3: v3

數組

一組按次序排列的值。array、list、queue

#行內寫法: 
k: [v1,v2,v3]
#或者
k:
 - v1
 - v2
 - v3

示例

@Data
public class Person {
    
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}

@Data
public class Pet {
    private String name;
    private Double weight;
}

# yaml表示以上對象
person:
  userName: zhangsan
  boss: false
  birth: 2019/12/12 20:12:33
  age: 18
  pet: 
    name: tomcat
    weight: 23.4
  interests: [籃球,游泳]
  animal: 
    - jerry
    - mario
  score:
    english: 
      first: 30
      second: 40
      third: 50
    math: [131,140,148]
    chinese: {first: 128,second: 136}
  salarys: [3999,4999.98,5999.99]
  allPets:
    sick:
      - {name: tom}
      - {name: jerry,weight: 47}
    health: [{name: mario,weight: 47}]	

配置提示

自定義的類和配置文件綁定一般沒有提示。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
</dependency>


 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
 </build>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM