druid相对于传统的c3p0和dbcp及其dbcp2等多个很多新特性
可以在线监控数据库及其表和sql以及Controller的requestMapping和对应的业务方面请求和session等
是个非常好的数据库连接池
一、添加如下依赖
<!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.24</version> </dependency>
二、web.xml配置
注意:放在前端控制器之前,也就是dispatchServlet之前
<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> <init-param> <!-- 用户名 --> <param-name>loginUsername</param-name> <param-value>druid</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginPassword</param-name> <param-value>druid</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
三、spring-mybatis.xml配置
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.126.128:3306/test" /> <property name="username" value="root" /> <property name="password" value="1234" /> <property name="filters" value="stat,wall,config,log4j" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="maxActive" value="100"/> <property name="minIdle" value="10"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="600000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="3000000" /> <property name="validationQuery" value="SELECT 'x' FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 超过时间限制是否回收 --> <property name="removeAbandoned" value="true" /> <!-- 超时时间;单位为秒。180秒=3分钟 --> <property name="removeAbandonedTimeout" value="18000" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <property name="useUnfairLock" value="true"></property> <property name="proxyFilters"> <list> <ref bean="stat-filter"/> <ref bean="log-filter"/> </list> </property> </bean> <!-- 慢SQL记录 --> <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter"> <!-- 慢sql时间设置,即执行时间大于200毫秒的都是慢sql --> <property name="slowSqlMillis" value="200"/> <property name="logSlowSql" value="true"/> </bean> <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter"> <property name="dataSourceLogEnabled" value="true" /> <property name="statementExecutableSqlLogEnable" value="true" /> </bean>
<bean id="druid-stat-interceptor"
class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
scope="prototype">
<property name="patterns">
<list>
<value>cn.pms.service.*</value>
<value>cn.pms.controller.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor"
pointcut-ref="druid-stat-pointcut" />
</aop:config>
四、最终的效果如下:
在浏览器输入IP:端口/项目名/druid即可
例如:http://192.168.126.128:8080/test/druid