您好,登錄后才能下訂單哦!
解決方案:
多套sqlSessionFactory,針對不同的數(shù)據(jù)源
數(shù)據(jù)源
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>
本方案需要根據(jù)數(shù)據(jù)源的不同對*Mapper.xml進行分包處理,便于配置,同一個包下的*Mapper.xml如果對應(yīng)的數(shù)據(jù)源不同,則不方便使用通配符進行統(tǒng)一掃描配置,且sqlSessionFactory掃包的basePackage 也需要分包。
2.使用自定義注解來取代spring的repository設(shè)置到annotationClass中。這樣每個mapper文件中注入相應(yīng)的注解@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 數(shù)據(jù)源
@PapRepository
public interface UserMapper {
List<User> find(UserQueryBean userQueryBean);
}
// 使用dataSource_peccancy 數(shù)據(jù)源
@PeccancyRepository
public interface ParameterMapper{
List<Parameter> find(ParameterQueryBean queryBean);
}
獲取【下載地址】
最主流的Java后臺框架 springmvc spring mybatis SSM 項目源碼
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。