1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 16 17 18 19 <!-- 加载jdbc.properties文件 --> 20 <bean id="propertyConfigurer" 21 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 22 <property name="locations"> 23 <list> 24 <value>classpath*:config/datasource.properties</value> 25 </list> 26 </property> 27 </bean> 28 29 <!-- 配置DataSource数据源 --> 30 <!-- <bean id="dataSource" --> 31 <!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource"> --> 32 <!-- <property name="driverClassName" value="${driver}" /> --> 33 <!-- <property name="url" value="${url}" /> --> 34 <!-- <property name="username" value="${username}" /> --> 35 <!-- <property name="password" value="${password}" /> --> 36 <!-- </bean> --> 37 38 <!-- JNDI连接池配置(jboss:java:/socket tomcat : java:comp/env/jdbc/socket)--> 39 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 40 <property name="jndiName"> 41 <value>java:comp/env/jdbc/socket</value> 42 </property> 43 </bean> 44 45 46 47 48 <!-- 配置Mybatis SqlSessionFactoryBean --> 49 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 50 <property name="dataSource" ref="dataSource" /> 51 <property name="configLocation" value="classpath:config/mybatis.xml" /> 52 <!-- mapper和resultmap配置路径 --> 53 <property name="mapperLocations"> 54 <list> 55 <!-- 表示在com/smart/包或以下所有目录中,以_sql.xml结尾所有文件 --> 56 <value>classpath*:com/bd/**/*_sql.xml</value> 57 </list> 58 </property> 59 </bean> 60 61 <!-- 框架 --> 62 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 63 <constructor-arg index="0" ref="sqlSessionFactory" /> 64 </bean> 65 66 67 <bean id="dataSourceHr" class="org.springframework.jndi.JndiObjectFactoryBean"> 68 <property name="jndiName"> 69 <value>java:comp/env/jdbc/hr</value> 70 </property> 71 </bean> 72 73 74 <!-- 配置Mybatis SqlSessionFactoryBean --> 75 <bean id="sqlSessionFactoryHr" class="org.mybatis.spring.SqlSessionFactoryBean"> 76 <property name="dataSource" ref="dataSourceHr" /> 77 <property name="configLocation" value="classpath:config/mybatis.xml" /> 78 <!-- mapper和resultmap配置路径 --> 79 <property name="mapperLocations"> 80 <list> 81 <!-- 表示在com/smart/包或以下所有目录中,以_sql.xml结尾所有文件 --> 82 <value>classpath*:com/bd/**/*_sql.xml</value> 83 </list> 84 </property> 85 </bean> 86 87 <!-- 框架 --> 88 <bean id="sqlSessionHr" class="org.mybatis.spring.SqlSessionTemplate"> 89 <constructor-arg index="0" ref="sqlSessionFactoryHr" /> 90 </bean> 91 92 93 94 <bean id="batisDao" class="com.bd.core.base.dao.mybatis.BatisDaoImpl"> 95 <property name="sqlSession"> 96 <ref bean="sqlSession" /> 97 </property> 98 <property name="sqlSessionHr"> 99 <ref bean="sqlSessionHr" /> 100 </property> 101 </bean> 102 103 <!--事务相关控制--> 104 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 105 <property name="dataSource" ref="dataSource"></property> 106 </bean> 107 108 109 110 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 111 <tx:attributes> 112 <tx:method name="get*" propagation="REQUIRED" 113 rollback-for="Exception,BusinessException" /> 114 <tx:method name="find*" propagation="REQUIRED" 115 rollback-for="Exception,BusinessException" /> 116 <tx:method name="query*" propagation="REQUIRED" 117 rollback-for="Exception,BusinessException" /> 118 <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception,BusinessException" /> 119 <tx:method name="save*" propagation="REQUIRED" 120 rollback-for="Exception,BusinessException" /> 121 <tx:method name="update*" propagation="REQUIRED" 122 rollback-for="Exception,BusinessException" /> 123 <tx:method name="edit*" propagation="REQUIRED" 124 rollback-for="Exception,BusinessException" /> 125 <tx:method name="delete*" propagation="REQUIRED" 126 rollback-for="Exception,BusinessException" /> 127 <tx:method name="remove*" propagation="REQUIRED" 128 rollback-for="Exception,BusinessException" /> 129 <tx:method name="audit*" propagation="REQUIRED" 130 rollback-for="Exception,BusinessException" /> 131 <tx:method name="modify*" propagation="REQUIRED" 132 rollback-for="Exception,BusinessException" /> 133 </tx:attributes> 134 </tx:advice> 135 <aop:config> 136 <aop:pointcut id="interceptorPointCuts" 137 expression="execution(* com.bd.smart.*.*.service..*.*(..)) || execution(* com.bd.smart.*.service..*.*(..))" /> 138 <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /> 139 </aop:config> 140 141 142 143 144 145 </beans>
配置两个数据源和session工厂类和session对象,只需在dao的bean 中注入这些session对象,当调用的是哪个session 就会获取哪个数据库的连接。