spring boot配置文件application.properties配置JPA以及数据源

2022-10-16,,,,

1.application.properties配置jpa模板

spring.datasource.url=jdbc:mysql://localhost:3306/springboottest?useunicode=true&characterencoding=utf8&servertimezone=utc
spring.datasource.username=yourname
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.driver

#jpa setting
spring.jpa.database-platform=org.hibernate.dialect.mysql5innodbdialect

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

注意:url上一定要加上时区servertimezone=utc ,否则会报错。

当使用jpa访问数据库时,一定要设置数据库方言

2.application.properties配置druid数据源

springboot数据源配置的默认类型是org.apache.tomcat.jdbc.pool.datesource,为了使用druid连接池,可以将数据源类型更改为alibaba.druid.pool.druiddatasource。

修改数据源为druid的配置如下:

spring.datasource.type=com.alibaba.druid.pool.druiddatasource

配置参数设定数据源的工作方式,常用的数据源属性配置如下:

spring.datasource.initsize=5
spring.datasource.minidle=5
spring.datasource.maxactive=20
spring.datasource.maxwait=6000

#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timebetweenevictionrunsmillis=60000

#配置一个连接在池中最小生存时间,单位是毫秒
spring.datasource.minevictableidletimemillis=300000

 3.druid实现监控功能

druid简介:

  druid是一个关系型数据库连接池,druid支持所有jdbc兼容的数据库,包括mysql,ororacle,derby,h2,sql server等。druid在监控、可扩展性、稳定性和性能方面有明显优势。通过druid提供的监控功能可以实时观察数据库连接池和sql查询的工作情况。使用druid连接池在一定程度上可以提高数据库的访问性能。

druid依赖:

<dependency>
  <groupid>com.alibaba</groupid>
  <artifactid>druid</artifactid>
  <version>1.1.10</version>
</dependency>

监控功能的相关配置:

spring.datasource.filters=stat
spring.datasource.validationquery: select 1 from dual
spring.datasource.testwhileidle: true
spring.datasource.testonborrow: false
spring.datasource.testonreturn: false
spring.datasource.poolpreparedstatements: true
spring.datasource.maxopenpreparedstatements: 20
spring.datasource.filters: stat,wall,log4j
spring.datasource.connectionproperties: druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000
#spring.datasource.useglobaldatasourcestat=true

 

《spring boot配置文件application.properties配置JPA以及数据源.doc》

下载本文的Word格式文档,以方便收藏与打印。