1. 程式人生 > >SSM整合系列之 配置阿里DruidDataSource並實現SQL監控

SSM整合系列之 配置阿里DruidDataSource並實現SQL監控

摘要:Druid是阿里巴巴開源平臺上一個資料庫連線池實現,它結合了C3P0、DBCP、PROXOOL等DB池的優點,同時加入了日誌監控,可以很好的監控DB池連線和SQL的執行情況。正常訪問路IP地址/專案名/druid/index.html。本專案執行後效果如下圖
在這裡插入圖片描述
在這裡插入圖片描述
1.專案搭建
可以參考本系列文章,部落格地址:https://blog.csdn.net/caiqing116/article/details/84573166
或者直接下載專案,git地址:https://github.com/gitcaiqing/SSM_DEMO.git
2.Maven引入阿里Druid相關Jar包

<!-- 資料來源druid -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.11</version>
</dependency>

3.專案配置
(1)config/jdbc.config配置
如果沒有配置多資料來源,就配置一個連線即可

#連線驅動
jdbc.driverClassName=com.mysql.jdbc.Driver

#埠號3306的資料來源
jdbc.url = jdbc\:mysql\://localhost\:3306/db_ssmdemo?useUnicode\=true&amp;characterEncoding\=UTF-8&allowMultiQueries\=true
jdbc.username = root
jdbc.password = 123456

#埠號3308的資料來源
jdbc.3308.url = jdbc\:mysql\://localhost\:3308/db_ssmdemo?useUnicode\=true&amp;characterEncoding\=UTF-8&allowMultiQueries\=true
jdbc.3308.username = root
jdbc.3308.password = 123456

#定義初始連線數 
jdbc.initialSize=2 
#定義最大連線數 
jdbc.maxActive=20
#定義最大空閒 
jdbc.maxIdle=20
#定義最小空閒 
jdbc.minIdle=1
#定義最長等待時間 
jdbc.maxWait=60000
#驗證資料庫連線的有效性
jdbc.validationQuery=select 1

(2)spring/mybatis.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations">
			 <list>
                <value>classpath:sql/*.xml</value>
            </list>
		</property>
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ssm.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
  		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	
</beans>

(3)spring/dataAccessContext.xml配置阿里druid資料來源
DRUID的DataSource類為:com.alibaba.druid.pool.DruidDataSource
普通資料來源連線配置(單個數據源,無主從同步)

<?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:context="http://www.springframework.org/schema/context"  xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	<description>資料庫、事務配置</description>
	
	<!-- 資料來源-->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialSize" value="${jdbc.initialSize}" />
		<property name="maxActive" value="${jdbc.maxActive}" />
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<property name="minIdle" value="${jdbc.minIdle}" />
		<property name="maxWait" value="${jdbc.maxWait}"></property>
		<property name="validationQuery" value="${jdbc.validationQuery}" />
		<!-- 監控資料庫 -->
		<!--<property name="filters" value="mergeStat" />-->
		<property name="filters" value="stat" /> 
		<property name="connectionProperties" value="druid.stat.mergeSql=true" />  
	</bean>
    
    <!-- 使用annotation定義事務,使用cglib代理,解決同一service中事務方法相互呼叫的 巢狀事務失效問題 -->
	<tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true"/>
	<!--事務配置 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
</beans>

多個數據源實現了主從複製配置

<?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:context="http://www.springframework.org/schema/context"  xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	<description>資料庫、事務配置</description>
	
	<!-- 埠號3306的資料來源(主)-->
	<bean id="dataSource3306" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialSize" value="${jdbc.initialSize}" />
		<property name="maxActive" value="${jdbc.maxActive}" />
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<property name="minIdle" value="${jdbc.minIdle}" />
		<property name="maxWait" value="${jdbc.maxWait}"></property>
		<property name="validationQuery" value="${jdbc.validationQuery}" />
		<!-- 監控資料庫 -->
		<!--<property name="filters" value="mergeStat" />-->
		<property name="filters" value="stat" /> 
		<property name="connectionProperties" value="druid.stat.mergeSql=true" />  
	</bean>
	
	<!-- 埠號3308的資料來源(從) -->
	<bean id="dataSource3308" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.3308.url}" />
		<property name="username" value="${jdbc.3308.username}" />
		<property name="password" value="${jdbc.3308.password}" />
		<property name="initialSize" value="${jdbc.initialSize}" />
		<property name="maxActive" value="${jdbc.maxActive}" />
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<property name="minIdle" value="${jdbc.minIdle}" />
		<property name="maxWait" value="${jdbc.maxWait}"></property>
		<property name="validationQuery" value="${jdbc.validationQuery}" />
		<!-- 監控資料庫 -->
		<!--<property name="filters" value="mergeStat" />-->
		<property name="filters" value="stat" /> 
		<property name="connectionProperties" value="druid.stat.mergeSql=true" />  
	</bean>
	
	<!-- 資料來源,需要自定義類繼承AbstractRoutingDataSource,實現determineCurrentLookupKey -->
	<bean id="dataSource" class="com.ssm.datasource.DynamicDataSource">
		<!-- 設定預設資料來源 -->
		<property name="defaultTargetDataSource" ref="dataSource3306"></property>
		<!-- 設定多個數據源,後臺切換資料來源key與這裡key配置需要一致 -->
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry key="dataSource3306" value-ref="dataSource3306"/>
				<entry key="dataSource3308" value-ref="dataSource3308"/>
			</map>
		</property>
		<property name="methodPrefix">
			<map key-type="java.lang.String">
				<entry key="slave">
					<!-- "list","count","find","get","select","query" 等,根據開發人員方法命名習慣配置 -->
					<list>
						<value>list</value>
						<value>count</value>
						<value>find</value>
						<value>get</value>
						<value>select</value>
						<value>query</value>
					</list>
				</entry>
			</map>
		</property>
	</bean>
    
    <!-- 使用annotation定義事務,使用cglib代理,解決同一service中事務方法相互呼叫的 巢狀事務失效問題 -->
	<tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true"/>
	<!--事務配置 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
</beans>

4.執行專案訪問http://localhost:7080/SSM_DEMO/druid/index.html即可