Druid数据源相关内容

第一点,上百度查资料;

算了,查不到;就是一个简单的数据库连接池,和以前的c3p0一样;

官方文档:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

我们要做的就是使用一个类来对我们的Druid进行一个配置;

由于spring boot自带有我们的servlet容器,所以我们不需要使用web.xml来配置我们的Druid相关属性,我们只需要把他交给spring boot进行一个托管就可以了

先来看一下,web.xml应该如何配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<property name="slowSqlMillis" value="10000" />
<property name="logSlowSql" value="true" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
... ...
<property name="filters" value="log4j" />
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
</list>
</property>
</bean>

由官方指出的文档我们就要明白,我们所有的配置名,都会对应一个,所以我们的配置名字一定不可以写错;

由于spring boot不会自动注入一些配置,所以我们自己要配置一些配置文件,不然无法监听sql或者其他更大的问题,配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许报错,java.lang.ClassNotFoundException: org.apache.Log4j.Properity
#则导入log4j 依赖就行
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

image-20201002185047116