http://blog.csdn.net/wochunyang/article/details/52692619


1. Druid是什么?

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

2. 在哪里下载druid

 正式版本下载:
    maven中央仓库: http://central.maven.org/maven2/com/alibaba/druid/
  • 1
  • 2

3. 怎么获取Druid的源码

Druid是一个开源项目,源码托管在github上,源代码仓库地址是 : 
https://github.com/alibaba/druid 
同时每次Druid发布正式版本和快照的时候,都会把源码打包,你可以从上面的下载地址中找到相关版本的源码 
4. 怎么配置maven

Druid 0.1.18 之后版本都发布到maven中央仓库中,所以你只需要在项目的pom.xml中加上dependency就可以了。例如:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>${druid-version}</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

也可以选择 Maven仓库查找公共的仓库地址: 
http://www.mvnrepository.com/artifact/com.alibaba/druid

5. 怎么打开Druid的监控统计功能

applicationContext.xml 数据源配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <description>Spring公共配置文件 </description>
    <context:property-placeholder
        ignore-resource-not-found="true" location="classpath:spring/jdbc.properties" />

    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <context:component-scan base-package="com">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>


    <tx:annotation-driven />

    <!-- beecell_weixin_card 数据库配置连接开始 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/sqlmapper/sqlConfig.xml" />
        <property name="mapperLocations" value="classpath:/sqlmapper/**/*sqlmapper.xml" />
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <constructor-arg index="1" value="REUSE" />
    </bean>

    <!-- 事务管理器配置, 使用jdbc事务 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- spring declarative transaction management -->
    <aop:config>
        <aop:pointcut id="fooServiceMethods" expression="execution(* com.serviceImpl..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="create*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="save*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="insert*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="update*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="delete*" rollback-for="Exception" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.mysql.url}" />
        <property name="username" value="${jdbc.mysql.username}" />
        <property name="password" value="${jdbc.mysql.password}" />

        <!-- Connection Pooling Info -->
        <property name="maxActive" value="${druid.maxActive}" />
        <property name="minIdle" value="1" />
        <property name="defaultAutoCommit" value="false" />
        <!-- 连接Idle一个小时后超时 -->
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />
        <property name="minEvictableIdleTimeMillis" value="3600000" />
        <!-- DataSource Druid配置 -->
        <property name="validationQuery" value="SELECT SYSDATE() FROM DUAL" />
        <property name="testWhileIdle" value="true" />
        <property name="poolPreparedStatements" value="true" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
        <property name="filters" value="stat,config" /><!-- 开启druid监控支持(stat),SQL防火墙(wall)以及SQL合并(mergeStat) -->
        <property name="connectionProperties"
            value="druid.stat.slowSqlMillis=2000;config.decrypt=false" /><!-- 慢SQL标准 -->
        <property name="removeAbandoned" value="true" /> <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="1800" /> <!-- 1800秒,也就是30分钟 -->
        <property name="logAbandoned" value="true" /> <!-- 关闭abanded连接时输出错误日志 -->
    </bean>
    <!-- beecell_weixin_card 数据库配置连接结束 -->


    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />


    <!-- Druid AOP配置 -->
    <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>com.dao.base.*</value>
            </list>
        </property>
    </bean>
    <!-- 打开监控 -->
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor"
            pointcut-ref="druid-stat-pointcut" />
    </aop:config>
    <!-- 打开面向切面工具 -->
    <aop:aspectj-autoproxy />


</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120

web.xml 配置

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

访问地址: 
http://127.0.0.1:8080/项目/druid/index.html

这里写图片描述



参考文档: 
配置_StatFilter: https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter 
常见问题: https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98



本文转载:CSDN博客