那么首先,既然说到Druid是“数据库连接池”,那么我们就需要写配置文件来连接到数据库,直接贴码我的配置文件,参数含义就不赘述了,代码中我都写了注释
<?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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--注入properties文件-->
<context:property-placeholder location="classpath:dataSource.properties" ignore-unresolvable="true" />
<!--spring相关注解都需要声明Bean,包括@Autowired,@Resource等等,单个声明太过繁琐,spring就提供了这种简便的方式
注:由于<context:component-scan base-package=”xx.xx”/>也包含了自动注入上述Bean的功能,所以<context:annotation-config/> 可以省略。
如果两者都进行了配置,则只有前者有效。
同时<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。-->
<context:annotation-config />
<!--配置数据库-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverClassName" value="${jdbc.driver.class.name}" />
<!--druid开启日志 value是日志间隔时间,单位是ss-->
<property name="timeBetweenLogStatsMillis" value="300000" />
<!--初始连接数-->
<property name="initialSize" value="${jdbc.initialSize}"/>
<!--连接池中可同时连接的最大的连接数 一些版本maxActive配置选项已重命名为maxTotal-->
<property name="maxActive" value="${jdbc.maxActive}"/>
<!--最小连接池数量 maxIdle已不再使用,配置了也没效果-->
<property name="minIdle" value="${jdbc.minIdle}"/>
<!--最大等待毫秒数, 单位为 ms, 超过时间会出错误信息-->
<property name="maxWait" value="${jdbc.maxWait}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,最小可被驱逐时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<!--验证数据库连接的有效性
Oracle : select 1 from dual Mysql : select 1-->
<property name="validationQuery" value="select 1"/>
<!-- testOnBorrow和testOnReturn在生产环境一般是不开启的,主要是性能考虑。
失效连接主要通过testWhileIdle保证,如果获取到了不可用的数据库连接,一般由应用处理异常。-->
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="proxyFilters" >
<list>
<ref bean="log-filter"/>
<ref bean="stat-filter"/>
</list>
</property>
<!--属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
监控统计用的filter:stat
日志用的filter:log4j
防御sql注入的filter:wall-->
<property name="filters" value="stat,wall" />
</bean>
<!--druid监控-->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<!--慢查询-->
<property name="mergeSql" value="true"/>
<property name="slowSqlMillis" value="1000"/>
<property name="logSlowSql" value="true"/>
</bean>
<!--将监控到的数据持久化到日志-->
<bean id="log-filter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
<!--表示是否显示SQL语句-->
<!--<property name="statementExecutableSqlLogEnable" value="true"/>-->
<!--表示是否显示结果集-->
<property name="resultSetLogEnabled" value="false"/>
<!--所有DataSource相关的日志-->
<property name="dataSourceLogEnabled" value="true" />
<!--所有statement相关的日志-->
<property name="statementLogEnabled" value="true" />
<!--所有连接相关的日志-->
<property name="connectionLogEnabled" value="true" />
</bean>
<!--主库-->
<bean id="masterDateSource" parent="dataSource" init-method="init" destroy-method="close" >
<property name="url" value="${master.jdbc.url}" />
<property name="username" value="${master.jdbc.username}" />
<property name="password" value="${master.jdbc.password}" />
</bean>
<!--从库-->
<bean id="slaveDateSource" parent="dataSource" init-method="init" destroy-method="close" >
<property name="url" value="${slave.jdbc.url}" />
<property name="username" value="${slave.jdbc.username}" />
<property name="password" value="${slave.jdbc.password}" />
</bean>
<!--主从库动态切换切面-->
<bean class="com.abc.framework.db.DataSourceAspect" id="dataSourceAspect"/>
<aop:config>
<aop:aspect ref="dataSourceAspect" >
<aop:pointcut id="dbPonitCut" expression="execution(* com.abc.service.*.*(..))" />
<aop:before method="before" pointcut-ref="dbPonitCut" />
<aop:around method="doAround" pointcut-ref="dbPonitCut"/>
<aop:after method="doAfter" pointcut-ref="dbPonitCut"/>
</aop:aspect>
</aop:config>
<!--整合数据库-->
<bean id="dynamicDataSource" class="com.abc.framework.db.DynamicDataSource" >
<property name="defaultDataSourceName" value="slaveDateSource"/>
<property name="masterDataSourceName" value="masterDateSource"/>
<property name="slaveDataSourceNames" >
<list>
<value>slaveDateSource</value>
</list>
</property>
<property name="targetDataSources" >
<map key-type="java.lang.String">
<entry key="slaveDateSource" value-ref="slaveDateSource" />
<entry key="masterDateSource" value-ref="masterDateSource"/>
</map>
</property>
</bean>
<!--spring管理事物-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" c:dataSource-ref="dynamicDataSource" />
<!--启动对事物注解的支持-->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:context/mybaties/page-plugin.xml"/>
<property name="dataSource" ref="dynamicDataSource" />
<property name="mapperLocations">
<list>
<value>classpath:mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<!-- 通过扫描的模式,自动注入bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.abc.dao" />
</bean>
</beans>