Spring Boot的application.yml
配置文件是一个重要的部分,它允许你自定义应用程序的各种设置。以下是一些常见的配置项:
-
服务器配置:
https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.server
server: port: 8080 # 端口 servlet: context-path: / # 应用程序上下文路径【设置访问路径前缀】 session: timeout: 3600 # 配置会话(session)超时时间,单位秒
当你设置了上下文路径后,所有的请求URI必须以该上下文路径开头,才能访问到应用程序3。例如,如果你将上下文路径设置为/myapp,那么一个名为/api/test的接口,其完整的URL将会变为localhost:8080/myapp/api/test
-
数据库配置:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver # MySQL驱动类名 url: jdbc:mysql://localhost:3306/库名?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true # 数据库URL username: root # 用户名 password: 123456 # 密码
-
连接池配置:
hikari: # Hikari连接池的配置 minimum-idle: 15 # 连接池中保持的最小空闲连接数 maximum-pool-size: 25 # 连接池的最大连接数 idle-timeout: 400000 # 连接在连接池中保持空闲的最大时间(单位为毫秒),超过该时间会被释放 max-lifetime: 580000 # 连接在连接池中的最大生命周期(单位为毫秒),超过该时间会被释放 connection-timeout: 65000 # 获取连接的超时时间(单位为毫秒) connection-test-query: SELECT 1 # 用于测试连接是否有效的SQL查询语句
切换到Druid数据库连接池
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis username: root password: 1234 或 spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis username: root password: 1234
【Springboot3的配置】
# 连接池配置 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource # Druid的其他属性配置 springboot3整合情况下,数据库连接信息必须在Druid属性下! druid: url: jdbc:mysql://localhost:3306/mybatis username: root password: 1234 driver-class-name: com.mysql.cj.jdbc.Driver # 初始化时建立物理连接的个数 initial-size: 5 # 连接池的最小空闲数量 min-idle: 5 # 连接池最大连接数量 max-active: 20 # 获取连接时最大等待时间,单位毫秒 max-wait: 60000 # 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 test-while-idle: true # 既作为检测的间隔时间又作为testWhileIdel执行的依据 time-between-eviction-runs-millis: 60000 # 销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时,关闭当前连接(配置连接在池中的最小生存时间) min-evictable-idle-time-millis: 30000 # 用来检测数据库连接是否有效的sql 必须是一个查询语句(oracle中为 select 1 from dual) validation-query: select 1 # 申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true test-on-borrow: false # 归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true test-on-return: false # 是否缓存preparedStatement, 也就是PSCache,PSCache对支持游标的数据库性能提升巨大,比如说oracle,在mysql下建议关闭。 pool-prepared-statements: false # 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100 max-pool-prepared-statement-per-connection-size: -1 # 合并多个DruidDataSource的监控数据 use-global-data-source-stat: true
-
MyBatis配置:
mybatis: configuration: # setting配置 auto-mapping-behavior: full #指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示关闭自动映射;PARTIAL 只会自动映射没有定义嵌套结果映射的字段。 FULL 会自动映射任何复杂的结果集(无论是否嵌套)在多表时必须开启 map-underscore-to-camel-case: true #驼峰映射开启 # log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl #日志开启 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制台日志开启 type-aliases-package: com.hsc.pojo # 配置别名【可以省略返回对象时全限定类名的书写】 #mapper-locations: classpath:/mapper/*.xml #设置mapperxml扫描位置
mybatis-plus: mapper-locations: classpath:mapper/*Mapper.xml # 指定了Mapper XML文件所在的位置 configuration: lazy-loading-enabled: true # 开启懒加载 map-underscore-to-camel-case: true # 开启下划线到驼峰命名的自动映射转换 global-config: db-config: id-type: auto # 主键ID策略,auto表示自动增长
-
Redis配置:
redis: host: localhost port: 6379 password: 123456 pool: max-wait: 1600 # 当连接池耗尽时,调用者在等待可用连接时的最大等待时间(单位为毫秒) max-active: 40 # 连接池中的最大活动连接数 max-idle: 20 # 连接池中的最大空闲连接数 min-idle: 0 # 连接池中的最小空闲连接数 database: 0 # Redis数据库的索引,默认为0(用于区分不同的数据集)
-
日志级别配置:
logging: level: root: ERROR # 根日志记录器的级别,这里设置为ERROR,表示只记录错误日志信息 com.lck.mypackage: DEBUG # 包日志记录器,这里设置为DEBUG,表示记录DEBUG级别的日志信息
以上是一些常见的配置项,你可以根据实际需要进行修改和添加。希望这个总结对你有所帮助!
源:
(1) Spring Boot项目中yml文件的常见配置 - 优快云博客. https://blog.youkuaiyun.com/melck/article/details/133094420.
(2) springboot .yml配置所有内容总结_boot.yml-优快云博客. https://blog.youkuaiyun.com/youtiantin/article/details/118113839.
(3) Spring Boot配置文件(YAML & Properties)总结 - 腾讯云. https://cloud.tencent.com/developer/article/2370644.
(4) Spring Boot配置文件(YAML & Properties)总结 - IT·陈寒 - 博客园. https://www.cnblogs.com/ithan/p/17921081.html.
(5) Spring Boot常用配置,yml文件-优快云博客. https://blog.youkuaiyun.com/m0_69134284/article/details/136712251.
(6) SpringBoot入门——yml配置文件使用详解 - 优快云博客. https://blog.youkuaiyun.com/wk_cjs/article/details/134596080.