兩個文件的格式不同,都作爲springboot的配置文件,且優先級(application.properties>application.yml)
application.properties (鍵值對形式):
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.max-idle=10 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5
application.yml(樹形結構):
spring: devtools: restart: enabled: true #設置開啓熱部署 freemarker: cache: false #頁面不加載緩存,修改即時生效 datasource: type: com.alibaba.druid.pool.DruidDataSource #配置當前要使用的數據源的操作類型那個 driver-class-name: com.mysql.jdbc.Driver #配置MySQL的驅動程序類 url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false username: root password: root filters: stat,wall,log4j #druid監控配置 dbcp2: min-idle: 5 #數據庫連接池的最小維持連接數 initial-size: 5 #初始化提供的連接數 max-total: 5 #最大的連接數 max-wait-millis: 200 #等待連接獲取的最大超時時間 mybatis: config-location: classpath:mybatis.cfg.xml #mybatis配置文件所在路徑 type-aliases-package: com.example.demo01.PoJo #定義所有操作類的別名所在包 mapper-locations: #所有的mapper映射文件 - classpath:mappers/*.xml
這裏犯了一個錯誤:
在application.yml中根節點spring: 不能多次使用,否則後面的配置不會生效
spring: devtools: restart: enabled: true #設置開啓熱部署 freemarker: cache: false #頁面不加載緩存,修改即時生效 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource #配置當前要使用的數據源的操作類型那個 driver-class-name: com.mysql.jdbc.Driver #配置MySQL的驅動程序類 url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false username: root password: root filters: stat,wall,log4j #druid監控配置 dbcp2: min-idle: 5 #數據庫連接池的最小維持連接數 initial-size: 5 #初始化提供的連接數 max-total: 5 #最大的連接數 max-wait-millis: 200 #等待連接獲取的最大超時時間