springMVC+spring+mybatis多數據源配置
阿新 • • 發佈:2017-06-26
expr init mybatis onf exp 超時 png wait http
1.application.properties配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 自動掃描spring註解,排除springmvc已掃描的Controller註解 --> <context:component-scan base-package="com.ln.web"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- mybatis與spring整合 --> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:application.properties</value> </list> </property> </bean> <!-- dataSource 配置 --> <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.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${ds.initialSize}" /> <property name="minIdle" value="${ds.minIdle}" /> <property name="maxActive" value="${ds.maxActive}" /> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="${ds.maxWait}" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}" /> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}" /> </bean> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/ln/web/dao/*.xml"></property> </bean> <!-- 對dataSource 數據源進行事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 掃描mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ln.web.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 事務管理 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 對insert,update,delete 開頭的方法進行事務管理,只要有異常就回滾 --> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <!-- select,count開頭的方法,開啟只讀,提高數據庫訪問性能 --> <tx:method name="select*" read-only="true"/> <tx:method name="count*" read-only="true"/> <!-- 對其他方法 使用默認的事務管理 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 啟用對事務註解的支持 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 配置多個數據源 --> <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="url" value="${jdbc.url_2}" /> <property name="username" value="${jdbc.username_2}" /> <property name="password" value="${jdbc.password_2}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${ds.initialSize}" /> <property name="minIdle" value="${ds.minIdle}" /> <property name="maxActive" value="${ds.maxActive}" /> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="${ds.maxWait}" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}" /> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}" /> </bean> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config-2.xml" /> <property name="dataSource" ref="dataSource2" /> <property name="mapperLocations" value="classpath:com/ln/web/db2/dao/*.xml"></property> </bean> <!-- 對dataSource 數據源進行事務管理 --> <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource2"></property> </bean> <!-- 事務管理 通知 --> <tx:advice id="txAdvice2" transaction-manager="transactionManager2"> <tx:attributes> <!-- 對insert,update,delete 開頭的方法進行事務管理,只要有異常就回滾 --> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <!-- select,count開頭的方法,開啟只讀,提高數據庫訪問性能 --> <tx:method name="select*" read-only="true"/> <tx:method name="count*" read-only="true"/> <!-- 對其他方法 使用默認的事務管理 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 事務 aop 配置 --> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* com.simple.web.service..*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/> <aop:advisor advice-ref="txAdvice2" pointcut-ref="serviceMethods"/> </aop:config> <!-- 啟用對事務註解的支持 --> <tx:annotation-driven transaction-manager="transactionManager2" /> <!-- 掃描mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ln.web.db2.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"></property> </bean> </beans>
2.mybatis配置文件:
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "classpath:schema/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.ln.web.model"/> </typeAliases> </configuration>
mybatis-config-2.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "classpath:schema/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.ln.web.db2.model"/> </typeAliases> </configuration>
3.目錄結構
springMVC+spring+mybatis多數據源配置