那么首先,既然說到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>