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