Druid 在spring中的配置

时间:2023-09-04 13:52:32

Spring配置中dataSource配置

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <!-- 基本属性 url、user、password
-->
    <property name="url"
value="${jdbc.mysql.master.url}" />
    <property
name="username" value="${jdbc.mysql.master.username}" />
    <property
name="password" value="${jdbc.mysql.master.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'" />
    <property
name="testWhileIdle" value="true" />
    <property
name="testOnBorrow" value="false" />
    <property name="testOnReturn"
value="false" />

<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
    <property
name="poolPreparedStatements" value="true" />
    <property
name="maxPoolPreparedStatementPerConnectionSize" value="20"
/>

<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
    <property name="filters"
value="stat" />
</bean>

Web.xml配置文件中添加

<filter>
   
<filter-name>DruidWebStatFilter</filter-name>
   
<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
    <init-param>
        <param-name>exclusions</param-name>
       
<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
    </init-param>
</filter>
<filter-mapping>
   
<filter-name>DruidWebStatFilter</filter-name>
   
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>DruidStatView</servlet-name>
   
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
   
<servlet-name>DruidStatView</servlet-name>
   
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>

启动项目后,访问http://localhost:8080/*****/druid/sql.html 可以监控sql