springBoot+gradle+postgresql运行环境配置以及集成druid连接池(application.yml)

一、没有连接池的配置

server:
  port: 4567

spring:
  datasource:
    username: postgres
    password: 123456
    url: jdbc:postgresql://localhost:5432/myjpaproject
    driver-class-name: org.postgresql.Driver
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQL95Dialect  #设置方言
    properties:
      hibernate:
        jdbc.lob.non_contextual_creation: true #防止postgresql数据库因未实现CLOB格式而造成的数据检查出错

或者多环境配置(此处仅添加了开发环境) ,测试与生产环境往后添加即可

server:
  port: 4567

spring:
  profiles:
      active: dev #默认激活的profiles
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQL95Dialect  #设置方言
    properties:
      hibernate:
        jdbc.lob.non_contextual_creation: true #防止postgresql数据库因未实现CLOB格式而造成的数据检查出错


---
spring:
  profiles: dev
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/myjpaproject
    username: postgres
    password: 123456

二、有连接池的配置

build.gradle中添加包

// druid数据库连接池
implementation 'com.alibaba:druid-spring-boot-starter:1.1.10'

配置:

server:
  port: 4567

spring:
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: org.postgresql.Driver
      url: jdbc:postgresql://localhost:5432/myjpaproject
      username: postgres
      password: 123456
      filters: stat #别名方式,扩展插件,监控统计用的filter:stat,日志用的filter:log4j,防御sql注入的filter:wall

或者

server:
  port: 4567

spring:
  profiles:
      active: dev #默认激活的profiles
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQL95Dialect  #设置方言
    properties:
      hibernate:
        jdbc.lob.non_contextual_creation: true #防止postgresql数据库因未实现CLOB格式而造成的数据检查出错


---
spring:
  profiles: dev
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: org.postgresql.Driver
      url: jdbc:postgresql://localhost:5432/myjpaproject
      username: postgres
      password: 123456
      filters: stat #别名方式,扩展插件,监控统计用的filter:stat,日志用的filter:log4j,防御sql注入的filter:wall

driud全部配置:

db-type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: org.postgresql.Driver
      url: jdbc:postgresql://localhost:5432/myjpaproject
      username: postgres
      password: 123456
      # 下面为连接池的补充设置,应用到上面所有数据源中
      # 初始化大小,最小,最大
      initialSize: 1
      minIdle: 3
      maxActive: 20
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 30000
      validationQuery: select 'x'
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,#别名方式,扩展插件,监控统计用的filter:stat,日志用的filter:log4j,防御sql注入的filter:wall
      filters: stat,slf4j
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

猜你喜欢

转载自blog.csdn.net/weixin_41996632/article/details/88344449