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