<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 加载jdbc.properties文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:config/datasource.properties</value>
</list>
</property>
</bean> <!-- 配置DataSource数据源 -->
<!-- <bean id="dataSource" -->
<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
<!-- <property name="driverClassName" value="${driver}" /> -->
<!-- <property name="url" value="${url}" /> -->
<!-- <property name="username" value="${username}" /> -->
<!-- <property name="password" value="${password}" /> -->
<!-- </bean> --> <!-- JNDI连接池配置(jboss:java:/socket tomcat : java:comp/env/jdbc/socket)-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/socket</value>
</property>
</bean> <!-- 配置Mybatis SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/mybatis.xml" />
<!-- mapper和resultmap配置路径 -->
<property name="mapperLocations">
<list>
<!-- 表示在com/smart/包或以下所有目录中,以_sql.xml结尾所有文件 -->
<value>classpath*:com/bd/**/*_sql.xml</value>
</list>
</property>
</bean> <!-- 框架 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean> <bean id="dataSourceHr" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/hr</value>
</property>
</bean> <!-- 配置Mybatis SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryHr" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceHr" />
<property name="configLocation" value="classpath:config/mybatis.xml" />
<!-- mapper和resultmap配置路径 -->
<property name="mapperLocations">
<list>
<!-- 表示在com/smart/包或以下所有目录中,以_sql.xml结尾所有文件 -->
<value>classpath*:com/bd/**/*_sql.xml</value>
</list>
</property>
</bean> <!-- 框架 -->
<bean id="sqlSessionHr" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactoryHr" />
</bean> <bean id="batisDao" class="com.bd.core.base.dao.mybatis.BatisDaoImpl">
<property name="sqlSession">
<ref bean="sqlSession" />
</property>
<property name="sqlSessionHr">
<ref bean="sqlSessionHr" />
</property>
</bean> <!--事务相关控制-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="find*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="query*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception,BusinessException" />
<tx:method name="save*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="edit*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="remove*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="audit*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="modify*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.bd.smart.*.*.service..*.*(..)) || execution(* com.bd.smart.*.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config> </beans>
配置两个数据源和session工厂类和session对象,只需在dao的bean 中注入这些session对象,当调用的是哪个session 就会获取哪个数据库的连接。