Springboot-Druid 配置

本文主要介绍了在SpringBoot中如何配置Druid数据源,包括初始化连接数、最大和最小连接池大小、连接超时时间等关键参数,并详细讲解了Druid的监控配置和JDBC密码加密设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis配置(工作中用springdata-jpa这个不用配)

mybatis.type-aliases-package=com.example.miaosha_xdp.entity
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
mybatis.mapperLocations = classpath:mapper/*.xml

druid配置

spring.datasource.url=jdbc:mysql://xdp:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

初始化时建立物理连接的个数

spring.datasource.druid.initial-size=5

最大连接池数量

spring.datasource.druid.max-active=30

最小连接池数量

spring.datasource.druid.min-idle=5

获取连接时最大等待时间,单位毫秒

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 1 FROM DUAL

建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。

spring.datasource.druid.test-while-idle=true

申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。

spring.datasource.druid.test-on-borrow=false

归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。

spring.datasource.druid.test-on-return=false

是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。

spring.datasource.druid.pool-prepared-statements=true

要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。

spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50

配置监控统计拦截的filters,去掉后监控界面sql无法统计

spring.datasource.druid.filters=stat,wall

通过connectProperties属性来打开mergeSql功能;慢SQL记录

spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

合并多个DruidDataSource的监控数据

spring.datasource.druid.use-global-data-source-stat=true

druid连接池监控

spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123

排除一些静态资源,以提高效率

spring.datasource.druid.web-stat-filter.exclusions=.js,.gif,.jpg,.png,.css,.ico,/druid/*

JDBC配置

配置密码加密

publickey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIonPACL8ikgJDdYrDBbTtCx53uQSRltqkoRUAjmGJGFXVBLH6fCYq3StG5P7XzWSXAnUIKERZMxbJv0nDJOM90CAwEAAQ==

spring.datasource.druid.filter.config.enabled=true
spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=${public-key}

Spring Boot是一个用于简化Spring应用开发的框架,而Druid是一个高效的数据库连接池。在Spring Boot项目中使用Druid连接池可以提供高性能的数据库连接管理和监控功能。 要在Spring Boot中使用Druid连接池,需要以下步骤: 1. 在项目的pom.xml文件中添加Druid依赖: ```xml <dependencies> <!-- Spring Boot Starter JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Druid依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> </dependencies> ``` 2. 在application.properties或application.yml文件中配置Druid连接池相关属性,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # Druid连接池配置 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 spring.datasource.druid.validation-query=SELECT 1 spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.filters=stat spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.use-global-data-source-stat=true ``` 3. 在启动类上添加`@EnableTransactionManagement`和`@MapperScan`注解,例如: ```java @SpringBootApplication @EnableTransactionManagement @MapperScan("com.example.mapper") public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 以上是在Spring Boot项目中使用Druid连接池的基本配置步骤,你可以根据自己的需求修改配置参数来满足具体业务场景。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值