Springboot 配置yml文件与映射到java类

YAML语法:

1、基本语法 k:(空格)v:表示一对键值对(空格必须有);

以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的

属性和值也是大小写敏感;

2、值的写法 字面量:普通的值(数字,字符串,布尔)

k: v:字面直接来写; 字符串默认不用加上单引号或者双引号;

"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思

name: "zhangsan \n lisi":输出;zhangsan 换行 lisi '':

单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据

name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi

对象、Map(属性和值)(键值对):

k: v:在下一行来写对象的属性和值的关系;

注意缩进 对象还是k: v的方式

 

 

我们可以导入配置文件处理器,以后编写配置就有提示了

<!--配置  yml文件处理器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

完整代码:

Dog类

public class Dog {
    String name;
    int age;
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

person 类

Person 类
//将配置文件中的值映射到person中
//@ConfigurationProperties 告诉springboot将本类中的所有属性与配置文件中相关的属性配置
//这个组件是容器中的组件,才能提供功能加@Component注解
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    String name;
    int age;
    Date birth;
    Map<String,Object> map;
    Dog dog;
    List list;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", map=" + map +
                ", dog=" + dog +
                ", list=" + list +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public Date getBirth() {
        return birth;
    }

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

    public Map<String, Object> getMap() {
        return map;
    }

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

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}

yml文件中的配置:

person:
    name: sunchao
    age: 12
    birth: 2022/12/12
    map: {k1: v1,k2: v2}
    list: [lisi,zhangsan]
    dog:
        name: xiaogou
        age: 2

 测试类:

package com.sunchao.demo;

import com.sunchao.demo.bean.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {
    @Autowired
    Person person;
    @Test
    void contextLoads() {
        System.out.println(person);
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Excel数据转换为YAML格式并附加到现有的YAML文件中,您可以按照以下步骤进行操作: 1. 读取Excel文件并将其转换为Java对象。 2. 使用Jackson库将Java对象转换为YAML字符串。 3. 打开现有的YAML文件并将其读取为字符串。 4. 将新的YAML字符串附加到现有的YAML字符串。 5. 将附加后的YAML字符串写回到现有的YAML文件中。 下面是一个示例代码,可以帮助您实现这个过程: ```java import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelToYamlAppender { public static void main(String[] args) { // Step 1: Read Excel file and convert to Java object List<Map<String, String>> excelData = readExcelFile("input.xlsx"); // Step 2: Convert Java object to YAML string String yamlData = convertToYaml(excelData); // Step 3: Read existing YAML file as string String existingYaml = readYamlFile("existing.yaml"); // Step 4: Append new YAML data to existing YAML data String appendedYaml = existingYaml + yamlData; // Step 5: Write appended YAML data back to YAML file writeYamlFile("existing.yaml", appendedYaml); } private static List<Map<String, String>> readExcelFile(String filePath) { List<Map<String, String>> excelData = null; try (Workbook workbook = new XSSFWorkbook(new File(filePath))) { Sheet sheet = workbook.getSheetAt(0); for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row != null) { String key = row.getCell(0).getStringCellValue(); String value = row.getCell(1).getStringCellValue(); // Create Java object with key-value pairs Map<String, String> data = Map.of(key, value); excelData.add(data); } } } catch (IOException e) { e.printStackTrace(); } return excelData; } private static String convertToYaml(List<Map<String, String>> data) { String yaml = null; try { ObjectMapper objectMapper = new ObjectMapper(new YAMLMapper()); yaml = objectMapper.writeValueAsString(data); } catch (IOException e) { e.printStackTrace(); } return yaml; } private static String readYamlFile(String filePath) { String yamlData = null; try { yamlData = new String(Files.readAllBytes(Paths.get(filePath))); } catch (IOException e) { e.printStackTrace(); } return yamlData; } private static void writeYamlFile(String filePath, String yamlData) { try (FileWriter fileWriter = new FileWriter(new File(filePath))) { fileWriter.write(yamlData); } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,此示例代码仅供参考,并且可能需要根据您的特定需求进行修和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值