您好,登錄后才能下訂單哦!
解決方案:
多套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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。