SpringBoot-07配置文件

1、properties

同传统配置文件

2、yaml

springboot推荐使用,适合以数据为中心的配置文件

2.1基本语法

  • key: value;kv之间有空格
  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释
  • 字符串无需加引号,如果要加,’'与""表示字符串内容 会被 转义/不转义
  • 通过@ConfigurationProperties(prefix = "student")使用
  • 双引号 \n 换行,单引号转义不换行

实例:

  • 对象实例
@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表示以上对象
# 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}]

例2

/**
 * 配置绑定
 * 只有注册到容器中的组件,才会拥有SpringBoot提供的功能
 */
//@Component
@ConfigurationProperties(prefix = "student")

public class ConfigStudent {
    private Long id;
    private String name;
    private Integer age;
}
server:
  port: 8003

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver

  #自定义日期格式化配置
  mvc:
    format:
      date: dd/MM/yyyy

#2、mybatis-plus配置
mybatis-plus:
  #2-1 lodging 日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  #2-2 deleted 逻辑删除
  global-config :
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

#对象赋值
student:
  id: 1
  name: pige
  age: 17
  ###############布尔型
  #  happy: true
  ###############日期
  #  birth: 1998/09/09
  ###############map
  #  maps: {k1: v1,k2: v2}
  ###############list
#  lists:
#    - play
#    - music
#    - girl

#查看配置
debug: true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值