SpringBoot的yaml文件

yaml文件

1、yaml数据格式

在这里插入图片描述
在这里插入图片描述
把之前的properties和yaml文件删除掉,只用yml文件

server:
  port: 80

likes:
  - game
  - music
  - sleep

likes2: [game,music,sleep]

user1:
  - name: zhangsan
    age: 18
  - name: lisi
    age: 20

user2:
    -
      name: zhangsan
      age: 18
    -
      name: lisi
      age: 20

user3: [{name:zhangsan,age:18},{name:lisi,age:20}]

country: china
province: Hubei
city: Wuhan

dog:
    name: wangcai
    age: 1

语法规则

在这里插入图片描述

2、读取yaml单一数据

package com.example.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//rest模式
@RestController
@RequestMapping("/books")
public class BookController {

    //读取yaml文件中的单一数据
    @Value("${country}")
    private String country1;

    //注意dog下的属性没有-符号
    @Value("${dog.name}")
    private String name1;

    @Value("${likes[1]}")
    private String like1;

    @GetMapping
    public  String gerById(){
        System.out.println("SpringBoot is running···");
        System.out.println("country1:"+country1);
        System.out.println("like1:"+like1);
        return "SpringBoot is running···";
    }
}


运行,打开浏览器,输入网址http://localhost:80/books,返回控制台看见如下图
在这里插入图片描述

在这里插入图片描述

3、yaml文件中的变量引用

在这里插入图片描述
转义字符

4、读取yaml文件全部属性数据

package com.example.controller;

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;

//rest模式
@RestController
@RequestMapping("/books")
public class BookController {

    //读取yaml文件中的单一数据
    @Value("${country}")
    private String country1;

    //注意dog下的属性没有-符号
    @Value("${dog.name}")
    private String name1;

    @Value("${likes[1]}")
    private String like1;

    //使用自动装配将所有的数据封装到一个对象Environment中
    @Autowired
    private Environment env;


    @GetMapping
    public  String gerById(){
        System.out.println("SpringBoot is running···");
        System.out.println("country1:"+country1);
        System.out.println("name1:"+name1);
        System.out.println("like1:"+like1);
        System.out.println("--------------------------");
        System.out.println(env.getProperty("dog.name"));
        System.out.println(env.getProperty("likes[1]"));
        return "SpringBoot is running···";
    }
}

在这里插入图片描述
在这里插入图片描述

5、读取yamml文件引用数据类型属性数据

在yaml文件中添加数据

#创建类,用于封装下面的数据
#由Spring替我们加载数据对象 一定要告诉Spring加载这组信息
#使用时候从Spring中直接获取信息使用
datasource:
  driver: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306
  root: root
  password: 123456

在example包下创建文件MyDatasource

package com.example;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//1、定义数据模型封装yaml文件中对应的数据
//2、定义由spring管控的Bean
@Component
//3.指定加载的数据
@ConfigurationProperties("datasource")
public class MyDatasource {
    /*
    datasource:
        driver: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306
        root: root
        password: 123456
     */
    private String driver;
    private String url;
    private String root;
    private String password;


    //添加toString()方法
    //添加所有属性的getter和setter方法
package com.example.controller;

import com.example.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;

//rest模式
@RestController
@RequestMapping("/books")
public class BookController {

    //读取yaml文件中的单一数据
    @Value("${country}")
    private String country1;

    //注意dog下的属性没有-符号
    @Value("${dog.name}")
    private String name1;

    @Value("${likes[1]}")
    private String like1;

    //使用自动装配将所有的数据封装到一个对象Environment中
    @Autowired
    private Environment env;

    @Autowired
    private MyDatasource myDatasource;

    @GetMapping
    public  String gerById(){
        System.out.println("SpringBoot is running···");
        System.out.println("country1:"+country1);
        System.out.println("name1:"+name1);
        System.out.println("like1:"+like1);
        System.out.println("--------------------------");
        System.out.println(env.getProperty("dog.name"));
        System.out.println(env.getProperty("likes[1]"));
        System.out.println("--------------------------");
        System.out.println(myDatasource);
        return "SpringBoot is running···";
    }
}

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值