springBoot+mave+mysql運行環境配置以及集成druid連接池(application.properties)

一、無連接池

1、單一配置

server.port=7654

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisproject?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

2、或者多環境配置spring.profiles.active=test 其中test代表的就是application-test.properties文件配置,同理將test換成dev或者prod改變配置啓動環境,文件目錄圖

application-test.properties

server.port=7654

#默認激活的profiles
spring.profiles.active=test

application-test.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisproject?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

二、有鏈接池

添加包

pom.xml中添加

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-starter</artifactId>
	<version>1.1.10</version>
</dependency>

1、單一環境配置 

server.port=7654

#使用druid數據源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/mybatisproject?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
# 初始化大小,最小,最大
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
# 配置獲取連接等待超時的時間
spring.datasource.druid.max-wait=60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=300000
#檢測連接是否有效的sql
spring.datasource.druid.validation-query=SELECT 'x'
spring.datasource.druid.validation-query-timeout=60000
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
# PSCache Mysql下建議關閉
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=-1
#spring.datasource.druid.max-open-prepared-statements= #等價於上面的max-pool-prepared-statement-per-connection-size
# 配置監控統計攔截的filters,去掉後監控界面sql無法統計,'wall'用於防火牆,#別名方式,擴展插件,監控統計用的filter:stat,日誌用的filter:log4j,防禦sql注入的filter:wall
spring.datasource.druid.filters=stat,slf4j

2、或者多環境配置spring.profiles.active=dev 其中dev代表的就是application-dev.properties文件配置,同理將dev換成test或者prod改變配置啓動環境,文件目錄圖

application.properties

server.port=7654

#默認激活的profiles
spring.profiles.active=dev

application-dev.properties

#使用druid數據源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/mybatisproject?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
# 初始化大小,最小,最大
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
# 配置獲取連接等待超時的時間
spring.datasource.druid.max-wait=60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=300000
#檢測連接是否有效的sql
spring.datasource.druid.validation-query=SELECT 'x'
spring.datasource.druid.validation-query-timeout=60000
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
# PSCache Mysql下建議關閉
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=-1
#spring.datasource.druid.max-open-prepared-statements= #等價於上面的max-pool-prepared-statement-per-connection-size
# 配置監控統計攔截的filters,去掉後監控界面sql無法統計,'wall'用於防火牆,#別名方式,擴展插件,監控統計用的filter:stat,日誌用的filter:log4j,防禦sql注入的filter:wall
spring.datasource.druid.filters=stat,slf4j

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章