druid数据库连接池配置

多数内容摘自alibaba/druid

1. 引入jar包

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>${druid-version}version>
dependency>

2. 在spring配置文件里配置dataSource


<bean id="druidDataSource_wxsr" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    
    <property name="url" value="${druid.wxsr.url}" />
    <property name="username" value="${druid.wxsr.username}" />
    <property name="password" value="${druid.wxsr.password}" />

    
    <property name="initialSize" value="1" />
    <property name="minIdle" value="1" />
    <property name="maxActive" value="20" />

    
    <property name="maxWait" value="60000" />

    
    <property name="timeBetweenEvictionRunsMillis" value="60000" />

    
    <property name="minEvictableIdleTimeMillis" value="300000" />

    <property name="validationQuery" value="SELECT 'x' FROM DUAL" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />

    
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

    
    <property name="filters" value="stat,log4j" />
bean>

3. 配置WebStatFilter

<filter>
    <filter-name>DruidWebStatFilterfilter-name>
    <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
    <init-param>
        <param-name>exclusionsparam-name>
        <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*param-value>
    init-param>
    <init-param>
        <param-name>profileEnableparam-name>
        <param-value>trueparam-value>
    init-param>
filter>
<filter-mapping>
    <filter-name>DruidWebStatFilterfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

4. 用于展示Druid的统计信息的配置


<servlet>
    <servlet-name>DruidStatViewservlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
    <init-param>
        
        <param-name>resetEnableparam-name>
        <param-value>trueparam-value>
    init-param>
    <init-param>
        
        <param-name>loginUsernameparam-name>
        <param-value>druidparam-value>
    init-param>
    <init-param>
        
        <param-name>loginPasswordparam-name>
        <param-value>druid123param-value>
    init-param>
servlet>
<servlet-mapping>
    <servlet-name>DruidStatViewservlet-name>
    <url-pattern>/druid/*url-pattern>
servlet-mapping>

5. 访问统计页面

http://127.0.0.1:8080/xxxx/druid/index.html

你可能感兴趣的:(数据库)