← 返回首页
SpringBoot基础教程(五)
发表时间:2020-10-19 09:20:43
SpringBoot的配置文档

SpringBoot的有properties和yml两种配置文档格式。

1.properties配置文档范例

#properties格式的配置文档范例
#项目端口配置
server.port=8080
#上下文配置
server.servlet.context-path=/firstdemo

#数据库连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

#配置jpa
#帮我们自动生成表结构
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

#本地MySQL数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialSize=20
spring.datasource.minIdle=50
spring.datasource.maxActive=500

#配置自定义属性
usersystem.upload.path=/temp/upload

2.yml格式配置文档范例

#yml格式配置文档范例
server:
  #端口配置
  port: 8080
  #上下文
  servlet:
    context-path: /seconddemo
#数据源配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
    username: root
    password: root
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    filters: stat
    maxActive: 500
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20
  jpa:
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        format_sql: true
        enable_lazy_load_no_trans: true
        hbm2ddl:
          auto: update
#自定义属性
usersystem:
  upload:
    path: \temp\upload