← 返回首页
SpringBoot基础教程(三十一)
发表时间:2021-02-16 12:43:27
SpringBoot数据源配置

1.hikari配置 Hikari是一款非常强大,高效,并且号称“史上最快连接池”。并且在springboot2.0之后,采用的默认数据库连接池就是Hikari。不需要引入依赖,已经在SpringBoot中包含了。

yml中配置方式。

#yml格式配置文档范例
server:
  #端口配置
  port: 8080
  #上下文
  servlet:
    context-path: /springbootdemo
#数据源配置
spring:
  #开启热部署
  devtools:
    restart:
      enabled: true
      #让静态页面也能热部署
      additional-paths: resources/**,static/**,templates/**
  #配置数据源
  datasource:
    url: jdbc:mysql://localhost:3306/springcloud?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    #Springboot2.0 的hikari配置
    hikari:
      minimum-idle: 5
      #空闲连接存活最大时间,默认600000(10分钟)
      idle-timeout: 180000
      #连接池最大连接数,默认是10
      maximum-pool-size: 10
      #此属性控制从池返回的连接的默认自动提交行为,默认值:true
      auto-commit: true
      #连接池名称
      pool-name: MyHikariCP
      #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
      max-lifetime: 1800000
      #数据库连接超时时间,默认30秒,即30000
      connection-timeout: 30000
      connection-test-query: SELECT 1
  jpa:
    show-sql: true
    properties:
      hibernate:
        #dialect: org.hibernate.dialect.MySQL5Dialect ,注意这个是不支持事务的方言。
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        format_sql: true
        #配置懒加载策略
        enable_lazy_load_no_trans: true
        hbm2ddl:
          auto: update

2.druid配置 druid是alibaba开源平台上的一个数据库连接池实现,对比c3p0要高出不少。

pom中添加druid依赖

<properties>
    <druid.version>1.1.23</druid.version>
</properties>
...
<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>${druid.version}</version>
</dependency>

yml中配置方式。

#yml格式配置文档范例
server:
  #端口配置
  port: 8080
  #上下文
  servlet:
    context-path: /datasourcedemo
#数据源配置
spring:
  #使用自定义banner的配置
  output:
    ansi:
      enabled: always
  datasource:
    url: jdbc:mysql://localhost:3306/springboot?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    druid:
      initialSize: 5
      minIdle: 5
      maxActive: 20
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,此处是filter修改的地方
      filter:
        commons-log:
          connection-logger-name: stat,wall,log4j
        # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      #合并多个DruidDataSource的监控数据
      useGlobalDataSourceStat: true
  jpa:
    show-sql: true
    properties:
      hibernate:
        #dialect: org.hibernate.dialect.MySQL5Dialect ,注意这个是不支持事务的方言。
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        format_sql: true
        #配置懒加载策略
        enable_lazy_load_no_trans: true
        hbm2ddl:
          auto: update