SpringBoot数据库链接
Druid连接池
直接在yml中配置相关内容即可
spring:
datasource:
username: root
password: root.123
url: jdbc:mysql://localhost:3306/library?useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
#直接指定DataSource
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置(需要自己配置DataSource在注入)
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
参数问题:默认参数不能全部赋值:
由于默认配置文件是和DataSourceProperties
中文件相对应的,由于默认对象中没有这么多参数,所以需要通过DruidConfig
配置文件自己注入Bean对象
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean(name = "dataSource")
public DataSource druidDataSource(){
return new DruidDataSource();
}
}
遇到日志导致的参数赋值问题:
添加文件依赖
<dependency>
<groupId>org.slf4j</groupId>