解决方案:
多套sqlSessionFactory,针对不同的数据源
数据源
Xml代码
<something-else-entirely>
<proxool>
<alias>peccancy</alias>
<driver-url>jdbc:oracle:thin:@172.16.60.46:1521:orcl</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="oses"/>
<property name="password" value="oses"/>
</driver-properties>
...
</proxool>
<proxool>
<alias>yhtepap</alias>
<driver-url>jdbc:oracle:thin:@172.16.60.46:1521:orcl</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="pap"/>
<property name="password" value="pap"/>
</driver-properties>
...
</proxool>
</something-else-entirely>
1.方法一
Xml代码
<bean id="dataSource_peccancy" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.peccancy</value>
</property>
</bean>
<bean id="dataSource_pap" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.pap</value>
</property>
</bean>
Xml代码
<bean id="sqlSessionFactory_peccancy" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_peccancy" />
<property name="mapperLocations">
<list>
<value>classpath:net/yhte/common/query/mapper/PageData_Mapper.xml</value>
<value>classpath*:net/yhte/web/peccancymappermappermappermapper/*Mapper.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="net.yhte.common.service.PapRepository"></property>
<property name="basePackage" value="net.yhte.web.**.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_pap"></property>
</bean>
如上所配置使用pap的mapper文件就加上注解@PapRepository
Java代码
public @interface PeccancyRepository {
}
public @interface PapRepository {
}
Java代码
// 使用dataSource_pap 数据源
@PapRepository
public interface UserMapper {
List<User> find(UserQueryBean userQueryBean);
}
// 使用dataSource_peccancy 数据源
@PeccancyRepository
public interface ParameterMapper{
List<Parameter> find(ParameterQueryBean queryBean);
}
获取【下载地址】
最主流的Java后台框架 springmvc spring mybatis SSM 项目源码