解决方案:
多套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/peccancy/**/mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.yhte.web.peccancy.**.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_peccancy"></property>
</bean>
<bean id="sqlSessionFactory_pap" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_pap" />
<property name="mapperLocations">
<list>
<value>classpath*:net/yhte/web/pap/**/mapper/*Mapper.xml</value>
<value>classpath:net/yhte/common/query/mapper/PageData_Mapper.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.yhte.web.pap.**.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_pap"></property>
</bean>
本方案需要根据数据源的不同对*Mapper.xml进行分包处理,便于配置,同一个包下的*Mapper.xml如果对应的数据源不同,则不方便使用通配符进行统一扫描配置,且sqlSessionFactory扫包的basePackage 也需要分包。
2.使用自定义注解来取代spring的repository设置到annotationClass中。这样每个mapper文件中注入相应的注解@PapRepository,@PeccancyRepository
<property name="annotationClass" value="org.springframework.stereotype.Repository"></property>
改成<property name="annotationClass" value="net.xxx.PapRepository"></property>
配置文件如下:
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/**/mapper/*Mapper.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="net.yhte.common.service.PeccancyRepository"></property>
<property name="basePackage" value="net.yhte.web.**.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_peccancy"></property>
</bean>
<bean id="sqlSessionFactory_pap" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_pap" />
<property name="mapperLocations">
<list>
<value>classpath:net/yhte/common/query/mapper/PageData_Mapper.xml</value>
<value>classpath*:net/yhte/web/**/mapper/*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 项目源码
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。