摘要: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&characterEncoding\=UTF-8&allowMultiQueries\=true
jdbc.username = root
jdbc.password = 123456
#端口号3308的数据源
jdbc.3308.url = jdbc\:mysql\://localhost\:3308/db_ssmdemo?useUnicode\=true&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即可