前言
在基于SpringMVC+MyBatis的开发过程中,我们希望能看到自己手写SQL的执行情况,在开发阶段我们可以配置log4j在控制台里基于debug模式查看,那么上线后,在生产声我们想查看SQL的执行情况呢,这时候就该druid出场了,druid自带一些监控界面,可以监控SQL、监控Web、监控URL。
使用druid监控SQL
使用druid监控URI
开发环境
idea2016、SpringMVC4、Mybatis3、druid1.0.28
SSM整合
1、pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.autohome</groupId> 5 <artifactId>SpringMVC3</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>SpringMVC3</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>4.10</version> 15 </dependency> 16 <dependency> 17 <groupId>org.springframework</groupId> 18 <artifactId>spring-core</artifactId> 19 <version>4.3.6.RELEASE</version> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-beans</artifactId> 24 <version>4.3.6.RELEASE</version> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-context</artifactId> 29 <version>4.3.6.RELEASE</version> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-web</artifactId> 34 <version>4.3.6.RELEASE</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-context-support</artifactId> 39 <version>4.3.6.RELEASE</version> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-webmvc</artifactId> 44 <version>4.3.6.RELEASE</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-jdbc</artifactId> 49 <version>4.3.6.RELEASE</version> 50 </dependency> 51 <dependency> 52 <groupId>org.apache.velocity</groupId> 53 <artifactId>velocity</artifactId> 54 <version>1.6.2</version> 55 </dependency> 56 <dependency> 57 <groupId>org.apache.velocity</groupId> 58 <artifactId>velocity-tools</artifactId> 59 <version>2.0</version> 60 </dependency> 61 <dependency> 62 <groupId>org.mybatis</groupId> 63 <artifactId>mybatis</artifactId> 64 <version>3.4.2</version> 65 </dependency> 66 <dependency> 67 <groupId>org.mybatis</groupId> 68 <artifactId>mybatis-spring</artifactId> 69 <version>1.3.0</version> 70 </dependency> 71 <dependency> 72 <groupId>com.microsoft.sqlserver</groupId> 73 <artifactId>sqljdbc4</artifactId> 74 <version>4.0</version> 75 </dependency> 76 <dependency> 77 <groupId>commons-dbcp</groupId> 78 <artifactId>commons-dbcp</artifactId> 79 <version>1.4</version> 80 </dependency> 81 <dependency> 82 <groupId>javax.servlet</groupId> 83 <artifactId>javax.servlet-api</artifactId> 84 <version>3.1.0</version> 85 </dependency> 86 <dependency> 87 <groupId>com.alibaba</groupId> 88 <artifactId>druid</artifactId> 89 <version>1.0.28</version> 90 </dependency> 91 <dependency> 92 <groupId>log4j</groupId> 93 <artifactId>log4j</artifactId> 94 <version>1.2.17</version> 95 </dependency> 96 </dependencies> 97 <build> 98 <finalName>SpringMVC3</finalName> 99 </build> 100 </project>
2、web.xml
使用druid监控主要配置DruidWebStatFilter、StatViewServlet、druid用户名和密码(可选)
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--告知javaEE容器,有那些内容需要添加到上下文里去--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--log4j参数配置--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:config/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!--spring 前端控制器--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <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> <!--druid监控servlet--> <servlet> <servlet-name>druidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <!-- 允许清空统计数据 --> <param-name>resetEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- 用户名 --> <param-name>loginUsername</param-name> <param-value>sqlserver</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginPassword</param-name> <param-value>123</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>druidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!--防止js,css等文件被springmvc拦截 让tomcat来处理--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/2sc/js/*</url-pattern> <url-pattern>/2sc/Style/*</url-pattern> </servlet-mapping> </web-app>
3、springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd 12 "> 13 14 <!--从配置文件加载数据库信息--> 15 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 16 <property name="locations" value="classpath:config/jdbc.properties"/> 17 <property name="fileEncoding" value="UTF-8"/> 18 </bean> 19 20 <!--配置数据源,这里使用Spring默认--> 21 <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">--> 22 <!--<property name="driverClassName" value="${sqlserver.driver}"/>--> 23 <!--<property name="url" value="${sqlserver.url}"/>--> 24 <!--<property name="username" value="${sqlserver.username}"/>--> 25 <!--<property name="password" value="${sqlserver.password}"/>--> 26 <!--</bean>--> 27 28 <!--配置数据源 druid--> 29 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 30 <property name="driverClassName" value="${sqlserver.driver}"/> 31 <property name="url" value="${sqlserver.url}" /> 32 <property name="username" value="${sqlserver.username}" /> 33 <property name="password" value="${sqlserver.password}" /> 34 35 <property name="filters" value="stat" /> 36 37 <property name="maxActive" value="20" /> 38 <property name="initialSize" value="1" /> 39 <property name="maxWait" value="60000" /> 40 <property name="minIdle" value="1" /> 41 42 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 43 <property name="minEvictableIdleTimeMillis" value="300000" /> 44 45 <property name="testWhileIdle" value="true" /> 46 <property name="testOnBorrow" value="false" /> 47 <property name="testOnReturn" value="false" /> 48 49 <property name="poolPreparedStatements" value="true" /> 50 <property name="maxOpenPreparedStatements" value="20" /> 51 </bean> 52 53 <!--扫描Mapper--> 54 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 55 <property name="basePackage" value="com.autohome.mapper"/> 56 </bean> 57 58 <!--配置sqlSessionFactory--> 59 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 60 <property name="configLocation" value="classpath:springmvc-mybatis.xml"/> 61 <property name="dataSource" ref="dataSource"/> 62 </bean> 63 64 <!--启用最新的注解器、映射器--> 65 <mvc:annotation-driven/> 66 67 <!--使用默认的Servlet来响应静态资源--> 68 <mvc:default-servlet-handler/> 69 70 <!--扫描Controller注解类--> 71 <context:component-scan base-package="com.autohome.controller" /> 72 <!--扫描Service注解类--> 73 <context:component-scan base-package="com.autohome.service"/> 74 75 <!--velocity模板配置--> 76 <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 77 <property name="resourceLoaderPath" value="/WEB-INF/views/"/> 78 <property name="configLocation" value="classpath:config/velocity.properties"/> 79 <property name="velocityProperties"> 80 <props> 81 <prop key="input.encoding">UTF-8</prop> 82 <prop key="output.encoding">UTF-8</prop> 83 </props> 84 </property> 85 </bean> 86 87 <!--配置视图解析器--> 88 89 <!--jsp视图解析器--> 90 <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">--> 91 <!--<property name="prefix" value="/WEB-INF/views/"/>--> 92 <!--<property name="suffix" value=".jsp"/>--> 93 <!--</bean>--> 94 95 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 96 <property name="suffix" value=".vm"/> 97 <property name="prefix" value=""/> 98 <property name="contentType" value="text/html;charset=UTF-8"/> 99 </bean> 100 101 </beans>
总结
项目结构我在第一篇ssm整合的基础上继续开发,加入了log4j、druid监控。
参考
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE
时间: 2024-12-29 11:14:00