Druid集链接池,监控于一体整好复合当前项目的须要,项目是ssh结构,以前是用C3p0的,如今换一个链接池也是很简单的,首先spring配置DataSource,配置以下:css
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_user}" /> <property name="password" value="${jdbc_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>
目前这样的配置已经可以使用链接池,注意不要忘记了jar文件,下载地址:http://code.alibabatech.com/mvn/releases/com/alibaba/druid/html
而后是监控的配置:java
web.xmlmysql
<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>
filter能够监控webURl 访问
web
<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>
该配置能够访问监控界面spring
配置好后访问 sql
http://ip:port/projectName/druid/index.html具体和项目集成实例:express
1、相关JAR包apache
因为是基于Maven的项目,找起JAR包天然就方便了许多,咱们只须要知道JAR的名字或者其中关键字就能够很轻松的把JAR包以及依赖JAR下载下来,须要多少下多少。spring-mvc
这里给出pom的配置文件。
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>SpringMybatis</groupId> 5 <artifactId>SpringMybatis</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>SpringMybatis Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>org.springframework</groupId> 13 <artifactId>spring-core</artifactId> 14 <version>3.2.10.RELEASE</version> 15 </dependency> 16 <dependency> 17 <groupId>org.springframework</groupId> 18 <artifactId>spring-web</artifactId> 19 <version>3.2.10.RELEASE</version> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-webmvc</artifactId> 24 <version>3.2.10.RELEASE</version> 25 </dependency> 26 <dependency> 27 <groupId>org.mybatis</groupId> 28 <artifactId>mybatis</artifactId> 29 <version>3.2.8</version> 30 </dependency> 31 <dependency> 32 <groupId>org.mybatis</groupId> 33 <artifactId>mybatis-spring</artifactId> 34 <version>1.1.1</version> 35 </dependency> 36 <dependency> 37 <groupId>mysql</groupId> 38 <artifactId>mysql-connector-java</artifactId> 39 <version>5.0.2</version> 40 </dependency> 41 <dependency> 42 <groupId>junit</groupId> 43 <artifactId>junit</artifactId> 44 <version>4.12</version> 45 <scope>test</scope> 46 </dependency> 47 <dependency> 48 <groupId>com.alibaba</groupId> 49 <artifactId>druid</artifactId> 50 <version>1.0.11</version> 51 </dependency> 52 <dependency> 53 <groupId>log4j</groupId> 54 <artifactId>log4j</artifactId> 55 <version>1.2.17</version> 56 </dependency> 57 </dependencies> 58 <build> 59 <finalName>SpringMybatis</finalName> 60 </build> 61 </project>
因为Maven会把JAR包所依赖的JAR包也一块儿下载下来,这里咱们就不须要逐个去写Spring的相关JAR包。
这里用到了阿里巴巴温少的开源项目Druid的数据源,因此额外的多引入了一个Druid的JAR包。
关于JAR的扩展,若是有须要别的能够到:http://search.maven.org/ 去查找,而后复制到这个配置文件便可,Maven会帮咱们自动下载添加。
2、相关配置
spring.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" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd"> 13 14 <!-- 自动注入 --> 15 <context:component-scan base-package="lcw/service"/> 16 17 18 </beans>
mybatis-spring.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" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd"> 13 14 <!-- 配置数据源 --> 15 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 16 <property name="url" value="jdbc:mysql:///test"></property> 17 <property name="username" value="root"></property> 18 <property name="password" value="root"></property> 19 </bean> 20 21 22 <!-- Mybatis文件 --> 23 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 24 <property name="dataSource" ref="dataSource"></property> 25 <property name="mapperLocations" value="classpath:lcw/mapping/*.xml"></property> 26 </bean> 27 28 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 29 <property name="basePackage" value="lcw.dao"></property> 30 <property name="sqlSessionFactoryBeanName" value ="sqlSessionFactory"></property> 31 </bean> 32 33 34 <!-- 事务管理器 --> 35 <bean id="transactionManager" 36 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 37 <property name="dataSource" ref="dataSource"></property> 38 </bean> 39 40 <tx:annotation-driven transaction-manager="transactionManager" /> 41 42 </beans>
springmvc.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" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 13 "> 14 15 <!-- 启用spring mvc 注解 --> 16 <context:annotation-config /> 17 18 <!-- 设置使用注解的类所在的jar包 --> 19 <context:component-scan base-package="lcw.controller"></context:component-scan> 20 21 <!-- 完成请求和注解POJO的映射 --> 22 <bean 23 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 24 25 <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> 26 <bean 27 class="org.springframework.web.servlet.view.InternalResourceViewResolver" 28 p:prefix="/" p:suffix=".jsp" /> 29 30 31 </beans>
log4j.properties
1 # 2 # Copyright 2009-2012 the original author or authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 17 ### Global logging configuration 18 log4j.rootLogger=DEBUG, stdout 19 20 ### Uncomment for MyBatis logging 21 log4j.logger.org.apache.ibatis=DEBUG 22 23 ### Console output... 24 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 25 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 26 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n